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 273553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:28:26+00:00 2026-05-12T00:28:26+00:00

Is the sizeof(enum) == sizeof(int), always ? Or is it compiler dependent? Is it

  • 0

Is the sizeof(enum) == sizeof(int), always ?

  • Or is it compiler dependent?
  • Is it wrong to say, as compiler are optimized for word lengths (memory alignment) ie y int is the word-size on a particular compiler? Does it means that there is no processing penalty if I use enums, as they would be word aligned?
  • Is it not better if I put all the return codes in an enum, as i clearly do not worry about the values it get, only the names while checking the return types. If this is the case wont #DEFINE be better as it would save memory.

What is the usual practice?
If I have to transport these return types over a network and some processing has to be done at the other end, what would you prefer enums/#defines/ const ints.

EDIT – Just checking on net, as complier don’t symbolically link macros, how do people debug then, compare the integer value with the header file?

From Answers —I am adding this line below, as I need clarifications—

“So it is implementation-defined, and
sizeof(enum) might be equal to
sizeof(char), i.e. 1.”

  • Does it not mean that compiler checks for the range of values in enums, and then assign memory. I don’t think so, of course I don’t know. Can someone please explain me what is “might be”.
  • 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-05-12T00:28:26+00:00Added an answer on May 12, 2026 at 12:28 am

    It is compiler dependent and may differ between enums. The following are the semantics

    enum X { A, B };
    
    // A has type int
    assert(sizeof(A) == sizeof(int));
    
    // some integer type. Maybe even int. This is
    // implementation defined. 
    assert(sizeof(enum X) == sizeof(some_integer_type));
    

    Note that "some integer type" in C99 may also include extended integer types (which the implementation, however, has to document, if it provides them). The type of the enumeration is some type that can store the value of any enumerator (A and B in this case).

    I don’t think there are any penalties in using enumerations. Enumerators are integral constant expressions too (so you may use it to initialize static or file scope variables, for example), and i prefer them to macros whenever possible.

    Enumerators don’t need any runtime memory. Only when you create a variable of the enumeration type, you may use runtime memory. Just think of enumerators as compile time constants.

    I would just use a type that can store the enumerator values (i should know the rough range of values before-hand), cast to it, and send it over the network. Preferably the type should be some fixed-width one, like int32_t, so it doesn’t come to conflicts when different machines are involved. Or i would print the number, and scan it on the other side, which gets rid of some of these problems.


    Response to Edit

    Well, the compiler is not required to use any size. An easy thing to see is that the sign of the values matter – unsigned types can have significant performance boost in some calculations. The following is the behavior of GCC 4.4.0 on my box

    int main(void) {
      enum X { A = 0 };
      enum X a; // X compatible with "unsigned int"
      unsigned int *p = &a;
    }
    

    But if you assign a -1, then GCC choses to use int as the type that X is compatible with

    int main(void) {
      enum X { A = -1 };
      enum X a; // X compatible with "int"
      int *p = &a;
    }
    

    Using the option --short-enums of GCC, that makes it use the smallest type still fitting all the values.

    int main() {
      enum X { A = 0 };
      enum X a; // X compatible with "unsigned char"
      unsigned char *p = &a;
    }
    

    In recent versions of GCC, the compiler flag has changed to -fshort-enums. On some targets, the default type is unsigned int. You can check the answer here.

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

Sidebar

Ask A Question

Stats

  • Questions 188k
  • Answers 188k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If a method is expecting a BSTR passing a BSTR… May 12, 2026 at 5:34 pm
  • Editorial Team
    Editorial Team added an answer You may have a look on Pex - it allows… May 12, 2026 at 5:34 pm
  • Editorial Team
    Editorial Team added an answer More information would make it easier to figure out. I… May 12, 2026 at 5:34 pm

Related Questions

I have this generic string to number conversion : enum STRING_BASE : signed int
We use a simple object model for our low level networking code at work
Is it possible to define a C/C++ macro BUILD(a, i) which expands to x[0],
I'm writing a unit test for a method that packs boolean values into a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.