Skip to main content

Enums in C

 


What is Enum?

The enumeration in C, helps the programmer to create user-defined data types, to make the code more easy and readable. Enum is almost similar to structure, as it can hold any data type, that the programmer may want, this is advantageous when the program gets complicated or when more than one programmer would be working on it, for the sake of better understanding.  Enum saves time. Make the program more readable To define enums, the enum keyword is used enum variables can also take their corresponding integral value, starting from zero. 


How to declare an enum?

Declaring an enum is almost similar to declaring a structure. We can declare an enum using the keyword “enum” followed by the enum name, and then stating its elements with the curly braces “{}”, separated by commas.   

enum color {
red,
green,
blue
};
  • The first part of the above declaration declares the data type and specifies its possible values. These values are called “enumerators”.
  • The second part declares the variables of these data-types

 

 

How does enum works?

The variables declared within the enum, are treated as integers, i.e.; each variable of the enumerator can take its corresponding integral value, starting from zero(0). That means in our above-discussed example,

  • res = 0
  • green = 1
  • blue = 2

The main purpose of using enumerated variables is to ease the understanding of the program. For example, if we need to employ departments in a payroll program, it’ll make the listing easier to read if we use values like Assembly, Production, Accounts, etc, rather than using 1,2, and 3. Let's take one more example for understanding this

#include<stdio.h>
#include<string.h>
void main()
   {
       enum emp_dept    //declaring enum emp_dept and its variables
           {
                 assembly,
                 manufacturing,
                 accounts,
                 stores
           };
       struct employee   //declaring srtucture
           {
              char name[30];
              int age;
              float bs;
              enum emp_dept department;
           };
         struct employee e;
       strcpy(e.name, "Coding Turtle");   //intializing employee details
       e.age=21;
       e.bs=125000;
       e.department=stores;

       printf("\n Name=%s",e.name);
       printf("\n Age=%d",e.age);
       printf("\n Basic Salary=%f",e.bs); 
       printf("\n Department=%d",e.department);

          if(e.department==accounts)
              printf("\n %s is an accountant",e.name);
          else
              printf("\n %s is not an accountant",e.name);
    }

Output

Name=Coding Turtle
 Age=21
 Basic Salary=125000.000000
 Department=3
 Coding Turtle is not an accountant
Name=Vibush Upadhyay
 Age=22
 Basic Salary=12500.000000
 Department=3
 Vibush Upadhyay is not an accountant

Comments

Popular posts from this blog

Variables in C

  Variables in C   Variables in programming In computer terms, a variable is the name of the memory location of the data which can be accessed with the help of that name and can be rewritten or used for calculations later on. Variable is a storage area. which is used to store the data. variable data is contained, which can be changed at any time during program execution. After declaring the variable it is given a value, this value is to be assigned in many forms. Like x = 5; or a=10;    Before using the variable it is necessary to declare in C. Values of variables are changeable. You can delete a value and enter another value.  You can also do this on compile time and dynamically(during program execution). For example:  Let’s say we have to store a student’s name and his roll number in a variable. For this, we take two variables, store the name of the student in a variable while the other variable stores the roll number ...

Identifiers and keywords in C

In this section, we will learn about the keywords and reversed words in C programming that are part of the syntax. also, we will learn about the identifiers and how to name them. Keywords int, double, struct, switch, etc. Identifiers test, count, high, high-speed, etc. now we will study identifiers and keywords in detail. we will see, how many keywords are there in C programming and all the rule for the select identifiers. Keywords in C Keywords are predefined, reversed words used in programming that have special meanings to the compiler. Each keyword is meant to perform a specific function in a C program. Since keywords are referred names for the compiler, they can not be used as variable names. There are a fixed number of  Keyword in the C programming language  and every Keyword serve as a building block for program statements.  There is a total of 32 keywords in C keywords written in lowercase letters. The following table represents the keywords i...

Sqrt() function in C

  What is the sqrt() function in C? In C programming, the sqrt() function is used to find out the square root of a number. sqrt() function is one of the pre-defined functions in C programming, stored in math.h header file. We can use the sqrt() function, after including the math. h header file at the start of our program. There are many other library functions and header files available in the C programming language, you can study them on our library functions on the C page   How to use sqrt() sqrt() function in C programming is a double-type function, that is it returns the value in decimal format. This function works with int, short, byte long, double, and float datatypes, but sometimes, on some compilers, it may give an error while using any datatype other than double. Syntax for sqrt() function sqrt( integer ); Let’s take a sample code, and understand how to use the sqrt() function C code for using sqrt() function #include #include int main() { ...