HomeLinuxMethods to Make and Use Enum in C Language

Methods to Make and Use Enum in C Language


Within the C language, the constants which are outlined by macros make it simpler to arrange the programming work as a result of they can be utilized to affiliate an identifier with a continuing worth. However not like the variables, a continuing worth will also be related to an identifier.

The power to “label” the fixed values permits us to develop this system in a a lot clearer and readable method. For instance, if we write the code 3.14159265359, it’s not the identical as merely calling it with the “pi” identifier.

Not like the macro-defined constants, the enumerators can create lists of a number of constants, every is enumerated with an integer and is enclosed in a typical enumerator.

On this Linuxhint article, you’ll learn to use the enumerators within the C language. We’ll see their syntax and the theoretical description of how they work. We’ll additionally present you the best way to outline an enumerator and its record of constants by assigning them the default or predefined values. After that, we’ll apply what we discovered in sensible examples that embody code snippets and pictures through which we implement using enumerators in several instances.

Syntax of the Enum Sentence in C Language

enum enumerator
              {
               const_1,
               const_2,
               const_3,
               const_…n
              }enumerated variable;

Description of the Enum Sentence in C Language

The enum assertion creates an enumerator which comprises an inventory of constants, every represented by an identifier and a continuing integer worth. The programmer can assign a selected integer worth to every fixed. If this worth isn’t specified, the enumerator assigns a default integer worth to every fixed, counting up from 0.

To declare an enumerator, the enum assertion is used, adopted by its identifier and the declaration of the constants that are enclosed in curly braces and are separated by commas.

The enum assertion additionally offers the chance to declare a number of integer variables to retailer the values of the enumerated constants.

Methods to Declare an Enumerator with Fixed Default Values in C Language

On this instance, we’ll present you the best way to declare an enumerator with its default enumeration constants.

To do that, we declare the record enumerator with enum and 5 constants in it with the const_1, const_2, const_…4 identifiers. This manner, we’ve an enumerator with an inventory of 5 constants that are numbered from 0 to 4. Subsequent, we’ll see the best way to outline this enumerator.

enum record
        {
         const_0,
         const_1,
         const_2,
         const_3,
                  const_4
         };

Have a look at the next code the place this enumerator is said and the printf() perform shows the default worth of every:

#embody <stdio.h>

void primary ()

{
enum record
        {
         const_0,
         const_1,
         const_2,
         const_3,
                  const_4
         };

printf(“Fixed 1 enumeration %inFixed 2 enumeration %inFixed 3 enumeration %inFixed 4 enumeration %inFixed 5 enumeration %in, const_0, const_1,const_2,const_3,const_4);
}

You possibly can see an image that exhibits the compilation and execution of the code within the following picture. Right here, you may see the enumerated default worth of every fixed which is contained within the enumerator record:

Methods to Declare an Enumerator with Assigned Fixed Values in C Language

On this instance, we’ll present you the best way to outline an enumerator and set the enumeration values to constants.

The values of the constants within the declaration of an enumerator are sentenced in the identical method that the worth is assigned to a variable, that’s, the identifier adopted by the “=” image and the assigned worth. The next code declares the identical enumerator from the earlier instance and assigns the enumeration values of 0, 25, 50, 75, and 100, respectively to the constants:

enum record
        {
         const_0 =0,
         const_1 =25,
         const_2 =50,
         const_3 =75,
         const_4 =100
                 }

The next is the code the place this enumerator is said and the printf() perform returns the set of fixed worth of every:

#embody <stdio.h>

void primary ()

{
enum record
        {
         const_0 =0,
         const_1 =25,
         const_2 =50,
         const_3 =75,
         const_4 =100
         };

printf(“Fixed 1 enumeration %inFixed 2 enumeration %inFixed 3 enumeration %inFixed 4 enumeration %inFixed 5 enumeration %in, const_0, const_1,const_2,const_3,const_4);
}

You possibly can see a following picture which exhibits the compilation and execution of the code. You possibly can see the enumerated set worth of every fixed that’s contained within the enumerator record:

Methods to Use the Enumerators in Change Conditionals

The usage of enumerators may be very helpful in branching or bounce conditionals. The next instance exhibits the best way to use an enumerator and its enumerated constants in a conditional change.

Step one is to declare the enumerator operation which incorporates three constants which are enumerated by default, every representing the trigonometric capabilities like sine, cosine, and tangent. We additionally declare an enumeration variable with the identifier perform.

Utilizing scanf(), we enter the worth of an operand and choose a continuing that represents a trigonometric perform by its enumeration worth.

The change bounce situation is the variable perform of the enumeration variable and every bounce case is an enumerated fixed.

In every case, the desired trigonometric perform is named to unravel the method and the result’s displayed on the display screen.

The next code exhibits how an enumerator can declare an inventory of constants for use as instances in a change situation. On this case, we create a fundamental trigonometric calculator from the essential inputs and outputs and a change that’s managed by enumerated constants:

#embody <stdio.h>
#embody <stdarg.h>
#embody <math.h>

void primary ()
{
double x;
double a;
enum operation
            {
             sine,
             cosine,
             tangent
            } perform;

printf(“Enter a worth of the variable n);
scanf (“%lf”, &x);
printf(“Enter the trigonometric operation nnSine      [0]nCosine     [1]nTan gent   [2]n);
scanf (“%i”, &perform);

a = x * 3.14159265359 / 180; //levels to radians
change (perform){

    case sine:
            printf(“The sine of %f is = %fn, x, sin(a));
            break;
    case cosine:
            printf(“The cosine of %f is = %fn,x ,cos(a));
            break;
    case tangent:
           printf(“The tangent of %f is = %fn, x, tan(x));
         break;
    }

}

The next picture exhibits the compilation and execution of this code which performs the trigonometric calculations from an inventory of enumerated capabilities:

Conclusion

On this Linuxhint article, we confirmed you the best way to use the enumerators within the C language. Now we have seen a theoretical half which explains what an enumerator is on this language and describes its syntax and operation.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments