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

  • SEARCH
  • Home
  • 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 7816371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:50:38+00:00 2026-06-02T05:50:38+00:00

In both c and c++ enum could be defined with tag enum e_smth {

  • 0

In both c and c++ enum could be defined with tag

enum e_smth {
    smth_one,
    smth_two,
    smth_err
};

or without tag

enum {
    smth_one,
    smth_two,
    smth_err
};

If it was defined with tag it makes sense in switch statements in both c and c++ :

e_smth some_var;
....//do some stuff with some_var
switch (some_var)
{
case smth_one:
break;
case smth_two:
break;
}

will produce -Wswitch warning if this will be compiled with gcc or g++.

It makes sense in functions declarations and variables initialization in c++ :

e_smth var;
var=99;

Will produce -fpermissive error if compiled with g++.

Both types with or without tag could be used as one file #define macro without parameters.


update

could be used as one file #define macro without parameters

meant : Instead of writing #define MAX 1000 in file and adding MAX to global use enum { MAX=1000 } just in file scope


And what about anonymous enums, I’ve found just one use cases:
definition like typedef enum { a,b,c } some_t; makes it work like enum with tag

Question:

if I haven’t yet described all reasonable use cases, what for anonymous enums should be used?

  • 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. Editorial Team
    Editorial Team
    2026-06-02T05:50:39+00:00Added an answer on June 2, 2026 at 5:50 am

    In C (but not in C++), enum can be [ab]used to define int constants.

    For example, given this declaration:

    const int MAX = 1024;
    

    MAX is not a constant expression, it’s the name of a read-only object. That means you can’t use it in a case label, as the size of an array declared at file scope or with static, or in any other context requiring a constant expression.

    But if you write:

    enum { MAX = 1024 };
    

    then MAX is a constant expression of type int, usable in any context where you could use the constant 1024.

    Of course you could also write:

    #define MAX 1024
    

    but there are disadvantages to using the preprocessor: the identifier isn’t scoped the way it would be given an ordinary declaration, for example.

    The drawback is that such a constant can only be of type int.

    C++ has different rules; enumeration constants are of the enumerated type, not int, but you can use declared constant objects as constant expressions (as long as the initializer is a constant expression).

    To address the original question, when you use an enum declaration to create constants like this, there’s no point in having either a tag or a typedef, since you’ll never use the type itself.

    Background: This:

    enum foo { zero, one, two };
    enum foo obj = two;
    

    creates a type enum foo and constants zero, one, and two. In C, the constants are always of type int, which is admittedly odd, and the initialization of obj involves an implicit conversion from int to enum foo.

    In C++, the type enum foo can also be referred to as just foo, and the constants are of type enum foo (which is compatible with some integer type, not necessarily int).

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

Sidebar

Related Questions

Both in Actionscript3 and Javascript these statements give the same result: /\S/.test(null) => true
Let's say I need both an enum both in a flags and in a
I have an enum type defined: EnumType Now imagine object A = EnumType.Value1; object
I'm trying to sort order by both timestamp and enum (main order timestamp, sub-order
The CheckBox control exposes both boolean Checked and System.Windows.Forms.CheckState enum CheckState properties, which allow
Is there a way I could point an Enum to another Enum? Here are
I have a C# file A which defined some public enum and public struct.
I have an enum Pitch and a class Pitch, both naturally to me should
I have this macro code, which allows me to define both a C enum
Both Ruby and Python have the ability of calling the debugger from code (

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.