If struct a a1 = {0}; initializes all elements (of different types) of the structure to zero, then struct a a2 = {5}; should initialize it to 5 .. no?
#include <stdio.h> typedef struct _a { int i; int j; int k; }a; int main(void) { a a0; a a1 = {0}; a a2 = {5}; printf("a0.i = %d \n", a0.i); printf("a0.j = %d \n", a0.j); printf("a0.k = %d \n", a0.k); printf("a1.i = %d \n", a1.i); printf("a1.j = %d \n", a1.j); printf("a1.k = %d \n", a1.k); printf("a2.i = %d \n", a2.i); printf("a2.j = %d \n", a2.j); printf("a2.k = %d \n", a2.k); return 0; }
An uninitialized structure contains garbage values
a0.i = 134513937 a0.j = 134513456 a0.k = 0
Initialized structure 0 contains all elements initialized to 0
a1.i = 0 a1.j = 0 a1.k = 0
The initialized structure 5 contains only the first element initialized to 5 , and the remaining elements are initialized to 0 .
a2.i = 5 a2.j = 0 a2.k = 0
If a2.j and a2.k will always be initialized to 0 during a a2 = {5}; (or), this is undefined behavior
OTOH, why I do not see all s2 elements initialized to 5 . How is struct initialization performed during {0} and how does it differ when using {5} ?
source share