Skip to content

Instantly share code, notes, and snippets.

@cmcbride
Created May 19, 2011 06:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmcbride/980272 to your computer and use it in GitHub Desktop.
Save cmcbride/980272 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void
func( )
{
static int x = 0; // x is initialized only once across three calls of func()
printf( " %d", x ); // outputs the value of x
x = x + 1;
}
void
func2( )
{
static int x = 1; // this is another static variable, but with different scope
printf( " %d", x );
x = x + 2;
}
int
main( int argc, char *const argv[] )
{
printf( "func() : " );
func( ); // prints 0
func( ); // prints 1
func( ); // prints 2
printf( "\nfunc2() : " );
func2( ); // prints 1
func2( ); // prints 3
func2( ); // prints 5
puts( "" );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment