1

I didn't find anything online about what happens when objects are created in C: like their value are initialized or they take garbage value.

#include <stdio.h> 
struct temp 
{ 
    int a; 
} s;

int main() 
{ 
    printf("%d", s.a); 
}

OUTPUT is : 0.

So is 0 a garbage value?? Or is it an undefined behavior?

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • Does this answer your question? [Are the members of a global structure initialized to zero by default in C?](https://stackoverflow.com/questions/4080673/are-the-members-of-a-global-structs-in-c-initialized-to-zero) – RobertS supports Monica Cellio Apr 18 '20 at 07:50
  • 1
    Also: *"I didn't find anything online about what happens when objects are created in C: like their value are initialized or they take garbage value."* -> https://en.cppreference.com/w/c/language/storage_duration – RobertS supports Monica Cellio Apr 18 '20 at 07:55

2 Answers2

5

Since globals and static structures have static storage duration, the answer is yes - they are zero initialized (pointers in the structure will be set to the NULL pointer value, which is usually zero bits, but strictly speaking doesn't need to be).

You have used a global structure variable. Therefore initialised to default value, i.e., 0.

Deepak Tatyaji Ahire
  • 4,883
  • 2
  • 13
  • 35
0

It depends on the place where the variable is declared. The structure variable s is declared as global. So it initialized to zero by default.

More in this link

Karthick
  • 1,010
  • 1
  • 8
  • 24