Skip to content

Instantly share code, notes, and snippets.

@theonewolf
Created September 14, 2012 02:36
Show Gist options
  • Select an option

  • Save theonewolf/3719473 to your computer and use it in GitHub Desktop.

Select an option

Save theonewolf/3719473 to your computer and use it in GitHub Desktop.
static keyword C puzzle :-)
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int static_puzzle()
{
static int i = 0;
for (; i < 10; i++)
{
return i;
}
return i;
}
int static_puzzle2()
{
static int i = 0;
for (; i < 10;)
{
return i++;
}
return i;
}
int main(int argc, char* argv[])
{
int val;
while ((val = static_puzzle2()) < 10)
{
fprintf(stdout, "%d\n", val);
fflush(stdout);
}
sleep(5);
while ((val = static_puzzle()) < 10)
{
fprintf(stdout, "%d\n", val);
fflush(stdout);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment