User defined data type


Published on

Published in: Technology, Sports
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

User defined data type

  1. 1. User Defined Data Type “ Enumerated Data Type”
  2. 2. Enumerated Data Type ( enum ) <ul><li>An Enumerated data type consists of an ordered set of distinct constant values defined in a data type in a program. The format of en-um is:- </li></ul><ul><li>enum name </li></ul><ul><li>{ </li></ul><ul><li>value-1,value-2,value-3,…….,value-4; </li></ul><ul><li>}; </li></ul><ul><li>where., name is the name of the enumerated data type, also known as tag and value-1,value-2,value-3,…….,value-n are values that variable of type name can take. </li></ul>
  3. 3. <ul><li>Example (1) : - </li></ul><ul><li>enum fruit </li></ul><ul><li>{ </li></ul><ul><li>apple,orange,mango,pineapple; </li></ul><ul><li>}; </li></ul><ul><li>This declaration states that a variable of type fruit can have any of the four values namely apple, orange, mango and pineapple, but no other value. Once the type fruit has been defined then variable of that type can be defined in the following manner :- </li></ul><ul><li>fruit a,b; </li></ul><ul><li>Now, the variable a and b are of type fruit and can assume value from the list : (apple, orange, mango, pineapple). </li></ul>
  4. 4. <ul><li>Example (2) : - </li></ul><ul><li>Part-1 enum days </li></ul><ul><li>{ </li></ul><ul><li>mon,tue,wed,thu,fri,sat,sun; </li></ul><ul><li>}; </li></ul><ul><li>Part-2 days holiday,wdays; </li></ul><ul><li>The first part defines an enumeration named days . The second part declares the variables holiday and wdays to be enumeration variable of type days. Thus each variable can be assigned any one of the constants mon,tue,wed,thu,fri,sat,sun. </li></ul>
  5. 5. <ul><li>The two parts in example-2 can be combined if desired, resulting in </li></ul><ul><li>enum days </li></ul><ul><li>{ </li></ul><ul><li>mon,tue,wed,thu,fri,sat,sun; </li></ul><ul><li>} holiday,wdays; </li></ul><ul><li>or without the name(tag) , simply </li></ul><ul><li>enum </li></ul><ul><li>{ </li></ul><ul><li>mon,tue,wed,thu,fri,sat,sun; </li></ul><ul><li>} holiday,wdays; </li></ul>
  6. 6. <ul><li>Enumeration constants are automatically assigned equivalent integer values, beginning with 0 for the first constant, with each successive constant increasing by 1. Therefore in example-1, the enumeration constants will represent the following integer values: - </li></ul><ul><li>Mon 0 </li></ul><ul><li>Tue 1 </li></ul><ul><li>Wed 2 </li></ul><ul><li>Thu 3 </li></ul><ul><li>Fri 4 </li></ul><ul><li>Sat 5 </li></ul><ul><li>Sun 6 </li></ul>
  7. 7. <ul><li>These automatic assignments can be overridden by assigning explicit integer values which differ from the default values. Those constants are not assigned explicit values will automatically be assigned values which increase successively by 1 from the last explicit assignment. </li></ul><ul><li>enum days </li></ul><ul><li>{ </li></ul><ul><li>mon=10; </li></ul><ul><li>tue=11; </li></ul><ul><li>wed=12; </li></ul><ul><li>thu=13 </li></ul><ul><li>fri=20; </li></ul><ul><li>sat=21; </li></ul><ul><li>sun=50; </li></ul><ul><li>}; </li></ul>
  8. 8. Operations on Enumerated Data Types <ul><li>Assignment </li></ul><ul><ul><li>example : - holiday=sun; </li></ul></ul><ul><li>Comparing using Relational Operators </li></ul><ul><li>Example : - </li></ul><ul><li>Consider the enumerated data types defined in </li></ul><ul><li>previous example: - </li></ul><ul><li>expression values </li></ul><ul><li>sun<tue false </li></ul><ul><li>mon!=fri true </li></ul><ul><li>mon<sat sat </li></ul>
  9. 9. Advantages of Enumerated Data Types <ul><li>Simplifies the Program. </li></ul><ul><li>enhances the readability of the program. </li></ul><ul><li>Help to locate more errors at the compile time. </li></ul><ul><li>Helps us to express a program in a more natural way. </li></ul>
  10. 10. Redefining Data Types with typedef <ul><li>In c, it is possible to redefine the built-in as well as user defined data types. This task is accomplished by the typedef statement. </li></ul><ul><li>Syntax </li></ul><ul><li>typedef type new_type; </li></ul><ul><li>where typedef is keyword, type is either built-in data type or user-defined data type, and new_type is the new name of the type. </li></ul><ul><li>Example: - </li></ul><ul><li>typedef float real; </li></ul><ul><li>This statement redefines the data types float to real. Then to declare x,y and z of type float, we can also write </li></ul><ul><li>real x,y,z; </li></ul><ul><li>The compiler will still recognize the statement </li></ul><ul><li>float x,y,z; </li></ul><ul><li>as correct. </li></ul>