0% found this document useful (0 votes)
6 views

Notes Enumeration

The document explains the concept of enumeration (enum) in C, which is a user-defined data type used to assign names to integral constants for better readability and maintenance. It provides examples of defining enums, changing default values, and using enums in switch-case statements. Additionally, it covers the typedef keyword, which allows for creating meaningful names for existing variable types, including its use with structures.

Uploaded by

Mani R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Notes Enumeration

The document explains the concept of enumeration (enum) in C, which is a user-defined data type used to assign names to integral constants for better readability and maintenance. It provides examples of defining enums, changing default values, and using enums in switch-case statements. Additionally, it covers the typedef keyword, which allows for creating meaningful names for existing variable types, including its use with structures.

Uploaded by

Mani R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Enumeration

Enumeration (or) enum is a user defined data type in C. It is mainly used to assign names to
integral constants, the names make a program easy to read and maintain.

The following is the way to define the enum in C:


enum flag{integer_const1, integer_const2,.....integter_constN};
Description: In the above declaration, we define the enum named as flag containing 'N' integer
constants. The default value of integer_const1 is 0, integer_const2 is 1, and so on. We can also
change the default value of the integer constants at the time of the declaration.
ex: enum fruits{mango, apple, strawberry, papaya};
Description: The default value of mango is 0, apple is 1, strawberry is 2, and papaya is 3.

They can be defined in two ways:

enum week{Mon, Tue, Wed};


enum week day;
Or
enum week{Mon, Tue, Wed}day;

If we want to change these default values, then we can do as given below:


enum fruits{
mango=2,
apple=1,
strawberry=5,
papaya=7, };
Ex:
#include <stdio.h>
enum weekdays{Sunday=1, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday};
int main()
{
enum weekdays w; // variable declaration of weekdays type
w=Monday; // assigning value of Monday to w.
printf("The value of w is %d",w);
return 0;
}
Output: 2
Ex:
#include <stdio.h>
enum months{jan=1, feb, march, april, may, june, july, august, september, october,
november, december};
int main()
{
for(int i=jan;i<=december ;i++)
{
printf("%d, ",i);
}
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10 11 12

Let's see how we can use an enum in a switch case statement.


#include <stdio.h>
enum days{sunday=1, monday, tuesday, wednesday, thursday, friday, saturday};
int main()
{
enum days d;
d=monday;
switch(d)
{
case sunday:
printf("Today is sunday");
break;
case monday:
printf("Today is monday");
break;
case tuesday:
printf("Today is tuesday");
break;
case wednesday:
printf("Today is wednesday");
break;
case thursday:
printf("Today is thursday");
break;
case friday:
printf("Today is friday");
break;
case saturday:
printf("Today is saturday");
break;
}

return 0;
}

Some important points related to enum

 The enum names available in an enum type can have the same value.

Let's look at the example.


#include <stdio.h>
int main(void)
{
enum fruits{mango = 1, strawberry=0, apple=1};
printf("The value of mango is %d", mango);
printf("\n The value of apple is %d", apple);
return 0;
}
 The values assigned to the enum names must be integral constant, i.e., it should not be of
other types such string, float, etc.
 All the enum names must be unique in their scope, i.e., if we define two enum having same
scope, then these two enums should have different enum names otherwise compiler will
throw an error.

Ex:
#include <stdio.h>
enum status{success, fail};
enum boolen{fail,pass};
int main(void)
{
printf("The value of success is %d", success);
return 0;
}
Output: Error
In enumeration, we can define an enumerated data type without the name also.
#include <stdio.h>
enum {success, fail} status;
int main(void)
{
status=success;
printf("The value of status is %d", status);
return 0;
}

Typdef:

The typedef is a keyword used in C programming to provide some meaningful names to the
already existing variable in the C program. It behaves similarly as we define the alias for the
commands. In short, we can say that this keyword is used to redefine the name of an already
existing variable.

Syntax of typedef

typedef <existing_name> <alias name>

Ex: typedef unsigned int unit;

In the above statements, we have declared the unit variable of type unsigned int by using a
typedef keyword.

Now, we can create the variables of type unsigned int by writing the following statement:
Ex: int main()

typedef unsigned int unit;


unit i, j;
i=10, j=20;
printf("Value of i is :%d", i);
printf("\tValue of j is :%d",j);
return 0;
}
Output: Value of i is :10 Value of j is: 20

Using typedef with structures:

Ex: struct student {


char name[20];
int age;
};
typedef struct student stud;
stud s1, s2;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy