In the absence of explicit initialization, external and static
variables are guaranteed to be initialized to zero; automatic variables (including register
variables) have indeterminate1 (i.e., garbage) initial values.
Scalar variables may be initialized when they are defined by following the name with an equals sign and an expression:
int x = 1;
char squota = '\'';
long day = 1000L * 60L * 60L * 24L; /* milliseconds/day */
For external and static
variables, the initializer must be a constant expression2; the initialization is done once, conceptually before the program begins execution.
For automatic and register
variables, the initializer is not restricted to being a constant: it may be any expression involving previously defined values, even function calls.
For example, see the code snippet below
int binsearch(int x, int v[], int n)
{
int low = 0;
int high = n - 1;
int mid;
...
}
instead of
int low, high, mid;
low = 0;
high = n - 1;
In effect, initialization of automatic variables are just shorthand for assignment statements. Which form to prefer is largely a matter of taste. We generally use explicit assignments, because initializers in declarations are harder to see and further away from the point of use. On the other hand, variables should only be declared when they're about to be used whenever possible.
Initializing an array:
An array may be initialized by following its declaration with a list of initializers enclosed in braces and separated by commas.
For example, to initialize an array days with the number of days in each month:
int days_of_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
(Note that January is encoded as month zero in this structure.)
When the size of the array is omitted, the compiler will compute the length by counting the initializers, of which there are 12 in this case.
If there are fewer initializers for an array than the specified size, the others will be zero for all types of variables.
It is an error to have too many initializers. There is no standard way to specify repetition of an initializer — but GCC has an extension to do so.
In C89/C90 or earlier versions of C, there was no way to initialize an element in the middle of an array without supplying all the preceding values as well.
With C99 and above, designated initializers allow you to initialize arbitrary elements of an array, leaving any uninitialized values as zeros.
Initializing Character arrays:
Character arrays are a special case of initialization; a string may be used instead of the braces and commas notation:
char chr_array[] = "hello";
is a shorthand for the longer but equivalent:
char chr_array[] = { 'h', 'e', 'l', 'l', 'o', '\0' };
In this case, the array size is six (five characters plus the terminating '\0'
).
1 What happens to a declared, uninitialized variable in C? Does it have a value?
2 Note that a constant expression is defined as something that can be evaluated at compile-time. So, int global_var = f();
is invalid. Another common misconception is thinking of a const
qualified variable as a constant expression. In C, const
means "read-only", not "compile time constant". So, global definitions like const int SIZE = 10; int global_arr[SIZE];
and const int SIZE = 10; int global_var = SIZE;
are not legal in C.