Home C C++ Java Python Perl PHP SQL JavaScript Linux Selenium QT Online Test

Home » C Programming » Tutorial » Enums

Enums in C Programming

Like Strucutres, Enums (also known as enumerated type) are also user defined data types but they are different from structures. Enum contains set of named values called members, elements or enumeral. Enums variables are identifiers that behaves as constants in the language. Please note in enum variables you can assigned values which are part of enum, any other constant can't be assigned.

Syntax of Enum in C language


enum enum_variable {constant1, constant2, constant3..etc..};

If you look at the syntax, "enum" is a keyword used to defined the enum type. constant1, constant2.. are constants which can be n in number. The default value of frst constant is 0, but we can assign any value to it. The value of next constant is always +1 to the previous one.

1st - Example on Enum


#include<stdio.h>

int main()
{
enum sex_enum{MALE, FEMALE};
enum sex_enum sex = MALE;

printf("Value assigned to enum's first element : %d", sex);

enum sex_enum_x{MALE = 10, FEMALE};
enum sex_enum_x sex_x = MALE;

printf("\n Value assigned to enum's first element : %d", sex);
return 0;
}