Presentation is loading. Please wait.

Presentation is loading. Please wait.

CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.

Similar presentations


Presentation on theme: "CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR."— Presentation transcript:

1 CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR

2 Outline Enumerated Data Types The typedef Statement 2

3 Enumerated Data Types An enumerated data type is one of the type of the variables that specifies the valid values that could be stored into that variable. For example, suppose you had a variable called myColor and we wanted to use it to store one of the primary colors, red, yellow, or blue, and no other values. This type of capability is provided by the enumerated data type. An enumerated data type definition is initiated by the keyword enum followed by the name of the enumerated data type, and then followed by a list of identifiers (enclosed in a set of curly braces) that define the possible values that can be assigned to the type. For example, the statement: enum primaryColor { red, yellow, blue }; defines a data type primaryColor. 3

4 Variables declared to be of primaryColor data type can be assigned the values red, yellow, and blue inside the program, and no other values. An attempt to assign another value to such a variable causes some compilers to issue an error message. To declare a variable to be of type enum primaryColor, you again use the keyword enum, followed by the enumerated type name, followed by the variable list. So the statement: enum primaryColor myColor, gregsColor; Defines the two variables myColor and gregsColor to be of type primaryColor.The only possible values that can be assigned to these variables are the names red, yellow, and blue. So the following two statements are valid: myColor = red; and if ( gregsColor == yellow )... 4 Enumerated Data Types

5 5 As another example of an enumerated data type definition, the following defines the type enum month, with possible values that can be assigned to a variable of this type being the months of the year: enum month { january, february, march, april, may, june, july, august, september, october, november, december }; The C compiler actually treats enumeration identifiers as integer constants. Beginning with the first name in the list, the compiler assigns sequential integer values to these names, starting with 0. If your program contains these two lines: enum month thisMonth;... thisMonth = february; the value 1 is assigned to thisMonth (and not the name february ) because it is the second identifier listed inside the enumeration list. Enumerated Data Types

6 If you want to have a specific integer value associated with an enumeration identifier, the integer can be assigned to the identifier when the data type is defined. Enumeration identifiers that subsequently appear in the list are assigned sequential integer values beginning with the specified integer value plus 1. For example, in the definition enum direction { up, down, left = 10, right }; an enumerated data type direction is defined with the values up, down, left, and right. The compiler assigns the values to the identifiers as follows: 0 to up because it appears first in the list; 1 to down because it appears next; 10 to left because it is explicitly assigned this value; and 11 to right because it appears immediately after left in the list. Enumerated Data Types 6

7 7 // Program to print the number of days in a month #include int main (void) { enum month { january = 1, february, march, april, may, june, july, august, september, october, november, december }; enum month aMonth; int days; printf (“Enter month number: “); scanf (“%i”, &aMonth); switch (aMonth ) { case january: case march: case may: case july: case august: case october: case december: days = 31; break; the value 1 is assigned to the first identifier. So the compiler will not assigns 0 to it

8 8 case april: case june: case september: case november: days = 30; break; case february: days = 28; break; default: printf (“bad month number\n”); days = 0; break; } if ( days != 0 ) printf (“Number of days is %i\n”, days); if ( amonth == february ) printf (“...or 29 if it’s a leap year\n”); return 0; } Output: Enter month number: 5 Number of days is 31 Output (Rerun): Enter month number: 2 Number of days is 28...or 29 if it’s a leap year

9 9 Enumeration identifiers can share the same value. For example, in enum switch { no=0, off=0, yes=1, on=1 }; the identifiers no and off to an enum switch variable both have the value 0. And the identifiers yes and on both have the value 1. The variations permitted when defining an enumerated data type are similar to those permitted with structure definitions: The name of the data type can be omitted, and variables can be declared to be of the particular enumerated data type when the type is defined. As an example showing both of these options, the statement enum { east, west, south, north } direction; defines an (unnamed) enumerated data type with values east, west, south, or north, and declares a variable direction to be of that type. Enumerated Data Types

10 10 The typedef Statement C provides a capability that enables you to assign an alternate name to a data type. This is done with a statement known as typedef. The statement: typedef int Counter; defines the name Counter to be equivalent to the C data type int. Variables can subsequently be declared to be of type Counter, as in the following statement: Counter j, n; The C compiler actually treats the declaration of the variables j and n as normal integer variables. The main advantage of the use of the typedef in this case is in the added readability that it lends to the definition of the variables. It is clear from the definition of j and n what the intended purpose of these variables is in the program.

11 11 The typedef Statement In many instances, a typedef statement can be equivalently substituted by the appropriate #define statement. For example, you could have instead used the statement #define Counter int to achieve the same results as the preceding statement. However, because the typedef is handled by the C compiler proper, and not by the preprocessor, the typedef statement provides more flexibility than does the #define when it comes to assigning names to derived data types.

12 12 The typedef Statement For example, the following statement: typedef char Linebuf [81]; defines a type called Linebuf, which is an array of 81 characters. So, now we can declare variables to be of type Linebuf, as follows: Linebuf text, inputLine; has the effect of defining the variables text and inputLine to be arrays containing 81 characters. This is equivalent to the following declaration: char text[81], inputLine[81]; Note that, in this case, Linebuf could not have been equivalently defined with a #define preprocessor statement.

13 13 The typedef Statement The following typedef defines a type name StringPtr to be a char pointer: typedef char *StringPtr; Variables subsequently declared to be of type StringPtr, as follows: StringPtr buffer; are treated as character pointers by the C compiler. So, to define a new type name with typedef, follow these steps: 1.Write the statement as if a variable of the desired type were being declared. 2.Where the name of the declared variable would normally appear, substitute the new type name. 3.In front of everything, place the keyword typedef.

14 14 The typedef Statement As an example of this procedure, to define a type called Date to be a structure containing three integer members called month, day, and year, we write out the structure definition, substituting the name Date where the variable name would normally appear (before the last semicolon). Before everything, you place the keyword typedef as follows: typedef struct { int month; int day; int year; } Date; With this typedef in place, we can subsequently declare variables to be of type Date, as in: Date birthdays[100]; This defines birthdays to be an array containing 100 Date structures.


Download ppt "CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR."

Similar presentations


Ads by Google