ENUMERATED(ENUM) DATATYPE in E-Text C PROGRAMMING

ENUMERATED(ENUM) DATATYPE in E-Text C PROGRAMMING

ENUMERATED(ENUM) DATATYPE IN e-Text C PROGRAMMING B.Durga Anuja, Lecturer in Computer Applications Govt. Degree College (W), Srikalahasti Email. Id : [email protected] Enumerated Data Type(enum): The ANSI provides user defined data type which is called enumerated data type i.e., the programmer can create their own data type and define what values the variables of these data types can hold. • The enum in C is also known as the enumerated type. • It is a user-defined data type. • The use of enum in C makes the program easy to understand and maintain. • The enum is defined by using the enum keyword. Syntax for enum: enum flag {constant1,constant2,…constantN}; Name of the Enumeration Values for created Enumeration In the above syntax enum is a keyword to create the user defined data type and flag is a name of the data type that user creates and constant1..constantN are set possible values for that datatype. Example: I Eg1: enum gender{male,female,other}; Eg2: enum week{sunday,monday,tuesday,wednesday,thursday,friday,saturday}; Here in example1 gender is a user defined data type with possible values male, female and other. This gender datatype variables can hold only these three values. Here in example2 week is a user defined data type with possible values sunday, monday, tuesday, wednesday, thursday, friday, saturday. This week datatype variables can hold only these seven values. 1 variable declaration: enum result{pass,fail}r1; enum result{pass,fail}; enum result r1; In the above variable declaration process the result is user defined datatype where data type creation and variable declaration done in single statement.The same process can be done in two statements by creating enum data type in online and variable declaration in other line. r1 is a variable of result data type where it can store two values either pass or fail. Storage in Memory: • Enum consists of integer values. • Whatever constant values created using enum will be saved as integer values. enum direction{north,south,east,west} north south east west • The default value of const1 is 0, const2 is 1, and so on. • We can also change the default value of the integer constants at the time of the declaration. enum gender{male=1,female=3,other=5}; male female other 2 When we assign value to the one constant in the set and left other without assigning any value then compiler automatically increments previous constant value by 1 and assign to the next constant. enum status{false=1,true}; . false true In the above example in status data type false is assigned to 1 and true is left. So compiler takes value of the false i.e 1 and increments it by 1 and assigns to true.so the value of constant true is 2. Rules to create user defined data type with enum • The values assigned to the enum names must be integral constant • it should not be of other types such string, float, etc. • All the enum names must be unique in their scope enum status{success, fail}; enum boolean{fail,pass}; • if we define two enum having same scope, then these two enums should have different enum names otherwise compiler will throw an error 3 Example Program: #include<stdio.h> #include<conio.h> void main() { enum day{sunday,monday,tuesday,thursday,friday,saturday}; enum day d1,d2,d3; clrscr(); d1=tuesday; printf("\nThe value d1 is:%d",d1); d2=friday; printf("\nThe value of d2 is:%d",d2); d3=d1; printf("\nThe value of d3 is:%d",d3); getch(); } Output: The value of d1 is:2 The value of d2 is:4 The value of d3 is:2 Reference Books/Web Links: [1] “Programming in C” by E Balagurusamy [2] “Introduction to C Programming” by Reema Thareja [3] “Let us C “ by Yashavanth Kanetkar [4] “C:The Complete Reference” by Herbert Schildt [5] https://www.geeksforgeeks.org/c-programming-language/ [6] https://codeforwin.org/ [7] https://www.cprogramming.com/ [8] https://www.tutorialspoint.com/cprogramming/index.htm [9] https://www.javatpoint.com/c-programming-language-tutorial [10] https://beginnersbook.com/ 4 .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    5 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us