C

Initialization in C Explained

When learning the C programming language, you will frequently encounter three important terms: declaration, definition, and initialization.

Although these concepts are closely related, they serve different purposes. Understanding the differences is essential for writing well-structured C programs, especially when working with multiple source files.

The topic about declaration vs definition I already covered in a separate article, so in this, I will explore the initialization concept, and demonstrate the practical usage with code examples.

What is Initialization in C?

Initialization provides an initial value for an object as it is created. For example:

int counter = 10;

Here, counter is defined and initialized with the value 10. Initialization should not be confused with the assignment.

int counter = 10;    // Initialization

counter = 20;        // Assignment

The first statement initializes the variable, while the second modifies its value after initialization. The initialization must happen first. After a variable has been initialized, we can assign another value to it.

Initialization of Arrays and Structures

Initialization is not limited to simple variables. An array can be initialized as follows:

int numbers[3] = {1, 2, 3};

Similarly, a structure can be initialized using an initializer list:

Struct Point {
    int x;
    int y;
};

struct Point position = {10, 20};

C also supports designated initializers, which make it possible to initialize specific structure members explicitly.

struct Point position = {
    .x = 10,
    .y = 20
};

Designated initializers are particularly useful in embedded software, where structures are frequently used to represent configuration data.

What happens if a Variable is Not Initialized in C?

In C, the behavior of a variable that has not been explicitly initialized depends on its storage duration.

Storage duration determines how long an object exists in memory during program execution. It also affects whether the object receives an initial value automatically.

Let’s compare three common cases: global variables, local variables, and static local variables.

Global Variables

Variables declared outside functions, at file scope, have static storage duration. This means they exist throughout the execution of the program. If a global variable is not explicitly initialized, it is automatically initialized to zero.

int counter;             // Initialized to 0
int temperature = 25;    // Explicitly initialized
int main(void)
{
    printf("%d\n", counter);    // Output: 0
    return 0;
}

This behavior applies to global variables regardless of whether they are declared with the static keyword. For example:

int global_counter;           // Initialized to 0
static int private_counter    // Also initialized to 0

Both variables have static storage duration. The difference is their linkage, not their initialization behavior. The static keyword at file scope gives the variable internal linkage, meaning its name cannot be used to refer to the same object from another translation unit.

Local Variables

Local variables declared inside functions or blocks normally have automatic storage duration. Unlike global variables, ordinary local variables are not automatically initialized.

void example(void)
{
    int counter; // Uninitialized local variable

    printf("%d\n", counter); // Invalid: do not read
}

The variable counter has an indeterminate value.

Reading an uninitialized automatic variable can result in undefined behavior. In particular, the example above must not be treated as a valid way to obtain a random value. Although the variable may appear to contain data left in memory from previous operations, its behavior is not guaranteed by the C standard.

Good practice: Always initialize local variables before reading their values.

void example(void)
{
    int counter = 0;

    printf("%d\n", counter); // Output: 0
}

Static Local Variables

A local variable declared with the static keyword behaves differently from an ordinary local variable. Although it is declared inside a function, it has static storage duration. This means it is automatically initialized to zero if no explicit initializer is provided, and its value is preserved between function calls.

{
    static int counter;

    counter++;

    printf("%d\n", counter);
}

int main(void)
{
    increment(); // Output: 1
    increment(); // Output: 2
    increment(); // Output: 3

    return 0;
}

Unlike an ordinary local variable, counter is not recreated each time the function is called. Instead, the same object exists throughout the program’s execution.

Comparison: Global vs Local vs Static Variables

The following table summarizes the most important differences.

Variable typeStorage durationDefault initializationLifetime
Global variableStaticZeroEntire program
Global static variableStaticZeroEntire program
Local variableAutomaticIndeterminateDruing bloc execution
Local static variableStaticZeroEntire program

Important: Zero initialization does not apply only to integer variables. For objects with static storage duration, the C language also guarantees appropriate initialization of other types. For example:

static int counter;       // 0
static float temperature; // 0.0
static int *ptr;          // Null pointer
static int array[3];      // {0, 0, 0}

Why Does This Matter in Embedded Systems?

Understanding storage duration is particularly important in embedded software development. On many embedded platforms, variables with static storage duration are placed in specific memory sections:

  • .bss — typically contains zero-initialized variables.
  • .data — typically contains variables with nonzero initial values.
  • Stack — typically stores ordinary automatic local variables, although compiler optimizations may keep some values in CPU registers.

During startup, the runtime initialization code typically clears the .bss section and copies the initial values of .data variables from nonvolatile memory into RAM.

This happens before the main() function is called.

As a result, global and static variables receive their initial values automatically, while ordinary local variables must be initialized explicitly before their values are used. These memory sections are common implementation conventions, not requirements imposed by the C language standard.

Summary

Declarations, definitions, and initialization are fundamental concepts in the C programming language.

A declaration introduces an identifier and specifies its properties.

A definition establishes an object or provides the implementation of a function.

Initialization provides an initial value when an object is created.

Understanding these differences is especially important when organizing C applications into multiple source files, sharing variables between modules, and debugging compiler or linker errors.

Hello šŸ‘‹ Nice to meet you.

Sign up here if you would like to be notified when new content is added to the blog.

We will not spam you. You will only receive important information, such as changes to the blog or new content.

Leave a Reply