why the output of this code is 4 why not 5 or 8. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

why the output of this code is 4 why not 5 or 8.

#include<iostream> using namespace std; class A { int a; static int b; }; int A::b; int main() { cout<<sizeof(A); return 0; }

9th Sep 2019, 4:22 AM
Aadarsh Gupta
Aadarsh Gupta - avatar
4 Answers
+ 3
~ swim ~ So the `sizeof` only returns summary of size of instance members (excludes class member) when it comes to objects?
9th Sep 2019, 6:28 AM
Ipang
+ 1
Because int is 4 bytes
9th Sep 2019, 4:56 AM
KfirWe
KfirWe - avatar
+ 1
but about static int b
9th Sep 2019, 5:02 AM
Aadarsh Gupta
Aadarsh Gupta - avatar
+ 1
Aadarsh Gupta The static int variable also occupies 4 bytes.
9th Sep 2019, 5:36 AM
KfirWe
KfirWe - avatar