Friday, 25 March 2016

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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...