hi Oxymoron,
in C language, there are more than one storage class of vars
(for storage class is not intended char,intfloat,ecc)
the most important are
global, static, automatic
(only static verb you meet in program, the other are implicit)
exists other, as register or other but they depends from Machine type and OS.
int total; // global one
void print()
{
static int ntime_called ; // static
ntime_called++ ;
printf( "%d %d\n", total, ntime_called ) ;
}
void load()
{
total = 10 ;
}
main()
{
int i ; // automatic
load();
for( i=0 ; i<3 ; i++ )
print();
}
A global is a var declared outside a routine: these vars
are loaded with 0 before program runs.
Automatic are declared inside ruotine, and they have life
from the begin of routine utils its end: after they come forgotten (the stotage area is the stack and it is lost when
the routine exist). The begin value is unpredictable, and it
has to be initialized
Static are a mix of them: we need them inside a routine and their scope does not outbound the routine, but, they are not stack-stored and their initial value is 0. Furthemore when you go back in a routine already called, the value of a static var is maintained, and it may be useful for counter or other. (another typical use is when you perform a return of a pointer)
char* String()
{
static char buffer[10];
...
return buffer;
}
The use of static verb outside rotine is allowed, but not useful for vars. Instead may be very important before a rotine name:
static void Total()
{
}
This tells to compiler and linker that the scope of rtn does not outbound module (you can have more than 1 Total bodies of routine in different files with different body in each module ( a module typically is a file)
The Total() rtn that stays in file p1.c is different from that is in p2.c, but they can have the same name, is at a name you give a meaning.
You can call them p1_Total() and p2_Total(), without declare them static and dont receive a linker message:
Total() has already a body, but is easier call them with the same name (if it has a sense) and prefix them with static verb.
ciao,
Vittorio