This link states that "When an automatic array or structure has a partial initializer, the remainder is initialized to 0". I decided to try out what I read and wrote the following piece of code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
//int arr[3] = {2}; // line no. 7
struct s {
int si;
int sj;
};
struct s myStruct;
myStruct.si = 9;
printf("%d\n", myStruct.sj);
}
I don't understand why 4096
(which I believe is some "garbage" value) is printed when I comment out line no. 7
and I get 0
when I uncomment line no. 7
. I don't think the arr
declaration has got something to do with main()
's activation record (or rather myStruct
) which should look like (provided we have line no. 7
uncommented):
---------------
| Saved PC |
---------------
| arr[2] |
---------------
| arr[1] |
---------------
| arr[0] |
---------------
| si |
---------------
| sj |
---------------
Can somebody please explain what I am missing here?