Total Pageviews

Saturday, November 13, 2010

Lesson 24 - C-Variables

In C, a variable must be declared before it can be used. Variables can be declared at the start of any block of code, but most are found at the start of each function. Most local variables are created when the function is called, and are destroyed on return from that function.

A declaration begins with the type, followed by the name of one or more variables. For example,


int high, low, results[20];
Declarations can be spread out, allowing space for an explanatory comment. Variables can also be initialised when they are declared, this is done by adding an equals sign and the required value after the declaration.

int high = 250; /* Maximum Temperature */
int low = -40; /* Minimum Temperature */
int results[20]; /* Series of temperature readings */
C provides a wide range of types. The most common are

int An Integer
float A floating point (real) number
char A single byte of memory, enough to hold a character


There are also several variants on these types.

short An integer, possibly of reduced range.
long An integer possibly of increased range.
unsigned An integer with no negative range, the spare capacity being used to increase the positive range.
unsgined long Like unsigned, possibly of increased range.
double A double precision floating point number.


All of the integer types plus the char are called the integral types. float and double are called the real types.

Global Variables

Local variables are declared within the body of a function, and can only be used within that function. This is usually no problem, since when another function is called, all required data is passed to it as arguments. Alternatively, a variable can be declared globally so it is available to all functions. Modern programming practice recommends against the excessive use of global variables. They can lead to poor program structure, and tend to clog up the available name space.

A global variable declaration looks normal, but is located outside any of the program's functions. This is usually done at the beginning of the program file, but after preprocessor directives. The variable is not declared again in the body of the functions which access it.

External Variables

Where a global variable is declared in one file, but used by functions from another, then the variable is called an external variable in these functions, and must be declared as such. The declaration must be preceeded by the word extern. The declaration is required so the compiler can find the type of the variable without having to search through several source files for the declaration.

Global and external variables can be of any legal type. They can be initialised, but the initialisation takes place when the program starts up, before entry to the main function.

Static Variables

Another class of local variable is the static type. A static can only be accessed from the function in which it was declared, like a local variable. The static variable is not destroyed on exit from the function, instead its value is preserved, and becomes available again when the function is next called. Static variables are declared as local variables, but the declaration is preceeded by the word static.


static int counter;
Static variables can be initialised as normal, the initialisation is performed once only, when the program starts up.

No comments:

Post a Comment