|
|
Apply whet you just learned and figure out what this program will print out:
#include <stdio.h>
void f( )
{
int i = 100; // local variable
static int j = 100; // Static local variable
i++;
j++;
printf( "i = %d, j = %d\n", i, j );
}
int main( int argc, char* argv[] )
{
f(); // Calls f() 4 times...
f();
f(); // What is printed by each call ?
f();
}
|
DEMO: demo/C/set1/kinds-vars2.c
Answer:
#include <stdio.h>
void f( )
{
int i = 100; // local variable
static int j = 100; // Static local variable
i++;
j++;
printf( "i = %d, j = %d\n", i, j );
}
int main( int argc, char* argv[] )
{
f(); // 101 101
f(); // 101 102
f(); // 101 103
f(); // 101 104
}
|
Because
i is
initialized to 100 ==>
each time the
function
f( ) is
called, it executes
i = 100 + 1 =
101
Because
j is
initialized at the start
(only)
==>
each time the
function
f( ) is
called,
j is
incremented