C ENUM

An enumeration type, often known as enum, is a data type used in C programming that is made up of integral constants. The term enum is used to define enums.

It is used to give integral constants names, which facilitates easier reading and maintenance of programs.

Syntax for defining enum:


 enum flags {flag1, flag2, ..., flagN};

Code:



  #include "stdio.h"

  enum days{Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};

  int main(){
      for(int i=Sunday;i<=Saturday;i++){
      printf("%d - ",i+1);
  }
  return 0;
  }

Output:


1-2-3-4-5-6-7-

Explanation:


The names of the weekdays beginning on Sunday were specified as an enum called days in the code above. Next, we set Sunday's starting value to be 1. By doing this, the value for the remaining days will be set as the previous value + 1. We have made a for loop and started the value for i as Sunday to go over the enum and output the values of each day.






© Copyright 2022 Advanced PLC. All rights reserved.