Skip to content

Instantly share code, notes, and snippets.

@theonewolf
Created February 23, 2012 22:22
Show Gist options
  • Save theonewolf/1895367 to your computer and use it in GitHub Desktop.
Save theonewolf/1895367 to your computer and use it in GitHub Desktop.
odd static test in C
/* 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