Created
February 23, 2012 22:22
-
-
Save theonewolf/1895367 to your computer and use it in GitHub Desktop.
odd static test in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 | |
* | |
* Why does the second function not properly update the static variable i? | |
* I encountered this behavior in a more complex for loop. | |
*/ | |
#include <stdio.h> | |
int static_good() | |
{ | |
static int i = 0; | |
i++; | |
return i; | |
} | |
int not_static_good() | |
{ | |
static int i = 0; | |
for (; i < 10; i++) | |
return i; /* I think the return occurs before the loop increment...a bit unexpected */ | |
return 0; | |
} | |
int main() | |
{ | |
while (1) | |
fprintf(stdout, "%d -- %d\n", static_good(), not_static_good()); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment