When a variable is declared within a function block with the static keyword, that variable is accessible only from within that function block when the function is called. The initial value of the static value is assigned at compile time and is not re-evaluated at runtime. Therefore, the value assigned to the static variable must be known at compile time and cannot be an expression or variable.
Consider the following program:
#include <stdio.h>
void printHeading( const char* aHeading );
int main( void ) {
printHeading( "Title Page" );
printHeading( "Chapter 1 " );
printHeading( "" );
printHeading( "" );
printHeading( "Chapter 2 " );
printHeading( "" );
printHeading( "Conclusion" );
}
void printHeading( const char* aHeading ) {
static int pageNo = 1;
printf( "%s \t\t\t Page %d\n" , aHeading , pageNo);
pageNo++;
}
The printHeading()function contains the...