ENEB340 Week04 Data Types
ENEB340 Week04 Data Types
Øscope:
– region of program in which a variable is known
Østorage duration:
– the time period when a variable exists in memory
Ølinkage:
– in multiple-source-file programs (more than one
*.c file), indicates the connection of variables in
one file to other files.
Global variables
Ø a global variable is defined outside (or between) functions.
Ø The scope of a global variable extends from the point of
definition up to the end of the file (but not before the
definition).
Ø global variables can be initialized only by a constant (not an
expression).
Ø global variables will be set to zero if not explicitly initialized.
Ø initialization of all globals occurs before main starts.
Ø globals exist for the life of the program
Automatic variables (auto)
Ø an automatic variable is defined inside a function or a
statement block {}.
Ø they must be defined immediately after the opening {.
Ø The scope of an automatic variable extends from the point of
definition up to the end of the function or block. No code
outside this block can access the variable.
Ø automatic variables can be initialized with an expression.
Ø automatic variables will contain garbage if not explicitly
initialized (they are NOT set to zero).
Ø initialization occurs at “run time”.
Automatic variables - continued
Ø duration: automatic variables are created when the execution
of their statement block begins and the variables cease to
exist when the block execution concludes. This means that the
next time the block is run, the value from the previous
execution is GONE!
Ø If a global variable and an automatic variable have the same
name, the global variable is “hidden” from the program for
the duration of the automatic variable’s existence. The global
variable becomes accessible to the program as soon as the
automatic variable is gone and retains its original value.
Global versus automatic
Øwhenever practical, automatic variables
should be used because:
– minimizes storage and program size.
– reduces the chance of accidently modifying a
variable (when inadvertently used for two
different purposes).
– improves the portability and reusability of the
code to have functions as self-contained units.
Register variables
Øvariables are normally stored in memory.
ØThe time it takes to acquire data from memory
is usually much longer than the time it takes
to perform an operation in the CPU/ALU.
ØC allows you to request that frequently used
variables be stored in CPU registers rather
than memory.
Register variables - continued
Ø place register in front of the variable type to request
a register variable.
Ø There are a limited number of registers, so the
compiler may choose to ignore request, in which
case the variable is stored in memory.
Ø global variables can not be register variables.
Ø arrays can not be register variables.
Ø function arguments can be register variables
Ø same initialization rules as auto variables
Static variables
Økeyword static precedes data type.
Øhas the scope of an auto variable.
Øduration is extended beyond the execution
of the statement block so that static
variables retain their values between
function calls.
Øcan be initialized only with a constant.
ØSet to zero if not explicitly initialized.
Program 4.3 - concept demonstration
#include <stdio.h>
/* demonstration of storage classes
written by W. Lawson
last modified 27 Sept 2019 */
int i=10, j=5, k; // global variables
int main(int argc, char *argv[])
{
int l=1, func(int);
printf("main: i=%d\t j=%d\t k=%d\t l=%d\n",i,j,k,l);
func(i);
l=func(j);
printf("main: i=%d\t j=%d\t k=%d\t l=%d\n",i,j,k,l);
return 0;
}
Program 4.3 - continuation
int func (register int i)
{
auto int j=i+4, l;
static int k;
printf("func: i=%d\t j=%d\t k=%d\t l=%d\n",i++,j++,k++,l++);
return k;
}
Program 4.3 - Integrated testing
int i=10, j=5, k; // global variables
int main(int argc, char *argv[])
{
int l, func(int);
printf("main: i=%d\t j=%d\t k=%d\t l=%d\n",i,j,k,l);
func(i);
l=func(j);
printf("main: i=%d\t j=%d\t k=%d\t l=%d\n",i,j,k,l);
return 0;
}
int func (register int i)
{
auto int j=i+4, l;
static int k;
printf("func: i=%d\t j=%d\t k=%d\t
l=%d\n",i++,j++,k++,l++);
return k;
}