Why are global and static variables initialized to their default values in C/C++?

Global and static variables are initialized to their default values because it is mandated by the C and C++ standards. The compiler assigns zero at compile time, and it costs nothing extra to do so. Both static and global variables behave the same way in the generated object code ? they are allocated in the .bss segment (Block Started by Symbol), and when the program is loaded into memory, the OS zeroes out this entire segment automatically.

In contrast, local (automatic) variables are stored on the stack and are not initialized ? they contain whatever garbage value was previously at that memory location.

Memory Layout

C/C++ Memory Layout Stack ? local vars Heap ? malloc/new .bss Segment ? zeroed out .data Segment ? initialized .text Segment ? code Global & static vars (uninitialized) ? .bss ? zeroed Global & static vars (initialized) ? .data

Default Values by Type

Data Type Default Value
int 0
float / double 0.0
char '\0' (null character)
pointer NULL

Example − Global and Static Variables

The following example demonstrates the default values of global, static, and local variables −

#include <stdio.h>

int a;
static int b;

int main() {
   int x;
   static int y;
   int z = 28;

   printf("Global variable a : %d\n", a);
   printf("Global static variable b : %d\n", b);
   printf("Local static variable y : %d\n", y);
   printf("Initialized local variable z : %d\n", z);
   return 0;
}

The output of the above code is −

Global variable a : 0
Global static variable b : 0
Local static variable y : 0
Initialized local variable z : 28

In the above program, a and b are global variables declared outside main(). The variable y is a local static variable. All three are automatically initialized to 0 because they reside in the .bss segment. The local variable z is explicitly initialized to 28.

Note − The uninitialized local variable x is not printed here because its value is indeterminate (garbage). Accessing it is undefined behavior in C/C++ and may produce different results on different compilers.

Example − Static Variables Retain Values

Static variables also retain their value between function calls, unlike regular local variables −

#include <stdio.h>

void counter() {
   static int count = 0;
   count++;
   printf("Called %d time(s)\n", count);
}

int main() {
   counter();
   counter();
   counter();
   return 0;
}

The output of the above code is −

Called 1 time(s)
Called 2 time(s)
Called 3 time(s)

The static variable count is initialized only once (when the program starts) and retains its value across multiple calls to counter(). A regular local variable would reset to 0 on each call.

Why the Compiler Does This

There are two main reasons why global and static variables are zero-initialized −

  • Security − Without zeroing, newly allocated memory could contain leftover data from other programs, potentially exposing sensitive information.

  • Zero cost − The .bss segment only records the size of uninitialized data in the executable file (not the actual zeros). The OS zeroes the memory pages when loading the program, so it adds no overhead to the binary size.

Conclusion

Global and static variables in C/C++ are guaranteed to be initialized to zero by the language standard. They are stored in the .bss segment, which the OS zeroes at load time at no extra cost. Local variables, on the other hand, live on the stack and contain indeterminate values unless explicitly initialized.

Updated on: 2026-03-18T07:59:36+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements