Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Signedness of enum in C/C99/C++/C++x/GNU C/GNU C99

Tags:

c++

c

enums

signed

c99

Is the enum type signed or unsigned? Does the signedness of enums differ between: C/C99/ANSI C/C++/C++x/GNU C/ GNU C99?

Thanks

like image 207
osgx Avatar asked Apr 05 '10 15:04

osgx


People also ask

Are C enums signed or unsigned?

Re: In C, enum types may be unsigned, but enum constants may not.

What is the point of enums in C?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

What is enum C __?

In C++ programming, enum or enumeration is a data type consisting of named values like elements, members, etc., that represent integral constants. It provides a way to define and group integral constants. It also makes the code easy to maintain and less complex.

Is enum a class in C?

C enums. In this tutorial, you will learn about enum (enumeration) in C programming with the help of examples. In C programming, an enumeration type (also called enum) is a data type that consists of integral constants. To define enums, the enum keyword is used.


1 Answers

An enum is guaranteed to be represented by an integer, but the actual type (and its signedness) is implementation-dependent.

You can force an enumeration to be represented by a signed type by giving one of the enumerators a negative value:

enum SignedEnum { a = -1 }; 

In C++0x, the underlying type of an enumeration can be explicitly specified:

enum ShortEnum : short { a }; 

(C++0x also adds support for scoped enumerations)

For completeness, I'll add that in The C Programming Language, 2nd ed., enumerators are specified as having type int (p. 215). K&R is not the C standard, so that's not normative for ISO C compilers, but it does predate the ISO C standard, so it's at least interesting from a historical standpoint.

like image 192
James McNellis Avatar answered Sep 17 '22 13:09

James McNellis