Course Content
Programming in C
About Lesson

All variables must be declared before use. A declaration specifies a type and contains a list of one or more variable names of that type. For example:

int age, height_in_cm, weight;
float income;
char name[32];

A variable may also be initialised in its declaration. For example:

int age = 21;

The initialisation is done only once for a non-automatic variable; the initialiser must be a constant expression. An explicitly initialised automatic variable is initialised every time the function or block containing it is entered; the initialiser may be any expression. External and static variables are initialised to zero by default. Automatic variables, with no explicit initialisers, have garbage (undefined) values by default.

The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed. For an array, the const qualifier suggests that the elements will not be altered.

const float pi = 3.14f;

The const declaration can also be used with array arguments, to indicate the function does not change that array:

int strlen(const char[]);

The outcome of changing values of a const is dependent on the implementation.

Scroll to Top