C++ - Size of a struct | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

C++ - Size of a struct

Why is output 12? #include <iostream> using namespace std; struct student{ char a; int c; char b; }; int main() { cout << sizeof(student); return 0; }

11th Sep 2019, 11:12 AM
Paolo De Nictolis
Paolo De Nictolis - avatar
3 Answers
+ 2
Very clear, thank you!
11th Sep 2019, 12:47 PM
Paolo De Nictolis
Paolo De Nictolis - avatar
0
Because char is considered to be 4 bytes in a struct. it is 1 byte but 3 bytes are added to satisfy alignment constraints.
11th Sep 2019, 12:53 PM
Dariush Shiri
Dariush Shiri - avatar