6.16 Declarations - 2
- Variables and functions need to be defined once. When a variable is
defined storage space is set aside for it. A variable can only be
initialized
when it is defined. When a function is defined code is generated.
- Variables need to be declared in each function that wants to access it.
This may be done explicitly, with a storage class specifiers of
extern
or implicitly by context.
- If the definition of an external variable occours in the same source file
before it is used, no declaration is needed. Kernighan p 33.
- Common practice is to place all definitions for all external variables
at the start of the file and omit all declarations Kernighan p 33.
extern
is used to declared a variable not to define it.
- If a external variable is to referenced before it is defined, or if it is
defined in a different source file from the one where it is being used,
then an
extern
declaration is required.
- A static storage class object has a lifetime of the entire
execution of
the program and is initilized only once. ANSI "C" Standard 9899 p32
6.2.4 paragraph 3.
Internal static variables provide private permanent storage within a
single function. Kernighan p83.
- The scope of a function can be limited to a single source file (or
compilation unit) by using the static storage class.
Instructor: Louis Taber, louis.taber.at.pima at gmail dot com (520) 206-6850
My web site in Cleveland, OH
The Pima Community College web site