SlideShare a Scribd company logo
1 of 17
Vijayananda Ratnam Ch,
Asst. Professor,
Dept. of CSE,
VVIT – Guntur.
Enumerations in C
1
Enumeration/enum
2
 An enumeration (enum) is a user-defined type (same as
structure) that represents a group of constants.
(or)
 enum is a user defined data type where we specify a set of
possible values for a variable and the variable can only take
one out of a set of possible values.
 It is used to assign names to the integral constants, which
makes​ a program easy to read and maintain.
When use enum in C
3
 Enums are used only when variable has one set of possible values
that are not going to change (e.g., days of the week, colors in a
rainbow, number of cards in a deck, directions, etc.​).
 For example, we have a direction variable that holds the direction.
Since we have four directions, this variable can take any one of the four
values, if we try to assign a another random value to this variable, it will
throw a compilation error.
 This increases compile-time checking and avoid errors that occurs by
passing in invalid constants.
 Another important place where they are used frequently are switch case
Defining an enum
4
 An enum is defined with a keyword enum and the elements separated
by 'comma' as follows.
Syntax:
enum enum_name
{
element1,
element2,
…
…
elementN
};
Example
5
 For example, Summer, Spring, Winter and Autumn are the names of four
seasons. Thus, we can say that these are of types season. Therefore, this
becomes an enumeration with name season and Summer, Spring, Winter and
Autumn as its elements.
 Now let's define an enum of the above example of seasons.
enum Season
{
Summer,
Spring,
Winter,
Autumn
};
Values for the Members of Enum
6
 All the elements of an enum have a value. By default, the value of the
first element is 0, that of the second element is 1 and so on.
Let's see an example.
#include <stdio.h>
enum season{ Summer, Spring, Winter, Autumn};
int main() {
printf("%dn",Summer);
printf("%dn",Autumn);
return 0;
}
Assigning custom values to enum elements
8
 It is also possible to assign your custom values to enum elements by writing:
enum enum_name {
name1 = constan1/expression1,
name2 = constan1/expression2,
…
…
name N = constanN/expression
};
Example:
enum TV { FOX = 11, CNN = 25, ESPN = 15, HBO = 22, SONY = 30, NG = FOX+CNN };
printf(" FOX: t%2dn", FOX);
printf(" CNN: t%2dn", CNN);
printf(" NG: t%2dn", NG);
9
 We can assign values to some name in any order. All
unassigned names get value as value of previous name
plus one.
#include <stdio.h>
enum rainbow {violet = 1, indigo, blue = 5,green, yellow=12, orange, red};
int main()
{
printf("%d %d %d %d %d %d %d", violet, indigo, blue, green, yellow,
orange, red);
return 0;
}
Output:
10
Two enum names can have same value.
For example, in the following C program both ‘Failed’ and ‘Freezed’ have same
value 0.
#include <stdio.h>
enum State {Working = 1, Failed = 0, Freezed = 0};
int main()
{
printf("%d, %d, %d", Working, Failed, Freezed);
return 0;
}
Output:
1, 0, 0
11
 All enum constants must be unique in their
scope.
 For example, the following program fails in
compilation.
enum state {working, failed};
enum result {failed, passed};
int main()
{
return 0;
}
Questions?
12
 Write a C program to create enumerated data type for 7 days and
display their values in integer constants.
 Write a C program to create enumerated data type for 7 days.
When you provide an enumerator as input to switch case, it should
display like “Today is monday”.
typedef in C
13
 typedef is a keyword used to create alias name for the existing
datatypes.
 Using typedef keyword we can create a temporary name to the system
defined datatypes and user – defined datatypes like structure, unions,
arrays,etc.
 We use that temporary name to create a variable. The general syntax of
typedef is as follows...
typedef <existing-datatype> <alias-name>;
 Example:
typdef int Number;
 In the above example, Number is defined as alias name for integer
datatype. So, we can use Number to declare integer variables.
Example Program to illustrate typedef in C.
14
#include<stdio.h>
typedef int Number;
int main()
{
Number a,b,c; // Here a, b and c are integer type of variables.
printf("Enter any two integer numbers: ") ;
scanf("%d%d", &a,&b) ;
c = a + b;
printf("Sum = %d", c) ;
return 0;
}
typedef with structures
15
 You can use typedef to give a name to your user defined data types as well.
 For example, you can use typedef with structure to define a new data type
and then use that data type to define structure variables directly as follows −
#include<stdio.h>
typedef struct employee
{
char name[50];
int salary;
}emp;
int main( )
{
emp e1;
printf("Employee name:");
scanf("%s", e1.name);
printf("Enter Employee salary:");
scanf("%d", &e1.salary);
printf("nStudent name :%sn", e1.name);
printf("roll no: %d", e1.salary);
return 0;
}
typedef with Arrays
16
 typedef is also used with arrays.
 Consider the following example program to understand how typedef is
