1 / 16

Enumerated Data Type

Enumerated Data Type.

maddox
Download Presentation

Enumerated Data Type

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Enumerated Data Type

  2. 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.

  3. 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;

  4. 1st part declares the data type and specifies possible values called “enumerators” • 2nd 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.

  5. 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;

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

  7. struct employee e; strcpy(e.name,”Lothar matheus”); e.age=28; e.bs=5675.50; 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); }

  8. Output: name=Lothar matheus age=28 basic salary=5575.50 dept=1 Lothar matheus is not an accountant Department is printed out as 1 and not as manufacturing.

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

  10. 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: 0 5 6 7 10 11

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

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

  13. TYPEDEF

  14. 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.

  15. 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; }; typedefstruct employee EMP EMP e1,e2;

  16. 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;

More Related