1
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.

Maroun
  • 94,125
  • 30
  • 188
  • 241
curious_kid
  • 379
  • 1
  • 4
  • 8

2 Answers2

4

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."

ouah
  • 142,963
  • 15
  • 272
  • 331
3

Your understanding is correct. Your code example is a declaration of a type. In 'C' you can declare a type and immediately use it to define a variable.

So your example is a pure declaration.

And here is an example of declaration+variable definition:

struct foo {
     char name[10];
     char title[10];
     int  salary;
} var;
Alexey Polonsky
  • 1,141
  • 11
  • 12