used with arrays.
int main(){
// Here Array acts like an integer array type of
size 5.
typedef int Array[5];
// List is an array of integer type with size 5.
Array list ={10,20,30,40,50};
int i;
printf("List elements are : n") ;
for(i=0; i<5; i++)
printf("%dt", list[i]) ;
return 0;
}
17
18

More Related Content

What's hot

Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2MOHIT TOMAR
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programmingprogramming9
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in CSahithi Naraparaju
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanMohammadSalman129
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programmingRumman Ansari
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programmingKamal Acharya
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT03062679929
 
Command line arguments
Command line argumentsCommand line arguments
Command line argumentsAshok Raj
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in cniyamathShariff
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 

What's hot (20)

Pointers
PointersPointers
Pointers
 
Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2Datatype in c++ unit 3 -topic 2
Datatype in c++ unit 3 -topic 2
 
Structure in c
Structure in cStructure in c
Structure in c
 
Unions in c
Unions in cUnions in c
Unions in c
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
Data types in java
Data types in javaData types in java
Data types in java
 
Strings
StringsStrings
Strings
 
constants, variables and datatypes in C
constants, variables and datatypes in Cconstants, variables and datatypes in C
constants, variables and datatypes in C
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Type casting in c programming
Type casting in c programmingType casting in c programming
Type casting in c programming
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
Managing input and output operations in c
Managing input and output operations in cManaging input and output operations in c
Managing input and output operations in c
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 

Similar to Enums in c

User defined data type
User defined data typeUser defined data type
User defined data typeAmit Kapoor
 
component of c language.pptx
component of c language.pptxcomponent of c language.pptx
component of c language.pptxAnisZahirahAzman
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01Wingston
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++Rokonuzzaman Rony
 
C intro
C introC intro
C introKamran
 
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptxLeenaChaudhari24
 
Introduction to C programming language. Coding
Introduction to C programming language. CodingIntroduction to C programming language. Coding
Introduction to C programming language. CodingTanishqGosavi
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokePranoti Doke
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Languagemadan reddy
 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)ssuser7f90ae
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic Manzoor ALam
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptxFolkAdonis
 
ENUM - make u r names as data types
ENUM - make u r names as data typesENUM - make u r names as data types
ENUM - make u r names as data typesAjay Chimmani
 
05a-enum.ppt
05a-enum.ppt05a-enum.ppt
05a-enum.pptMuthuMs8
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3Vince Vo
 

Similar to Enums in c (20)

User defined data type
User defined data typeUser defined data type
User defined data type
 
component of c language.pptx
component of c language.pptxcomponent of c language.pptx
component of c language.pptx
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
C intro
C introC intro
C intro
 
Tut1
Tut1Tut1
Tut1
 
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
3110003_PPS_GTU_Study_Material_Presentations_Unit-2_18122020041700AM (1).pptx
 
Introduction to C programming language. Coding
Introduction to C programming language. CodingIntroduction to C programming language. Coding
Introduction to C programming language. Coding
 
C programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti DokeC programming_MSBTE_Diploma_Pranoti Doke
C programming_MSBTE_Diploma_Pranoti Doke
 
Pointers in C Language
Pointers in C LanguagePointers in C Language
Pointers in C Language
 
23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)23UCACC11 Python Programming (MTNC) (BCA)
23UCACC11 Python Programming (MTNC) (BCA)
 
02a fundamental c++ types, arithmetic
02a   fundamental c++ types, arithmetic 02a   fundamental c++ types, arithmetic
02a fundamental c++ types, arithmetic
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
enums
enumsenums
enums
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
 
ENUM - make u r names as data types
ENUM - make u r names as data typesENUM - make u r names as data types
ENUM - make u r names as data types
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
05a-enum.ppt
05a-enum.ppt05a-enum.ppt
05a-enum.ppt
 
Java căn bản - Chapter3
Java căn bản - Chapter3Java căn bản - Chapter3
Java căn bản - Chapter3
 

Recently uploaded

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 

