Hello Everyone
Someone told me its never too late to start diving into the world of C programming. So here I am.
I am trying to find the difference between Struct and Union using the sizeof() function. I realise int maybe 2 or 4 bytes depending on the processor, char 1 byte, float 4 byte.
Code:
struct student
{
char name[30];
int num;
float fnum;
};
union teacher
{
char name[30];
int num;
float fnum;
};
int main()
{
struct student data;
union teacher details;
printf("The size of charx30 ,intx1 and floatx1 are %d,%d,%d respectively \n",sizeof(data.name),sizeof(data.num),sizeof(data.fnum));
printf("The size of struct data is %d\n",(sizeof(data)));
printf("The size of union teacher is %d\n",(sizeof(details)));
return 0;
}
Output:
The size of charx30 ,intx1 and floatx1 are 30,4,4 respectively
The size of struct data is 40
The size of union teacher is 32
my question is why is it 40 and 32 instead of 38 and 30??
30+4+4=38.
What am I missing??
Thank you for any response.