In C, if an object that has static storage duration is not initialized explicitly, then:
— if it has pointer type, it is initialized to a NULL pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these rules.
For example, following program prints:
Value of g = 0
Value of sg = 0
Value of s = 0
#include<stdio.h> int g; //g = 0, global objects have static storage duration static int gs; //gs = 0, global static objects have static storage duration int main() { static int s; //s = 0, static objects have static storage duration printf("Value of g = %d", g); printf("\nValue of gs = %d", gs); printf("\nValue of s = %d", s); getchar(); return 0; } |
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
References:
The C99 standard
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Difference between Static variables and Register variables in C
- Static Variables in C
- Initialization of static variables in C
- How are variables scoped in C - Static or Dynamic?
- Initialization of global and static variables in C
- Internal static variable vs. External static variable with Examples in C
- C++ Internals | Default Constructors | Set 1
- Templates and Default Arguments
- Default Assignment Operator and References
- Some Interesting facts about default arguments in C++
- C++ default constructor | Built-in types
- Default arguments and virtual function
- When does compiler create default and copy constructors in C++?
- C++ | Function Overloading and Default Arguments | Question 4
- C++ | Function Overloading and Default Arguments | Question 5
- C++ | Function Overloading and Default Arguments | Question 3
- Does C++ compiler create default constructor when we write our own?
- C++ | Function Overloading and Default Arguments | Question 2
- C++ | Function Overloading and Default Arguments | Question 5
- C Program to find IP Address, Subnet Mask & Default Gateway

