Today, i was a little bit surprised about the behavior of c structure vs c++ structure.
fun.cpp: http://ideone.com/5VLPC
struct nod
{
static int i;
};
int main()
{
return 0;
}
The above program works perfectly.
BUT,
When the same program is run in C environment, it is giving the error:
prog.c:3: error: expected specifier-qualifier-list before ‘static’
see here: http://ideone.com/2JRlF
Why it is so?
Because in C++, structs are just classes with default visibility of
public. So in C, the struct is only a aggregation of data, which does not know anything about the fact that it could be percieved as standalone type.See also What are the differences between struct and class in C++