The ability to “label” the constant values allows us to develop the program in a much clearer and readable way. For example, if we write the code 3.14159265359, it’s not the same as simply calling it with the “pi” identifier.
Unlike the macro-defined constants, the enumerators can create lists of multiple constants, each is enumerated with an integer and is enclosed in a common enumerator.
In this Linuxhint article, you’ll learn how to use the enumerators in the C language. We’ll see their syntax and the theoretical description of how they work. We’ll also show you how to define an enumerator and its list of constants by assigning them the default or predefined values. After that, we’ll apply what we learned in practical examples that include code snippets and images in which we implement the use of enumerators in different cases.
Syntax of the Enum Sentence in C Language
{
const_1,
const_2,
const_3,
const_…n
}enumerated variable;
Description of the Enum Sentence in C Language
The enum statement creates an enumerator which contains a list of constants, each represented by an identifier and a constant integer value. The programmer can assign a specific integer value to each constant. If this value isn’t specified, the enumerator assigns a default integer value to each constant, counting up from 0.
To declare an enumerator, the enum statement is used, followed by its identifier and the declaration of the constants which are enclosed in curly braces and are separated by commas.
The enum statement also provides the possibility to declare one or more integer variables to store the values of the enumerated constants.
How to Declare an Enumerator with Constant Default Values in C Language
In this example, we’ll show you how to declare an enumerator with its default enumeration constants.
To do this, we declare the list enumerator with enum and 5 constants in it with the const_1, const_2, const_…4 identifiers. This way, we have an enumerator with a list of 5 constants which are numbered from 0 to 4. Next, we’ll see how to define this enumerator.
{
const_0,
const_1,
const_2,
const_3,
const_4
};
Look at the following code where this enumerator is declared and the printf() function displays the default value of each:
void main ()
{
enum list
{
const_0,
const_1,
const_2,
const_3,
const_4
};
printf("Constant 1 enumeration %i\nConstant 2 enumeration %i\nConstant 3 enumeration %i\nConstant 4 enumeration %i\nConstant 5 enumeration %i\n", const_0, const_1,const_2,const_3,const_4);
}
You can see a picture that shows the compilation and execution of the code in the following image. Here, you can see the enumerated default value of each constant which is contained in the enumerator list:
How to Declare an Enumerator with Assigned Constant Values in C Language
In this example, we will show you how to define an enumerator and set the enumeration values to constants.
The values of the constants in the declaration of an enumerator are sentenced in the same way that the value is assigned to a variable, that is, the identifier followed by the “=” symbol and the assigned value. The following code declares the same enumerator from the previous example and assigns the enumeration values of 0, 25, 50, 75, and 100, respectively to the constants:
{
const_0 =0,
const_1 =25,
const_2 =50,
const_3 =75,
const_4 =100
}
The following is the code where this enumerator is declared and the printf() function returns the set of constant value of each:
void main ()
{
enum list
{
const_0 =0,
const_1 =25,
const_2 =50,
const_3 =75,
const_4 =100
};
printf("Constant 1 enumeration %i\nConstant 2 enumeration %i\nConstant 3 enumeration %i\nConstant 4 enumeration %i\nConstant 5 enumeration %i\n", const_0, const_1,const_2,const_3,const_4);
}
You can see a following image which shows the compilation and execution of the code. You can see the enumerated set value of each constant that is contained in the enumerator list:
How to Use the Enumerators in Switch Conditionals
The use of enumerators is very useful in branching or jump conditionals. The following example shows how to use an enumerator and its enumerated constants in a conditional switch.
The first step is to declare the enumerator operation which includes three constants that are enumerated by default, each representing the trigonometric functions like sine, cosine, and tangent. We also declare an enumeration variable with the identifier function.
Using scanf(), we enter the value of an operand and select a constant that represents a trigonometric function by its enumeration value.
The switch jump condition is the variable function of the enumeration variable and each jump case is an enumerated constant.
In each case, the specified trigonometric function is called to solve the formula and the result is displayed on the screen.
The following code shows how an enumerator can declare a list of constants to be used as cases in a switch condition. In this case, we create a basic trigonometric calculator from the basic inputs and outputs and a switch that is controlled by enumerated constants:
#include <stdarg.h>
#include <math.h>
void main ()
{
double x;
double a;
enum operation
{
sine,
cosine,
tangent
} function;
printf("Enter a value of the variable \n");
scanf ("%lf", &x);
printf("Enter the trigonometric operation \n\nSine [0]\nCosine [1]\nTan gent [2]\n");
scanf ("%i", &function);
a = x * 3.14159265359 / 180; //degrees to radians
switch (function){
case sine:
printf("The sine of %f is = %f\n", x, sin(a));
break;
case cosine:
printf("The cosine of %f is = %f\n",x ,cos(a));
break;
case tangent:
printf("The tangent of %f is = %f\n", x, tan(x));
break;
}
}
The following image shows the compilation and execution of this code which performs the trigonometric calculations from a list of enumerated functions:
Conclusion
In this Linuxhint article, we showed you how to use the enumerators in the C language. We have seen a theoretical part which explains what an enumerator is in this language and describes its syntax and operation.