Sizeof (enum_type)

Source: Internet
Author: User
Met during the interview. The other is how to prevent repeated definitions in header files.

# Ifndef

# Def

# Enddef

Wild pointer, free and then null.



Sizeof (enum_type) C ++ standard p692:

In C ++, the type of an enumerator is its enumeration. In C, Tye type of an enumerator is int.

Enum E {};

Sizeof (A) = sizeof (INT) // in C

Sizeof (A) = sizeof (e) // in C ++

/* And sizeof (INT) is not nessary equal to sizeof (e )*/

So how to calculate the sizeof of enumerator?
The C ++ programming language 3rd edition 4.8
The range of an enumeration holds all the enumeration's enumerator values rounded up to the nearest larger binary power minus 1. the range goes down to 0 if the smallest enumerator is nonnegative and to the nearest lesser negative binary power if the smallest enumerator is negative. this defines the smallest bitfield capable of holding the enumerator values. for example:

Enum E1 {dark, light}; // range 0:1

Enum E2 {A = 3, B = 9}; // range 0: 15

Enum E3 {min = 10, max = 1000000}; // range 1048576: 1048575

the sizeof an enumeration is the sizeof some integral type that can hold its range and not larger than sizeof (INT ), unless an enumerator cannot be represented as an int or as an unsigned Int. for example, sizeof (E1) cocould be 1 or maybe 4 but not 8 on a machine where sizeof (INT) = 4.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.