This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

How to Init Local Static Variable

Dear All,

I write a function with Local Static Variable. But can't get its value initiated.

void analyzeInputs() {
        static uByte TimeState = 0; // Init Value

        switch (TimeState) {
                case 0 :
                        TimeState++;
                        // MORE CODE HERE ...
                        break;
                case 1 :
                        TimeState++;
                        // MORE CODE HERE ...
                        break;
                case 2 :
                        TimeState = 0;
                        // MORE CODE HERE ...
                        break;
        }
}

When go to debug mode, at first, it always jump to Case 1 (not 0), mean : no value initiated.

But if I change, declare "TimeState" as Global Var then its value init fine.

-> Is it because the Keil won't init any static local variable during Start-Up Code ?

Parents
  • it always jump to Case 1 (not 0), mean : no value initiated.

    First of all, you don't really know where it jumped to. The compiler may just be cleverer than you imagined it could be.

    Second, why do you think you have to inspect execution order to see what value your switch() variable has? Just inspect the variable itself!

Reply
  • it always jump to Case 1 (not 0), mean : no value initiated.

    First of all, you don't really know where it jumped to. The compiler may just be cleverer than you imagined it could be.

    Second, why do you think you have to inspect execution order to see what value your switch() variable has? Just inspect the variable itself!

Children