Declaring a variable =
inform the
C compiler about the
data type of a variable
Actual goal
of a declaration:
Provide sufficient information
to the C compiler to
access the variable in
memory.
Information necessary to access a variable in memory
Information needed to
access a variable:
Location of the variable in
memory
Number of bytes (= size)
the comprises the variable.
Example:
How the information is
conveyed to the
C compiler:
The location information is
recorded by the
C compiler when the variable is
defined
(and memory cells are reserved in memory to store that variable)
The size information is
conveyed to the C compiler
through the
data type
in the
variable definition or
variable declaration.
How the C compiler works:
During the compilation
(translating C statement to
machine code), a
variable definition will
cause the C compiler to
reserve memory cells (record address)
and remember the type (size info)
During the compilation
(translating C statement to
machine code), a
variable declaration will
cause the C compiler to
generate a dummy address for that
variable name
and remember the type (size info)
All the dummy addresses
generated in the compilation phase is
filled in during the
linking process where
information from
multiple C program files are merged
The C compiler must locate
the location of every
declared variable and
replace the
dummy address with the
actual location of the
variable found in the file where the variable was
defined.