Tuesday, August 21, 2018

c - Is it a definition or a declaration?




struct foo {
char name[10];
char title[10];
int salary;
};


In the above code is it a structure definition or structure declaration ?




I'm learning structures in C, some books says that it is a declaration, some says it is a definition. So what exactly it is ?



From what I understand a declaration specifies the compiler what the type and name of a variable is, where as a definition causes memory space allocated for the variable.


Answer



It's a declaration. It declares the type struct foo.




(C99, 6.7p5) "A declaration specifies the interpretation and attributes of a set of identifiers. A definition
of an identifier is a declaration for that identifier that:




— for an object, causes storage to be reserved for that object;



— for a function, includes the function body;101)



— for an enumeration constant or typedef name, is the (only) declaration of the
identifier."



No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...