By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,600 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

when i prints struct type variable it prints more byte then it has data type bytes.

P: 1
when I declare
struct student{
int rollno; //it reserve 4 bytes
char name[25]; // it reserve 25 bytes
float marks; // it reserve 4 bytes
// total is 33byte
};

struct student s1; //it reserve 33 bytes
printf("%d",sizeof(s1));
// it prints 36.
// why prints 36 intead 33 because its total bytes is 33..
Feb 15 '17 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Yes, that can happen. When the compiler sees the struct, it tries to align the members on integer boundaries to simplify locating the members in memory.

I this case the int is on an int boundary and you reserve 4 bytes. The array takes 25 bytes so it extends into the next int area by one byte. The compiler then adds 3 pad bytes so the float starts on the next int boundary.
Feb 15 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.