• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
User defined data type
 

User defined data type

on

  • 1,807 views

 

Statistics

Views

Total Views
1,807
Views on SlideShare
1,807
Embed Views
0

Actions

Likes
0
Downloads
9
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    User defined data type User defined data type Presentation Transcript

    • User Defined Data Type “ Enumerated Data Type”
    • Enumerated Data Type ( enum )
      • 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:-
      • enum name
      • {
      • value-1,value-2,value-3,…….,value-4;
      • };
      • 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.
      • Example (1) : -
      • enum fruit
      • {
      • apple,orange,mango,pineapple;
      • };
      • 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 :-
      • fruit a,b;
      • Now, the variable a and b are of type fruit and can assume value from the list : (apple, orange, mango, pineapple).
      • Example (2) : -
      • Part-1 enum days
      • {
      • mon,tue,wed,thu,fri,sat,sun;
      • };
      • Part-2 days holiday,wdays;
      • 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.
      • The two parts in example-2 can be combined if desired, resulting in
      • enum days
      • {
      • mon,tue,wed,thu,fri,sat,sun;
      • } holiday,wdays;
      • or without the name(tag) , simply
      • enum
      • {
      • mon,tue,wed,thu,fri,sat,sun;
      • } holiday,wdays;
      • 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: -
      • Mon 0
      • Tue 1
      • Wed 2
      • Thu 3
      • Fri 4
      • Sat 5
      • Sun 6
      • 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.
      • enum days
      • {
      • mon=10;
      • tue=11;
      • wed=12;
      • thu=13
      • fri=20;
      • sat=21;
      • sun=50;
      • };
    • Operations on Enumerated Data Types
      • Assignment
        • example : - holiday=sun;
      • Comparing using Relational Operators
      • Example : -
      • Consider the enumerated data types defined in
      • previous example: -
      • expression values
      • sun<tue false
      • mon!=fri true
      • mon<sat sat
    • Advantages of Enumerated Data Types
      • Simplifies the Program.
      • enhances the readability of the program.
      • Help to locate more errors at the compile time.
      • Helps us to express a program in a more natural way.
    • Redefining Data Types with typedef
      • 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.
      • Syntax
      • typedef type new_type;
      • 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.
      • Example: -
      • typedef float real;
      • This statement redefines the data types float to real. Then to declare x,y and z of type float, we can also write
      • real x,y,z;
      • The compiler will still recognize the statement
      • float x,y,z;
      • as correct.