I have been working with structures in C. I had written a code to find the size of structure. It goes like this.
#include<stdio.h>
struct a
{
int b;
char c;
};
int main()
{
printf("%d\n",sizeof(struct a));
return 0;
}
Here I expected output to be 5. Because structure size is the sum of sizes of all variables in it and in the program integer takes 4 and character takes 1. But after compiling the program I got output like 8. I am thinking why the output is this and in the thought of the may be different compilers act differently so tried in different compilers. But still the output is same. Then another thought like is compiler may consider char as 4 by converting it into ASCII value. So tried with only keeping the single character in structure like this.
#include<stdio.h>
struct a
{
char c;
};
int main()
{
printf("%d\n",sizeof(struct a));
return 0;
}
Then the output is expected and matched which is 1. Can anyone please explain me my char is considered 4 there and why 1 here?