autoauto is the default for function/block variables
auto int a is the same as int a
registerregister provides a hint to the compiler that you think
a variable will be frequently used
register hint
auto variable
with the exception that you may not take the address of a register
(since, if put in a register, the variable will not have an address)
static
int
counter(void)
{
static int cnt = 0;
return cnt++;
}
causes the counter() function
to return a constantly increasing number
externextern keyword is used to
inform the compiler of the variable's existence:
int farvar;
{
extern int farvar;
int a;
a = farvar * 2;
}
extern keyword is for declarations,
not definitions
extern declaration does not create any storage; that
must be done with a global definition
staticstatic keyword is to ensure
that code outside this file cannot modify variables that are globally
declared inside this file
static int farvar;then the
extern int farvar statement in use.c
would cause an error
static is commonly used in situations where
a group of functions need to share information but do not want to risk
other functions changing their internal variables
static int do_ping = 1; /* start with `PING' */
void
ping(void)
{
if (do_ping == 1) {
printf("PING ");
do_ping = 0;
}
}
void
pong(void)
{
if (do_ping == 0) {
printf("PONG\n");
do_ping = 1;
}
}
auto, register and static
variables may be initialized at creation:
int
main(void)
{
int a = 0;
register int start = 1234;
static float pi = 3.141593;
}
static variables which have
not been explicitly initialized by the programmer are set to zero
auto or register variable has
not been explicitly initialized, it contains whatever was previously
stored in the space that is allocated to it
auto and register
variables should always be initialized before being used