Variable Initialization

Top 

 

Any variable, except for formal parameters, can be initialized upon definition.

Any permanent variable is initialized with 0, unless other initial value is explicitly defined.

Any expression can be used as the initial value.

Basic types

Examples:

 int i = 1 + j;

 float x = sin(_PI / 2);

Arrays

Examples:

 int a[] = {1,4,9,16,25,36};

 char s[20] = { 'a', 'b', 8 };

The values of array elements are listed in curly brackets.

If an array size is defined, then the values, which are not explicitly defined, will be equal to 0.

If an array size is omitted, then it will be determined by the amount of initial values.

Strings

Example:

 char s[] = "hello";

This description is equivalent to the description of

 char s[] = {'h','e','l','l','o','\0'};