Enums in c

  • 1. Vijayananda Ratnam Ch, Asst. Professor, Dept. of CSE, VVIT – Guntur. Enumerations in C 1
  • 2. Enumeration/enum 2  An enumeration (enum) is a user-defined type (same as structure) that represents a group of constants. (or)  enum is a user defined data type where we specify a set of possible values for a variable and the variable can only take one out of a set of possible values.  It is used to assign names to the integral constants, which makes​ a program easy to read and maintain.
  • 3. When use enum in C 3  Enums are used only when variable has one set of possible values that are not going to change (e.g., days of the week, colors in a rainbow, number of cards in a deck, directions, etc.​).  For example, we have a direction variable that holds the direction. Since we have four directions, this variable can take any one of the four values, if we try to assign a another random value to this variable, it will throw a compilation error.  This increases compile-time checking and avoid errors that occurs by passing in invalid constants.  Another important place where they are used frequently are switch case
  • 4. Defining an enum 4  An enum is defined with a keyword enum and the elements separated by 'comma' as follows. Syntax: enum enum_name { element1, element2, … … elementN };
  • 5. Example 5  For example, Summer, Spring, Winter and Autumn are the names of four seasons. Thus, we can say that these are of types season. Therefore, this becomes an enumeration with name season and Summer, Spring, Winter and Autumn as its elements.  Now let's define an enum of the above example of seasons. enum Season { Summer, Spring, Winter, Autumn };
  • 6. Values for the Members of Enum 6  All the elements of an enum have a value. By default, the value of the first element is 0, that of the second element is 1 and so on. Let's see an example. #include <stdio.h> enum season{ Summer, Spring, Winter, Autumn}; int main() { printf("%dn",Summer); printf("%dn",Autumn); return 0; }
  • 7. Assigning custom values to enum elements 8  It is also possible to assign your custom values to enum elements by writing: enum enum_name { name1 = constan1/expression1, name2 = constan1/expression2, … … name N = constanN/expression }; Example: enum TV { FOX = 11, CNN = 25, ESPN = 15, HBO = 22, SONY = 30, NG = FOX+CNN }; printf(" FOX: t%2dn", FOX); printf(" CNN: t%2dn", CNN); printf(" NG: t%2dn", NG);
  • 8. 9  We can assign values to some name in any order. All unassigned names get value as value of previous name plus one. #include <stdio.h> enum rainbow {violet = 1, indigo, blue = 5,green, yellow=12, orange, red}; int main() { printf("%d %d %d %d %d %d %d", violet, indigo, blue, green, yellow, orange, red); return 0; } Output:
  • 9. 10 Two enum names can have same value. For example, in the following C program both ‘Failed’ and ‘Freezed’ have same value 0. #include <stdio.h> enum State {Working = 1, Failed = 0, Freezed = 0}; int main() { printf("%d, %d, %d", Working, Failed, Freezed); return 0; } Output: 1, 0, 0
  • 10. 11  All enum constants must be unique in their scope.  For example, the following program fails in compilation. enum state {working, failed}; enum result {failed, passed}; int main() { return 0; }
  • 11. Questions? 12  Write a C program to create enumerated data type for 7 days and display their values in integer constants.  Write a C program to create enumerated data type for 7 days. When you provide an enumerator as input to switch case, it should display like “Today is monday”.
  • 12. typedef in C 13  typedef is a keyword used to create alias name for the existing datatypes.  Using typedef keyword we can create a temporary name to the system defined datatypes and user – defined datatypes like structure, unions, arrays,etc.  We use that temporary name to create a variable. The general syntax of typedef is as follows... typedef <existing-datatype> <alias-name>;  Example: typdef int Number;  In the above example, Number is defined as alias name for integer datatype. So, we can use Number to declare integer variables.
  • 13. Example Program to illustrate typedef in C. 14 #include<stdio.h> typedef int Number; int main() { Number a,b,c; // Here a, b and c are integer type of variables. printf("Enter any two integer numbers: ") ; scanf("%d%d", &a,&b) ; c = a + b; printf("Sum = %d", c) ; return 0; }
  • 14. typedef with structures 15  You can use typedef to give a name to your user defined data types as well.  For example, you can use typedef with structure to define a new data type and then use that data type to define structure variables directly as follows − #include<stdio.h> typedef struct employee { char name[50]; int salary; }emp; int main( ) { emp e1; printf("Employee name:"); scanf("%s", e1.name); printf("Enter Employee salary:"); scanf("%d", &e1.salary); printf("nStudent name :%sn", e1.name); printf("roll no: %d", e1.salary); return 0; }
  • 15. typedef with Arrays 16  typedef is also used with arrays.  Consider the following example program to understand how typedef is used with arrays. int main(){ // Here Array acts like an integer array type of size 5. typedef int Array[5]; // List is an array of integer type with size 5. Array list ={10,20,30,40,50}; int i; printf("List elements are : n") ; for(i=0; i<5; i++) printf("%dt", list[i]) ; return 0; }
  • 16. 17
  • 17. 18