AD1

Thursday, 2 March 2017

what is the output

what is the output of this C code 
#include <stdio.h>
union temp
{
int a;
char c;
};
int main()
{
union temp t={66,'A'};// initialize the union members
printf("%d\t%c\n",t.a,t.c);
printf("%d\n",sizeof(t));
return 0;
}
view raw union.c hosted with ❤ by GitHub
output:
66    B
4
Explanation:
union temp t={66,'A'};
this statement will initialize only the first member of the union x with 66 and the compiler will ignore the second value 'A', as one element is sufficient to initialize all union members as they share the same memory locations
sizeof(t) =4 size of largest element in memory location



No comments:

Post a Comment