
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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 in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code. These variables are allocated in .bss file and at the time of loading it allocates the memory by getting the constants alloted to the variables.
The following is an example of global and static variables.
Example
#include <stdio.h> int a; static int b; int main() { int x; static int y; int z = 28; printf("The default value of global variable a : %d", a); printf("\nThe default value of global static variable b : %d", b); printf("\nThe default value of local variable x : %d", x); printf("\nThe default value of local static variable y : %d", y); printf("\nThe value of local variable z : %d", z); return 0; }
Output
The default value of global variable a : 0 The default value of global static variable b : 0 The default value of local variable x : 0 The default value of local static variable y : 0 The value of local variable z : 28
In the above program, global variables are declared outside the main() function and one of them is static variable. Three local variables are declared and variable z is initialized too.
int a; static int b; …. int x; static int y; int z = 28;
Their default values are printed.
printf("The default value of global variable a : %d", a); printf("\nThe default value of global static variable b : %d", b); printf("\nThe default value of local variable x : %d", x); printf("\nThe default value of local static variable y : %d", y); printf("\nThe value of local variable z : %d", z);
- Related Articles
- How are C++ Local and Global variables initialized by default?
- Default values of static variables in C
- Do static variables get initialized in a default constructor in java?
- Interface variables are static and final by default in Java, Why?
- Initialization of global and static variables in C
- Why are global variables bad in C/C++?
- When do function-level static variables get initialized in C/C++?
- C++ static member variables and their initialization
- When are static C++ class members initialized?
- What are local variables and global variables in C++?
- What are global variables in C++?
- How and why to avoid global variables in JavaScript?
- Why we do not have global variables in C#?
- Why should we avoid using global variables in C/C++?
- Global and Local Variables in C#

Advertisements