Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C/C++ Create an enum with negative values, without having to number it

Tags:

c++

c

enums

For example in C/C++, I would have the code:

typedef enum fruits{
   apple,
   banana,
   lemon,
   orange
} fruit_t;

Which would be equivalent to:

typedef enum fruits{
   apple = 0,
   banana = 1,
   lemon = 2,
   orange = 3
} fruit_t;

However, I would like the values to be negative, so they do not conflict with anything else. I could do it like this:

typedef enum fruits{
   apple = -1,
   banana = -2,
   lemon = -3,
   orange = -4
} fruit_t;

But if I would like to add another fruit, I have to assign another value, and if I put one inbetween, I have to renumber most of it. Is there an easier way of doing this?

like image 614
Joe Avatar asked Jan 09 '14 17:01

Joe


People also ask

Can enum have negative values in C?

Each entity is called an Enum. An Enum has a Label string. An Enum has a Value. (The Value can be positive or negative Integer.)

Should enums be numbers?

Enums are always assigned numeric values when they are stored. The first value always takes the numeric value of 0, while the other values in the enum are incremented by 1.

Is enum always integer?

In ANSI C, the expressions that define the value of an enumerator constant always have int type. That means the storage associated with an enumeration variable is the storage required for a single int value.

Can we add constants to enum without breaking existing code?

4) Adding new constants on Enum in Java is easy and you can add new constants without breaking the existing code.


3 Answers

Start with INT_MIN on top like this:

#include <limits.h>

enum fruits
{
  orange = INT_MIN,
  lemon, 
  ...
}

Per "5.2.4.2.1 Sizes of integer types " and/or "Annex E/1" of the C11 Standard INT_MIN is at least -32767.

INT_MIN gets defined by including <limits.h>.

like image 138
alk Avatar answered Oct 16 '22 19:10

alk


In C you only need to number the top one and subsequent entries will be the previous entry +1, this is covered in the C99 draft standard section 6.7.2.2 Enumeration specifiers which says (emphasis mine):

[...]An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =, the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant.[...]

and the wording is similar in the C++ draft standard section 7.2 Enumeration declarations paragraph 2.

So just do something similar to the following:

#define MIN -4

typedef enum fruits{
   orange = MIN,
   lemon,  
   banana,
   apple,
} fruit_t;
like image 45
Shafik Yaghmour Avatar answered Oct 16 '22 20:10

Shafik Yaghmour


First of all, there is no such thing as "C/C++", those are two different languages. You can't look at C as a subset of C++; some legal C code won't compile as a C++ program.

If you can use C++11, I suggest you use enum class, which gives you strongly typed enumerations:

enum class Traffic {red , yellow, green};
Traffic t = Traffic::red;

if ( t == Traffic::red )  // to test the value of an enum 

This way you can guarantee no clashing, because the compiler won't let you do any comparison with an integer or with a variable of a different enum type.

You can only compare against a variable of the same enum type, or use the binary scope resolution operator as in the example.

Another advantage of this enum class is that you can set the size of your enum. You can use any signed or unsigned integer type like this:

enum class Traffic : char { red, yellow, green }

When a type is not specified, the default int is assumed.

Note that you need a compiler with C++11 support for this.

like image 28
Moha the almighty camel Avatar answered Oct 16 '22 20:10

Moha the almighty camel