Implicit initialization of variables with 0 or 1 in C

In C programming, uninitialized variables and partially initialized arrays follow specific rules for implicit initialization. Global variables, static variables, and array elements are automatically initialized to zero, while local variables contain garbage values if not explicitly initialized.

Syntax

// Global variables - implicitly initialized to 0
int global_var;
int global_array[SIZE];

// Partial array initialization - remaining elements become 0
int array[SIZE] = {value1, value2, ...};

Example 1: Global Variables Implicit Initialization

Global and static variables are automatically initialized to zero −

#include <stdio.h>

int x, y;           // Global variables - implicitly initialized to 0
int array[3];       // Global array - all elements initialized to 0

int main() {
    int index;
    printf("x = %d, y = %d

", x, y); for(index = 0; index < 3; index++) { printf("Array[%d] = %d
", index, array[index]); } return 0; }
x = 0, y = 0

Array[0] = 0
Array[1] = 0
Array[2] = 0

Example 2: Partial Array Initialization

When an array is partially initialized, the remaining elements are automatically set to zero −

#include <stdio.h>

int main() {
    int index;
    int array[10] = {1, 2, 3, 4, 5, 6}; // Only first 6 elements initialized
    
    for(index = 0; index < 10; index++) {
        printf("Array[%d] = %d
", index, array[index]); } return 0; }
Array[0] = 1
Array[1] = 2
Array[2] = 3
Array[3] = 4
Array[4] = 5
Array[5] = 6
Array[6] = 0
Array[7] = 0
Array[8] = 0
Array[9] = 0

Key Points

  • Global and static variables are automatically initialized to zero
  • Local variables contain garbage values if not initialized
  • Partial array initialization sets remaining elements to zero
  • This behavior is guaranteed by the C standard

Conclusion

Understanding implicit initialization helps write more predictable C code. Global variables and arrays are zero-initialized, while partial array initialization fills remaining elements with zeros automatically.

Updated on: 2026-03-15T10:31:11+05:30

366 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements