ccsa 221 programming in c chapter 14 more on data types 1 alhanouf alamr

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

Upload: augustine-kelley

Post on 12-Jan-2016

224 views

Category:

Documents


1 download

TRANSCRIPT

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

CCSA 221Programming in CCHAPTER 14

MORE ON DATA TYPES

1ALHANOUF ALAMR

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

Outline• Enumerated Data Types

• The typedef Statement

2

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

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

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

• 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;

andif ( gregsColor == yellow )...

4

Enumerated Data Types

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

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

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

• 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; and11 to right because it appears immediately after left in the list.

Enumerated Data Types

6

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

7

// Program to print the number of days in a month #include <stdio.h>

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

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

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: 5Number of days is 31

Output (Rerun):

Enter month number: 2Number of days is 28...or 29 if it’s a leap year

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

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, andvariables 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 statementenum { 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

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

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.

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

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

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

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.

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

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:

• Write the statement as if a variable of the desired type were being declared.• Where the name of the declared variable would normally appear, substitute the

new type name.• In front of everything, place the keyword typedef .

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

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.