Nothing applies to C++ . In C++ preferable:
struct Foo { . . . };
The rest applies only to C
Technically, struct Foo {}; enough for most purposes. However, this is a bit verbose, as struct must be repeated every time the Foo type is used.
struct Foo {} void func( struct Foo* foo );
typedef makes this easier.
struct Foo { }; typedef struct Foo Foo; void func( Foo* foo );
This can be further reduced:
typedef struct Foo { } Foo; void func( Foo* foo );
When executing a single liner, it can also use this syntax:
typedef struct { } Foo; void func( Foo* foo );
This creates an anonymous struct and then gives it the name Foo. You most often see this with enum s.
typedef enum { } Bar;
There is a reason that excess Foo usually remains. This is the only way to create a self-reference structure, such as a linked list.
typedef struct Node { Node* next; } Node;
If the starting Node , where ommited, there would be no way to declare a pointer to a structure. Technically, this is true, but now you need to come up with 2 names, not just one.
typedef struct node_ { node_* next; } Node;
So why use:
typedef struct Foo { } Foo;
For homogeneity. This declaration style covers all your bases, so you donβt need to think about it when declaring a structure.