In C, most of the code declaring structs will follow this pattern:
/* struct forward-declaration */
typedef struct T T ;
/* struct definition */
typedef struct T
{
/* etc. */
} T ;
This is so prevalent most developers I talked with didn't even know the code above did two things at the same time (struct declaration, then aliasing the struct name in the normal namespace), and just wrote it out of habit.
In C++, the issue is mitigated so you can omit the typedefing part. In C# and in Java, the designers didn't even bother. So those languages won't help understand why C does this that way.
So, after Oliver Charlesworth's suggestion:
Is there a technical reason to have struct T
in a separate namespace from other normal identifiers?
Edit
The relevant section in the C89/C90 standard is:
6.1.2.3 Name spaces of identifiers
It more than one declaration of a particular identifier is visible at any point in a translation unit. the syntactic context disambiguates uses that refer to different entities. Thus. there are separate name spaces for various categories of identifiers, as follows:
[...]
the tags of structure, unions and enumerations (disambiguated by following any of the keywords struct, union, or enum).
[...]
all other identifiers. called ordinary identifiers (declared in ordinary declarators or as enumeration constants).
The text for C11 (n1570:6.2.3 standard draft) is more or less the same.
typedef
change? – Thanetstruct T
in a separate namespace fromT
"? – Coffeecoloredtypedef struct foo foo;
) would be simply illegal (asfoo
is redefined in the same scope). – Upturned