0

Can anyone explain why it gives answer 6?

what will be the output of following C code? 1. struct abc 2. { 3. int b=6; 4. char c; 5. } 6. structure; 7. int main() 8. { 9. int i=sizeof(structure); 10. printf(“%d”,i); 11. }

6th Sep 2021, 3:31 PM
Shubham Maske
Shubham Maske - avatar
3 Réponses
+ 3
Your code can't compile anyway, I see there initialization of field b. But, in language C, structures is only data. Initializations need call some function, like constructors, its possible by default in C++, not C. WTF? Or I have mistake...
6th Sep 2021, 5:03 PM
Ом8рчо7сощччош
Ом8рчо7сощччош - avatar
+ 3
It must be 5. Cause size of an integer variable is 4 and size of a char variable is 1. Sum of these variable's sizes is 5. It's contradiction!
6th Sep 2021, 8:11 PM
mesarthim
mesarthim - avatar
6th Sep 2021, 4:57 PM
Ом8рчо7сощччош
Ом8рчо7сощччош - avatar