Initialization
Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression. Some examples are:
int index = 0, max = 99, *intptr = NULL; static char message[20] = "Example!";
Variables are not initialized in declarations that do not cause an object to be created, such as function prototypes and declarations that refer to external variable definitions.
Every initialization is subject to the following rules:
A variable declaration with an initializer is always a definition. This means that storage is allocated for the variable.
A variable with static storage duration can only be initialized with a value that can be calculated at the time of compiling. Hence the initial value must be a constant expression.
For declarations without an initializer: variables with static storage duration are implicitly initialized with NULL (all bytes have the value 0); the initial value of all other variables is undefined!
The type conversion rules for simple assignments are also applied on initialization.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access