1

In this piece of code, why in my testing the result is always 1, 2, and 3?

#include <stdio.h>

void test() {
    int a;
    a++;

    printf("%d",a);
}

int main(int argc, char *argv[]) {
    test();
    test();
    test();
}

I think the variable in test() is static, isn't it? And Why?

Ken Kin
  • 4,503
  • 3
  • 38
  • 76
iLeoDo
  • 397
  • 3
  • 11
  • 1
    http://stackoverflow.com/questions/1597405/what-happens-to-a-declared-uninitialized-variable-in-c-does-it-have-a-value – adt Feb 01 '13 at 12:41

5 Answers5

9

The variable is not static. You're accessing an uninitialized variable. The behavior is undefined.

Art
  • 19,807
  • 1
  • 34
  • 60
3

As other answers told, your variable is not initialized. It prints 1, 2 and 3 possibly because of your compiler compiled the code with prolog(prologue) that cleans the stack with zeros.

The local variables in C, actually point to the offset on your stack, and the stack frame will be restored after the call is returned.

I googled and selected a artical randomly that told about this, see [How function calls work?].

Here is a video talk about that [Assembly Programming Assembly Function Stack Frame Explained ] (again, selected randomly).

And Wikipedia explains the [Call stack] as well.

Ken Kin
  • 4,503
  • 3
  • 38
  • 76
2

Because you are working with an unintialize value... (random behaviour in this case).

Initialize your variable (example to 0) :

#include <stdio.h>
void test(){
    int a=0;
    a++;
    printf("%d",a);
}
int main(int argc, char *argv[]) {
    test();
    test();
    test();
}
Joze
  • 1,285
  • 4
  • 19
  • 33
  • It's quite complicated ! It could have been : 5, 8, 7. It's indeterminate ! Your variable 'a' could be : 0, the value it was before (probably your case) or even crash your program ! You always have to intialize variable. You probably got a warning, don't you ? – Joze Feb 01 '13 at 12:55
  • @Leo.Dong, the reason you are seeing 1, 2, 3 is because the stack frame of `test` so happens to be laid out on exactly the same spot as before, effectively making `a` use its previous value. This is totally undefined behavior and not to be relied upon. You could try inserting a couple of function calls (such as `printf`) in between your `test()` calls and see that the uninitialized value of `a` actually gets affected by whatever is called before `test()`. – Shahbaz Feb 01 '13 at 13:36
2

No, your variable is not static at all!

https://stackoverflow.com/a/1597426/1758762

Static variables (file scope and function static) are initialized to zero:

int x; // zero
int y = 0; // also zero

    void foo() {
        static int x; // also zero
    }

Non-static variables (local variables) are indeterminate. Reading them prior to assigning a value results in undefined behavior.

void foo() {
    int x;
    printf("%d", x); // the compiler is free to crash here
}
Community
  • 1
  • 1
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92
1

the variable you are trying to print is not static and neither it is initialized , thus it is taking garbage value , which seems random to you , if you execute this program on a different machine ,then there you will have different output because there you will have different garbage value

in order to avoid it , you will have to initialize your variable with some value