Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 116225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:08:39+00:00 2026-05-11T03:08:39+00:00

I encountered some code reading typedef enum eEnum { c1, c2 } tagEnum; typedef

  • 0

I encountered some code reading

typedef enum eEnum { c1, c2 } tagEnum;  typedef struct { int i; double d; } tagMyStruct; 

I heard rumours that these constructs date from C. In C++ you can easily write

enum eEnum { c1, c2 }; struct MyStruct { int i; double d; }; 

Is that true? When do you need the first variant?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:08:40+00:00Added an answer on May 11, 2026 at 3:08 am

    First, both declarations are legal in both C and C++. However, in C, they have slightly different semantics. (In particular, the way you refer to the struct later varies).

    The key concept to understand is that in C, structs exist in a separate namespace. All built-in types, as well as typedefs exist in the ‘default’ namespace. That is, when I type int, the compiler only checks this ‘default’ namespace. If I type ‘tagMyStruct’ as in your example, the compiler also only checks this one namespace. But depending which type of declaration you use, the struct may not exist in that namespace.

    Structs are different, and exist in a separate namespace. So if I make the following declaration:

    struct mystruct {}; 

    I can not simply refer to it as mystruct. Instead, I have to specify that I want the mystruct which exists in the struct namespace:

    void foo(struct mystruct bar); // Declare a function which takes a mystruct as its parameter 

    Which gets a bit verbose and awkward in the long run. Instead, you can typedef it into the default namespace:

    typedef struct mystruct mystruct; // From now on, 'mystruct' in the normal namespace is an alias for 'mystruct' in the struct namespace 

    and now, my function can be declared in the straightforward way:

    void foo(mystruct bar); 

    So your first example simply merges these two steps together: Declare a struct, and put an alias into the regular namespace. And of course, since we’re typedeffing it anyway, we don’t need the ‘original’ name, so we can make the struct anonymous. So after your declaration

    typedef struct { int i; double d; } tagMyStruct; 

    we have a struct with no name, which has been typedef’ed to ‘tagMyStruct’ in the default namespace.

    That’s how C treats it. Both types of declarations are valid, but one does not create the alias in the ‘default’ namespace, so you have to use the struct keyword every time you refer to the type.

    In C++, the separate struct namespace doesn’t exist, so they mean the same thing. (but the shorter version is preferred).

    Edit Just to be clear, no, C does not have namespaces. Not in the usual sense. C simply places identifiers into one of two predefined namespaces. The names of structs (and enums, as I recall) are placed in one, and all other identifiers in another. Technically, these are namespaces because they are separate ‘containers’ in which names are placed to avoid conflicts, but they are certainly not namespaces in the C++/C# sense.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.