Why are structure names different from their typedef names?

In Windows header files, many structures are declared like this:

typedef struct tagXYZ {
} XYZ;

typedef struct _XYZ {
} XYZ;

/* there are other variations, too */

Why is the structure name different from typedef name?

This is a holdover from very early versions of the C language where structure tags, union tags, and typedefs were kept in the same namespace. Consequently, you couldn't say typedef struct XYZ { ... } XYZ;. At the open brace, the compiler registers XYZ as a structure tag name, and then when XYZ appears a second time, you get a redeclaration error. The standard workaround for this was to make the structure tag name a minor modification of the typedef name, most typically by putting the word tag in front.

The C language standardization process separated the structure and typename name spaces, so this workaround is no longer necessary, but it doesn't hurt either. Besides, even if new structures followed the typedef struct XYZ { ... } XYZ; pattern, you would just have people asking, "Why do some structures in winuser.h use the tagXYZ pattern and others use the XYZ pattern? Why can't it just be consistent?"

Next time, why you also don't see the pattern typedef struct { ... } XYZ very much either.

Comments (25)
  1. Coleman says:


    I always thought this had more to do with name mangling in C++ than C namespaces.  

    Thanks for teaching me something new!

  2. Nathan_works says:

    This may sound like a dumb question, and the answer probably exists on the web, though I don’t know the right way to phrase it as such..

    Which came first, windows or MSVC (or whatever the first microsoft compiler was that contained all the windows libraries and headers) ?

    I’m wondering if MS had a C compiler on hand to develop windows, or if an alternate compiler was used to develop and compile windows, and later MS developed their own compiler.

    I recall that win95 and MSVC 6 were released in roughly the same time frame (as in, I had a "wow, neat, a windows 95 box!" and MSVC 6 in the same first job… )

  3. @Nathan_works: In the past, Microsoft didn’t have a compiler that was hosted on Windows. Quick C for Windows and Visual C++ 1.0 were the first.

    Before that, you could use Microsoft C 6.0 to target DOS, Windows or OS/2, if I recall correctly.

  4. Luciano says:

    If i remember correctly (it was many years ago) the first C compiler from microsoft was produced by Lattice…

  5. Dave says:

    <blockquote>Next time, why you also don’t see the pattern typedef struct { … } XYZ very much either.</blockquote>

    I’m guessing this has to do with compiler error messages.

  6. <i>Next time, why you also don’t see the pattern typedef struct { … } XYZ very much either.</i>

    That seems trivial: you can’t then have pointers to the same type of struct within the struct.

  7. ScottD says:

    "Next time, why you also don’t see the pattern typedef struct { … } XYZ very much either."

    My guess is because you can’t forward-declare a struct which has been defined that way. IIRC, it’s technically a typedef to an unnamed struct.

  8. James Curran says:

    Next time, why you also don’t see the pattern typedef struct { … } XYZ very much either."

    Mostly, because I don’t believe anonomous struct have ever been legal C syntax (like a widely implemented compiler extension)

    The more basic question is "why do we need the pattern typedef struct XYX { … } XYZ; at all" (Answer, so we can refer to "struct XYZ" as just "XYZ" is a C program — The designers of C++ wisely put that ability right into the language without needing the typedef"

  9. dave says:

    "Mostly, because I don’t believe anonomous struct have ever been legal C syntax (like a widely implemented compiler extension)"

    Anonymous struct types (structs without a tag naming them: `struct {int a, double b} foo={42,3.14};’) have been legal since at least 1989 when ANSI standardized the language; I don’t have a K&R1 handy to check, but I suspect it goes even farther back than that.

    You may be thinking of anonymous struct members of another struct: ‘struct foo { struct {int a; double b;}; char *c;};’.  The common extension is to allow access to the members of the anonymous inner struct as ‘x.a’ (instead of x.inner.a), but the construct isn’t legal C.

  10. pingpong says:


    The compiler released around Win95 timeframe was Microsoft Visual C++ 4.0. It was followed by versions 4.1, 4.2, 5.0 and finally 6.0 in 1998.

    There was no MSVC++ 3.0 AFAIR.

  11. Jay Bazuzi says:

    One of the annoyances with this use of typedefs is that VC++ debugger doesn’t know whether to use a type’s real name (“tagXYZ”) or the typedef’d name (“XYZ”).  Since a type can be typedef’d to multiple names, it makes sense for it to pick the canonical name, but as a user, that’s not what you usually want to see.

    So, why bother typdef’ing at all?  Why not just name the struct XYZ?

    [Most people would have quickly tired of typing “struct” all the time. After about the tenth time of typing struct DRAWITEMSTRUCT *pdis = (struct DRAWITEMSTRUCT)lParam; you’re going to say “Why can’t we create a typedef for this structure?” -Raymond]
  12. VisualC++ x.0 says:

    > The compiler released around Win95 timeframe was Microsoft Visual C++ 4.0.

    IIRC, Win95 was published at the same time of Visual C++ 2.0 – the first true 32bit version after the hybrid 1.5. 4.0 came shortly after.

  13. pingpong says:

    According to http://www.emsps.com/oldtools/mscppv.htm, the MSVC 2.0 folders are dated 9/20/94 2:55am – that’s almost a year before Win95 release.

  14. Nathan_works says:

    Thanks for the correction on which version of MSVC was released at/around win95 time — I certainly don’t have the install media around to check ;)

    It was 32bit, as the guys before me had just ported it over. I was too green to deal with thunks and other 16 bit issues …

  15. nikita says:

    Struct and union field names used to live in the same global name-space too, and this is where tradition to prepend a unique prefix to struct fields is from. As a byproduct, fields weren’t bound to any particular struct type:


    register *rp;
    rp = p;
    if(rp-&gt;i_count == 1) {



  16. ERock says:

    This is the first time on Raymond’s blog I’ve read a technical post and have known the answer before I read it.

    I feel so smart.

    Day after tomorrow, though, I will stop knowing the answers and will feel dumb again.

  17. Ulric says:

    so this workaround is no longer necessary, but

    it doesn’t hurt either.

    It’s an annoyance, actually, because in C++ you can’t derive a class from the the typedef you’re used to see.

    In other words, you have to derive a c++ class from ‘tagPOINT’ instead of from "POINT".

    I absolutely hate the ‘tag’ stuff in Windows header, because programmers have copied it into their code in many companies I’ve been at, without knowing why.  Just like the "LP" typedefs for pointers, or capital VOID.

    The reason why there are typedef at all was that in "C" we had to write "struct POINT pt;"  when defining a variable.  The typedef saves us from having to type the word ‘struct’.  In C++, this isn’t necessary, and would be totally transparent for us if it wasn’t for that ‘tag’ prefix, which forces us to be aware that there is a type and a typedef.

  18. steveg says:

    Here’s a gem of an article about running Windows 1.0 under Virtual PC by Charles Petzold, which might make some oldies smile.


    I was looking around for a circa Win2.0 Windows.h file to look at structs there… stupid internet, it’s like the world didn’t exist before 2000.

  19. Gabe says:

    The Win1.0 SDK came with Microsoft C 3.0, which predates Microsoft VisualC by many years.

  20. IgorD says:

    Some compilers alow me to do this:

    typedef struct _a  {

       int (*some_func_ptr)(struct _b *);

    } AType;

    typedef struct _b  {

    } BType;

    other compilers require this declaration up front before everything else:

    struct _b;

    typedef struct _a  {

       int (*some_func_ptr)(struct _b *);

    } AType;

    typedef struct _b  {

    } BType;

    I have no idea what is "more" legal.

  21. SuperKoko says:


    Next time, why you also don’t see the pattern typedef struct { … } XYZ very much either.


    Short answer: Anonymous structures don’t benefit from good error/warning/debug information.

    Side note:

    I remember that Borland C supports something like typedef struct XYZ { … }; as a special syntax for typedef struct XYZ { … } XYZ;

  22. Hendrik Boom says:

    C had one namespace for struct and union tags, but typedefs were in the same namespace as variables.  C did not automatically define a typedef name when you defined a struct.  It was C++ that introduced the implicit definition of a typedef name.

    So in C people would write

     typedef struct foo { … } foo;

    and then they sould refer to the type without mentioning struct.

    But when they tried compiling this in C++, the mention of "struct foo" would define the typedef name ‘foo’, and when the compiler got around to the ‘foo’ after then ‘}’, it would complain that ‘foo’ had already been defined.

    So to use anything like this in C++ you had to change the structure tag.

    I speak from memory – I was a C user in the 70’s, and a C++ implementer in the early nineties.  C++ may hae chenged then, of course.  I haven’t been tracking it since then.

  23. Yuhong Bao says:

    I think the Windows 1.0 SDK came around the time of MS C 4.0, and the Windows 2.0 SDK came around the time of MS C 5.0. Visual C++ 2.0 was released around the time of NT 3.5 (I wonder how many people believed Win95 was the first version of Windows to run Win32 apps natively). Visual C++ 4.0 was released around the release of Win95. Visual C++ 4.2 was released around the release of NT 4.0 and contained the beta of several SDKs, including the ActiveX SDK. 4.2c was released to patch them up to released versions. Visual C++ 6.0 was released around the release of Win98, but still contained the NT 4.0 headers and libraries.

  24. Alexandre Grigoriev says:


    A scope for a structure/class name implicitly declared in the function declaration argument list is limited to the function declaration. It’s not propagated up to the enclosing scope.

    void a(class b *c); // not in the global scope

    class b;

    void a(class b *c); // same as in the global scope

    On the other hand, a scope for a structure/class name implicitly declared in a member declaration is propagated to the enclosing scope.

    class d


    class a *b;


    Here ‘class a’ belongs to the global scope, as if it were declared before class d declaration.

    But if you do like this:

    class d


    class a;

    class a *b;


    Then it’s class d::class a.

Comments are closed.

Skip to main content