A character array is defined globally and a structure with same name is defined within a function. Why sizeof operator returns different values for c & c++ ?
char S[13];
void fun()
{
struct S
{
int v;
};
int v1 = sizeof(S);
}
// returns 4 in C++ and 13 in C
Because in C++, the
structyou defined is namedS, while in C, it’s namedstruct S(which is why you often seetypedef structused in C code). If you were to change the code to the following, you would get the expected results: