You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
616 B
C
32 lines
616 B
C
#include <stdio.h>
|
|
|
|
//In order to share state between functions want to use static variables in file scope
|
|
//At file scope like this static variables are only available within the same source file
|
|
//removing static it's possible that other files could cause name collision
|
|
|
|
static int eggs; //defaults to zero unless you provide your own initializer
|
|
|
|
|
|
void up()
|
|
{
|
|
eggs += 10;
|
|
}
|
|
|
|
void down()
|
|
{
|
|
eggs -= 5;
|
|
}
|
|
|
|
|
|
int main(int argc, char const *argv[])
|
|
{
|
|
up();
|
|
up();
|
|
down();
|
|
|
|
//Here we are sharing state across functions and at the end of this should have 15 eggs
|
|
|
|
printf("You have %d eggs\n", eggs);
|
|
|
|
return 0;
|
|
} |