-
size of structure
one int variable occupy 4 byte and one char variable occupy 1 byte. In my code I store one int and char variable using structure
Code:
#include <stdio.h>
struct S
{
int x;
char y;
}s1;
int main()
{
s1.x = 20;
s1.y ='A';
printf("Size of Structure : %d byte \n", sizeof(struct S));
printf("Size of s1 : %d byte \n", sizeof(s1));
printf("Size of s1.x : %d byte\n", sizeof(s1.x));
printf("Size of s1.x : %d byte \n", sizeof(s1.y));
return 0;
}
Size of Structure : 8 byte
Size of s1 : 8 byte
Size of s1.x : 4 byte
Size of s1.x : 1 byte
How much memory does my code occupy ?
Does structure code occupy 8 byte ?
I think code should be occupy 5 bytes only 4 byte for int and 1 byte for char total five bytes
-
> Does structure code occupy 8 byte ?
Yes.
> I think code should be occupy 5 bytes only 4 byte for int and 1 byte for char total five bytes
Reality wins, it's 8 bytes.
Read this for a better understanding.
Data structure alignment - Wikipedia
-
I found the following link to be useful for my own understanding of how structs are arranged in memory: Struct declaration - cppreference.com