for example consider:
struct strct
{
data member_1;
data member_2;
......
};
when does the compiler recognize
struct strct
as a data type? Is it after executing the line
struct strct
? Or after encountering the closing brace of the structure definition?
Declarations aren't "executed".
After reading
struct strct {
the compiler recognizes struct strct
as an incomplete type, which is a type it doesn't know the size of. As you can use pointers to incomplete types, this enables you to write something like this:
struct strct {
struct strct *next; // <- allowed, a pointer doesn't need the size of the object pointed to
int foo;
};
Once the "body" of the struct declaration is finished, struct strct
is a complete type, so you can declare variables of that type (the size must be known for that).
Side note: you could actually stop after the tag with your declaration like this:
struct strct;
and, as a consequence, have the compiler know an incomplete type struct strct
. This is also called a forward declaration. Of course, it only makes sense when you have the complete declaration somewhere (possibly private in a module) as well. This is used for information hiding when implementing OOP code in C. You'd for example just declare something like this publicly:
struct strct;
struct strct *strct_create(void);
strct_foo(struct strct *self);
strct_bar(struct strct *self, int x);
[...]
and have the full declaration of struct strct
in the file implementing these functions