Node:Initialization, Next:, Previous:Declarations, Up:Variables and declarations



Initialization

Assigning a variable its first value is called initializing the variable. When you declare a variable in C, you can also initialize it at the same time. This is no more efficient in terms of a running program than doing it in two stages, but sometimes creates tidier and more compact code. Consider the following:

int initial_year;
float percent_complete;

initial_year = 1969;
percent_complete = 89.5;

The code above is equivalent to the code below, but the code below is more compact.

int initial_year = 1969;
float percent_complete = 89.5;

You can always write declarations and initializers this way, but you may not always want to. (See Style.)