answersLogoWhite

0

What is enum in c?

Updated: 8/9/2023
User Avatar

Wiki User

13y ago

Best Answer

Enumerations are a method of grouping constant values. For example:

enum suits { clubs, diamonds, spades, hearts };

By default, the first constant is assigned the value 0 and all subsequent values increment by 1. However, you can assign any value to any constant -- the automatic increments will continue from that point.

enum suits { clubs = 1, diamonds, spades, hearts };

You can also assign the same value to multiple constants.

enum suits { clubs = 1, diamonds, spades = 1, hearts };

By grouping constants within enumerations your code becomes more secure because you cannot pass constant literals into functions that expect an enumeration. Consider the following:

void print_suit(unsigned id)

{

switch (id)

{

case (0): std:cout << "Clubs"; break;

case (1): std:cout << "Diamonds"; break;

case (2): std:cout << "Spades"; break;

case (3): std:cout << "Hearts"; break;

default: std::cout << "Invalid";

}

}

In the above example there is nothing to prevent the caller from passing an invalid value, such as 42, which the function caters for with a default case. However, by passing an enum instead, invalid values are completely eliminated:

void print_suit(suits suit)

{

switch (suit)

{

case (clubs): std:cout << "Clubs"; break;

case (diamonds): std:cout << "Diamonds"; break;

case (spades): std:cout << "Spades"; break;

case (hearts): std:cout << "Hearts";

}

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Enumerated values/variables are used when a variable cannot be expressed in a native way, i.e. the variable is not intrinsically int, float, double, etc.

For example, the day of the week really is the day of the week, not an int. A color is a color, not an int. So, you would express these are enums (enumerated) values:

enum { red, green, blue} ;

Using enumerations in this context also helps support developers manage changes to the code. enums are one of the mechanisms to replace "magic numbers" in code:

if (color == 1 /*red*/)
{
// do something

}

can be changed to:

if (color == red)
{
// do something

}

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

An enumeration is an user-defined type that encapsulates one or more constants, each of which is assigned a value. Unlike ordinary constants which have an explicit type (such as int, char or float), enumerated constants are a type in their own right, such that the enumeration identifier represents the type, while the constant identifiers are the members of that type.

Unless otherwise specified, the first member of an enumerated type is automatically and implicitly assigned the value 0. All other members that are not explicitly assigned a value are implicitly assigned the previous member's value plus 1. Member values need not be unique -- two or more members may share the same value.

Although enumerated members are an user-defined type in their own right, they do have an underlying integral type, which is int (by default). That is, when performing arithmetic with two or more enumerated members, the evaluation returns an int, not the enumerated type. You may also explicitly specify the underlying type, but it must be an integral type (char, short, long or long long). You cannot enumerate constant strings nor constant doubles.

You can use enumerated types wherever you would normally use a constant integral type. However, unlike constants which may be assigned any value, an enumerated type can only be assigned the value of an enumerated member. It is this property alone that makes an enumerated type so useful.

By way of an example, let's consider the following code snippet that makes use of some constant values:

const unsigned int black=0;

const unsigned int green=1;

const unsigned int blue=2;

const unsigned int red=3;

const unsigned int white=4;

// ...

void print_colour(const unsigned int colour)

{

using std::cout;

using std::endl;

switch(colour)

{

case(black): cout<<"Black"<<endl;

case(green): cout<<"Green"<<endl;

case(blue): cout<<"Blue"<<endl;

case(red): cout<<"Red"<<endl;

case(white): cout<<"White"<<endl;

}

}

The problem with this code is that there's nothing to stop us from calling print_colour(blue+white) or print_colour(42). There's nothing actually wrong with these calls -- they will compile and run just fine -- but we don't really want to allow calls such as these to creep into our code. We don't really want to catch these types of errors using runtime verifications (which would result in an unhandled exception at runtime), what we really want to do is catch them at compile time. For that we need an enumeration:

enum colour_t{black,green,blue,red,white};

// ...

void print_colour(const colour_t colour)

{

using std::cout;

using std::endl;

switch(colour)

{

case(black): cout<<"Black"<<endl;

case(green): cout<<"Green"<<endl;

case(blue): cout<<"Blue"<<endl;

case(red): cout<<"Red"<<endl;

case(white): cout<<"White"<<endl;

}

}

As you can see, we've replaced all the constants with a single enumeration statement, and changed the function signature to accept the enumerated type. Now the only valid values we can possibly pass to this function are the constants black, green, blue, red or white. Calling print_colour(blue+white) or print_colour(42) will not compile, because neither argument is a valid enumerated member of colour_t.

The enumeration does not explicitly assign values, so black is implicitly assigned the value 0, while green is implicitly black+1, blue is green+1, red is blue+1 and white is red+1 (0, 1, 2, 3 and 4 respectively).

So why does print_colour(blue+white) not work when blue and white are both members of the enumerated type? The answer is that the evaluation of blue+white does not equate to a colour_t member. What we're actually doing is implicitly calling print_colour((int)(blue+white)), which then reduces to print_colour((int)(2+4)), and thus print_colour((int)6). But the print_colour function expects a constant colour_t type, not a literal constant of type int.

Although it is possible to overload the + operator in such a way that enumerated members could be summed and thus return an enumerated type, this is rarely a good idea. For instance, the sum of green+blue is 1+2=3, which could easily be cast to a valid colour_t type (blue), but blue+white cannot be cast because we haven't actually defined any colour_t member with the value 6.

So although enumerations have limitations, this is actually a good thing. The more we can enlist the compiler to prevent us from writing bad code (whether by accident or on purpose), the more we do to ensure we write robust code.

Enumerations can be used in many ways where we would normally use a constant, but ultimately they are used to ensure our constants are always within the range of acceptable values, without requiring us to specifically write additional code to ensure that that is the case. Our code is therefore smaller and thus more efficient, because all possible errors are eliminated at compile time, rather than at runtime.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

An enumeration is a data type, used to declare variable that store list of names.

It is act like a database, which will store list of items in the variable.

example: enum shapes{triangle, rectangle, square};

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

Enumeration type allows programmer to define their own data type . Keyword enum is used as a keyword for the enumerated data types.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

The identifier is a user-defined enumerated data type which can be used to declare variables that can have one of the values enclosed within the braces (known as enumeration constants).

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

There are basically two uses 1. making new data (which does not exist in c language ) for example : Boolean typedef enum BooleanT { False; True; }boolean; 2.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

In C++, an enumeration (enum) is a group of constants. In C++11 an enumeration is a class of constants.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

// for definition of enumerated type:

enum [tag] [: type] {enum-list} [declarator];

// for declaration of variable of type tag:

enum tag declarator;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is enum in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is meant by enum in C programming?

The enum keyword means enumeration.


How do you pass enum object as argument to function in c?

You can't pass an enum as an argument to a function. An enum in C isn't an object, it's a type. All you can do is pass a variable that is of the particular enum's type.


What is difference between C enum and C plus plus enum?

In C++, enum signifies a slightly stronger type than in C. For example, in C, one could write: enum Direction { UP, DOWN }; Direction d = 1; In C++, this would be illegal, only UP or DOWN can be assigned to a variable of type Direction, though there is still implicit casting to integer, so one could still write: int i = UP; Another difference has to do with the way variable are declared in general in C++. In C, once the enum was declared as above, declaring variables of type Direction would have to be done through the enum keyword, like this: enum Direction d = UP; In C++, the name "Direction" becomes a type in itself, so you can write: Direction d = UP;


In c plus plus you cannot assign integer value to enum?

That is correct - In c plus plus you cannot assign integer value to enum - You can only assign an enum value to an enum. Even though an enum looks like an integer, it is not. It is an enum, and C++ implements strict type checking to reduce the probability of bad programming practices. enum ColorCode {black, brown, red, orange, yellow, green, blue, violet, grey, white}; ColorCode myColorCode; myColorCode = yellow; Even though yellow has an integer value of 4, you cannot say myColorCode = 4.


What is new keyword in c?

enum, void and const are relatively new keywords in Cnew, on the other hand, isn't a keyword in C

Related questions

What is meant by enum in C programming?

The enum keyword means enumeration.


How do you pass enum object as argument to function in c?

You can't pass an enum as an argument to a function. An enum in C isn't an object, it's a type. All you can do is pass a variable that is of the particular enum's type.


What is difference between C enum and C plus plus enum?

In C++, enum signifies a slightly stronger type than in C. For example, in C, one could write: enum Direction { UP, DOWN }; Direction d = 1; In C++, this would be illegal, only UP or DOWN can be assigned to a variable of type Direction, though there is still implicit casting to integer, so one could still write: int i = UP; Another difference has to do with the way variable are declared in general in C++. In C, once the enum was declared as above, declaring variables of type Direction would have to be done through the enum keyword, like this: enum Direction d = UP; In C++, the name "Direction" becomes a type in itself, so you can write: Direction d = UP;


In c plus plus you cannot assign integer value to enum?

That is correct - In c plus plus you cannot assign integer value to enum - You can only assign an enum value to an enum. Even though an enum looks like an integer, it is not. It is an enum, and C++ implements strict type checking to reduce the probability of bad programming practices. enum ColorCode {black, brown, red, orange, yellow, green, blue, violet, grey, white}; ColorCode myColorCode; myColorCode = yellow; Even though yellow has an integer value of 4, you cannot say myColorCode = 4.


What is new keyword in c?

enum, void and const are relatively new keywords in Cnew, on the other hand, isn't a keyword in C


What is similarity among structure union and enum in c language?

all the three are user defined datatypes


When was Kadhal Enum Nadhiyinile created?

Kadhal Enum Nadhiyinile was created in 1989.


What is enum type?

An enum type is a type whose fields consist of a fixed set of constants


How can you pass enum datatype as argument to function in c?

Here is the simple code, which demonstrates how you can use enum as function argument in c. =================================================== #include &lt;stdio.h&gt; // Enum declaration for power stat enum PowerStat{ OFF, ON } powerStat; // Function printing the stat of power supply void func( enum PowerStat ); int main(int argc, char* argv[]) { // Set power supply stat OFF powerStat = OFF; // Call function to print the stat func( powerStat); // Set power supply stat ON powerStat = ON; // Call function to print the stat func( powerStat); return 0; } void func( enum PowerStat stat) { printf("Power is = %s\n", stat ? "ON" : "OFF" ); } ================================================== I haven't compiled and run above code but it has to work fine. Let me know, if you found difficulty to compile it. Email: ramji.jiyani@gmail.com


What are Java enums?

An enum, short for enumerated type, is a variable type that can only take on the values that are declared inside the enum declaration. An enum is declared like a class, except the word "class" is replaced by the word "enum", and the class body is replaced by a list of values that a variable of that type can take on. You can also include methods, instance variables, and constructors in an enum.


What is enum in c sharp?

Enum in C#, most of time, are used to defined integer-based values in symbolic ways.Example - exiting or return codesTraditionally, when a program exits, it may return an integer value to indicate the status of the execution.0 everything is OK1 Something was wrong, but not fatal2 Fatal Errorpublic enum ExitCode {OK = 0, Warning = 1, FatalError = 2}... return ExitCode.OK; // old way would by return 0; and one would need to know 0 stands for


Enum in java?

Enum in java is a keyword which is introduced in JDK 1.5 and its a type like Interface and Class.Enum constants are implicitly static and final and you can not change there value once created. Enum in Java provides type-safety and can be used inside switch statment like int variables. Since enum is a keyword you can not use as variable name and since its only introduced in JDK 1.5 all your previous code which has enum as variable name will not work and needs to be refactored.