I’m playing with static keyword. In the code below I can’t figure out why x save it’s before state and increment.
I was expecting to print 1 tree times. As I know a such behaivor should happen if I declare x as static.
void print_it(void);
int main (int argc, const char * argv[])
{
print_it();
print_it();
print_it();
exit(EXIT_SUCCESS);
}
void print_it(void)
{
int x;
printf("%d\n", x++);
}
You have not initialized x to any value. Therefore, the initial value in
xwould be garbage and as it happens, this garbage increments itself every time because it is probably using the same memory location everytime.Try changing your code to this: