Enumerated Data Type. An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration.

Slides:



Advertisements
Similar presentations
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Advertisements

Programming In C++ Spring Semester 2013 Lecture 12 Programming In C++, Lecture 12 By Umer Rana.
C Language.
C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
C Structures Basics of structures Typedef. Data Hierarchy Byte –8 bits (ASCII character ‘A’ = ) Field –Group of characters (character string “Fred”)
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Variables and constants Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
Introduction to C language
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
C PROGRAMMING LECTURE 17 th August IIT Kanpur C Course, Programming club, Fall by Deepak Majeti M-Tech CSE
CHAPTER 7 DATA HANDALING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
CISC105 – General Computer Science Class 9 – 07/03/2006.
GUIDED BY- A.S.MODI MADE BY- 1. SHWETA ALWANI 2. PRIYANKA.
ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
CSCI 130 Chapter 3. Variables & Names Variable Declarations: –reserve a storage location in memory –identify the name of the variable –identify the type.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
Passing Structure to function.  structure to function structure to function  Passing structure to function in C Passing structure to function in C 
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Gator Engineering 1 Chapter 2 C Fundamentals (Continued) Copyright © 2008 W. W. Norton & Company. All rights reserved.
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Weekly C-minar Week 0. Today: Steps of the compile Basic inclusion/syntax rules Low-cost containment Debugging.
Computer Programming for Engineers
Structures and Union. Review bitwise operations –you need them for performance in terms of space and time –shifts are equivalent to arithmetics enumeration.
CSE 251 Dr. Charles B. Owen Programming in C1 structs Aggregating associated data into a single variable Box width length height Circle radius int main()
Structure A collection of values (members) struct date{ int day; char month[10]; int year; }; Declare a structure variable struct date today; struct struct_name.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Pointers A pointer type variable holds the address of a data object or a function. A pointer can refer to an object of any one data type; it cannot refer.
Chapter 6. Character String Types It is one in which the values consists of sequences of characters. How to Define a variable contain a string? In a programming.
Chapter 11 Structures, Unions and Typedef 11.1 Structures Structures allow us to group related data items of different types under a common name. The individual.
BASIC C PROGRAMMING LANGUAGE TUTORIAL infobizzs.com.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Lecture 10 union, enumeration, bit
Numbers in ‘C’ Two general categories: Integers Floats
C++ Lesson 1.
Ex-1 #include <stdio.h> struct sample { int a=0; char b='A';
‘C’ Programming Structures and Commands
Structures, Unions, Enumerations
A bit of C programming Lecture 3 Uli Raich.
LESSON 3 IO, Variables and Operators
Visit for more Learning Resources
By: Syed Shahrukh Haider
CMSC 104, Section 4 Richard Chang
DATA HANDLING.
Buy book Online -
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Structures and Union.
Govt. Polytechnic,Dhangar
Variables in C Declaring , Naming, and Using Variables.
Structures and Union.
C Structures, Unions, Bit Manipulations and Enumerations
WEEK-2.
Programming Language C Language.
Variables in C Topics Naming Variables Declaring Variables
C Language B. DHIVYA 17PCA140 II MCA.
CSCE 206 Lab Structured Programming in C
Variables in C Topics Naming Variables Declaring Variables
Presentation transcript:

Enumerated Data Type

An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name of the (optional) enumeration tag and defines the set of named integer identifiers (called the "enumeration set," "enumerator constants," "enumerators," or "members"). A variable with enumeration type stores one of the values of the enumeration set defined by that type.

It allows you to define your own data type and define what values the variable of this data type can take. One could invent data type “mar-status” which can have 4 possible values-single, married, divorced, or widowed. enum mar-status { single, married, divorced, widowed }; enum mar-status person1,person2;

1 st part declares the data type and specifies possible values called “enumerators” 2 nd part declares variables of this data type. we can give values to variables: person1=married; person2=divorced; person1=unknown ---> cause error. Compiler treats enumerators as integers. Single-0; married-1; divorced-2; widowed-3.

Programmer can initialize enumerators to different integer values as below enum mar-status { single=100,married=200,divorced=300,widowed=400 }; enum mar-status person1,person2;

#include Void main() { enum emp-dept { assembly,manufacturing,accounts,store }; struct employee { char name[30]; int age; float bs; enum emp-dept department; };

struct employee e; strcpy(e.name,”Lothar matheus”); e.age=28; e.bs= ; e.department=manufacturing; printf(“\n name=%s”,e.name); printf(“\n age=%d”,e.age); printf(“\n basic salary=%f”,e.bs); printf(“\n dept=%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=Lothar matheus age=28 basic salary= dept=1 Lothar matheus is not an accountant Department is printed out as 1 and not as manufacturing.

void main() { enum {a,b=32766,c,d,e=0}; clrscr(); printf("%d",a); getch(); } Output: error Explanation: value of enum constant must be within range of signed int.Maximum value of signed int is while value of d=32768

void main() { enum colour { green,red=5,blue,white,yellow=10,pink }; printf(“%d %d %d %d %d %d,green,red,blue,white,yellow,pink} }; Output:

Enum example in C #include int main() { enum Days{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturd ay}; Days TheDay; int j = 0; printf("Please enter the day of the week (0 to 6)\n"); scanf("%d",&j); TheDay = Days(j);

if(TheDay == Sunday || TheDay == Saturday) printf("Hurray it is the weekend\n"); else printf("Curses still at work\n"); return 0; }

TYPEDEF

Renaming Data Types with typedef It is used to redefine the name of an existing variable type. typedef unsigned long int TWOWORDS; We can declare variables of type unsigned long int by writing, TWOWORDS var1,var2; instead of unsigned long int var1,var2; Uppercase letters are used to deal with renamed data types.

Example: struct employee { char name[30]; int age; float bs; }; struct employee e; This structure declaration can be modified using typedef as below: struct employee { char name[30]; int age; float bs; }; typedef struct employee EMP EMP e1,e2;

Typedef can be used to rename pointer data types as: struct employee { char name[30]; int age; float bs; } typedef struct employee *PEMP; PEMP p; p->age=32;