Chapter 10 - C Structures, Unions, Bit Manipulations, and Enumerations
Chapter 10 - C Structures, Unions, Bit Manipulations, and Enumerations
Outline
10.1 10.2 10.3 10.4 10.5 10.6 10.7 10.8 10.9 10.10 10.11 Introduction Structure Definitions Initializing Structures Accessing Members of Structures Using Structures with Functions typedef Example: High-Performance Card Shuffling and Dealing Simulation Unions Bitwise Operators Bit Fields Enumeration Constants
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Objectives
In this tutorial, you will learn:
To be able to create and use structures, unions and enumerations. To be able to pass structures to functions call by value and call by reference. To be able to manipulate data with the bitwise operators. To be able to create bit fields for storing data compactly.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
10.1 Introduction
Structures
Collections of related variables (aggregates) under one name
Can contain variables of different data types
Commonly used to define records to be stored in files Combined with pointers, can create linked lists, stacks, queues, and trees
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
struct introduces the definition for structure card card is the structure name and is used to declare variables
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Definitions
Defined like other variables:
card oneCard, deck[ 52 ], *cPtr;
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Assignment statements
Example:
card threeHearts = oneCard;
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
myCardPtr->suit is equivalent to
( *myCardPtr ).suit
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9
/* Fig. 10.2: fig10_02.c Using the structure member and structure pointer operators */ #include <stdio.h> /* card structure definition */ struct card { char *face; /* define pointer face */ char *suit; /* define pointer suit */
10
Outline
fig10_02.c (Part 1 of 2)
10 }; /* end structure card */ 11 12 int main() 13 { 14 15 16 17 18 19 20 21 22 aPtr = &a; /* assign address of a to aPtr */ /* place strings into card structures */ a.face = "Ace"; a.suit = "Spades"; struct card a; /* define struct a */ struct card *aPtr; /* define a pointer to card */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
23 24 25 26 27 28
printf( "%s%s%s\n%s%s%s\n%s%s%s\n", a.face, " of ", a.suit, aPtr->face, " of ", aPtr->suit, ( *aPtr ).face, " of ", ( *aPtr ).suit ); return 0; /* indicates successful termination */
11
Outline
fig10_02.c (Part 2 of 2)
29 } /* end main */
Program Output
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
12
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
13
10.6 typedef
typedef
Creates synonyms (aliases) for previously defined data types Use typedef to create shorter type names Example:
typedef struct Card *CardPtr;
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
14
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9 10
/* Fig. 10.3: fig10_03.c The card shuffling and dealing program using structures */ #include <stdio.h> #include <stdlib.h> #include <time.h> /* card structure definition */ struct card { const char *face; /* define pointer face */ const char *suit; /* define pointer suit */
15
Outline
fig10_03.c (Part 1 of 4)
11 }; /* end structure card */ 12 13 typedef struct card Card; 14 15 /* prototypes */ 16 void fillDeck( Card * const wDeck, const char * wFace[], 17 const char * wSuit[] ); 18 void shuffle( Card * const wDeck ); 19 void deal( const Card * const wDeck ); 20 21 int main() 22 { 23 24 Card deck[ 52 ]; /* define array of Cards */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/* initialize array of pointers */ const char *face[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"}; /* initialize array of pointers */ const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"}; srand( time( NULL ) ); /* randomize */ fillDeck( deck, face, suit ); /* load the deck with Cards */ shuffle( deck ); /* put Cards in random order */ deal( deck ); /* deal all 52 Cards */ return 0; /* indicates successful termination */
16
Outline
fig10_03.c (Part 2 of 4)
41 } /* end main */ 42 43 /* place strings into Card structures */ 44 void fillDeck( Card * const wDeck, const char * wFace[], 45 46 { 47 48 int i; /* counter */ const char * wSuit[] )
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
49 50 51 52 53 54
/* loop through wDeck */ for ( i = 0; i <= 51; i++ ) { wDeck[ i ].face = wFace[ i % 13 ]; wDeck[ i ].suit = wSuit[ i / 13 ]; } /* end for */
17
Outline
fig10_03.c (3 of 4)
55 } /* end function fillDeck */ 56 57 /* shuffle cards */ 58 void shuffle( Card * const wDeck ) 59 { 60 61 62 63 64 65 66 67 68 69 70 71 72 } /* end function shuffle */ 73 /* loop through wDeck randomly swapping Cards */ for ( i = 0; i <= 51; i++ ) { j = rand() % 52; temp = wDeck[ i ]; wDeck[ i ] = wDeck[ j ]; wDeck[ j ] = temp; } /* end for */ int i; int j; /* counter */ /* variable to hold random value between 0 - 51 */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
74 /* deal cards */ 75 void deal( const Card * const wDeck ) 76 { 77 78 79 80 81 82 83 84 85 } /* end function deal */ /* loop through wDeck */ for ( i = 0; i <= 51; i++ ) { printf( "%5s of %-8s%c", wDeck[ i ].face, wDeck[ i ].suit, ( i + 1 ) % 2 ? '\t' : '\n' ); } /* end for */ int i; /* counter */
18
Outline
fig10_03.c (4 of 4)
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Four Three Four Nine Three Eight Deuce Seven Ace Ace Seven Eight Five Queen Queen Jack Eight King Eight Ace Four Deuce Deuce Seven King Ten
of of of of of of of of of of of of of of of of of of of of of of of of of of
Clubs Diamonds Diamonds Hearts Clubs Clubs Clubs Clubs Clubs Spades Diamonds Spades Spades Spades Diamonds Diamonds Hearts Spades Diamonds Hearts Spades Hearts Spades Spades Clubs Hearts
Three Three Ace Ten Four Nine Queen Jack Five Five Six Queen Deuce Six Seven Nine Five Six Ten King Jack Jack Ten Nine Six King
of of of of of of of of of of of of of of of of of of of of of of of of of of
Hearts Spades Diamonds Clubs Hearts Diamonds Clubs Spades Diamonds Clubs Spades Hearts Diamonds Hearts Hearts Spades Hearts Clubs Spades Hearts Hearts Clubs Diamonds Clubs Diamonds Diamonds
19
Outline
Program Output
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
20
10.8 Unions
union
Memory that contains a variety of objects over time Only contains one data member at a time Members of a union share space Conserves storage Only the last data member defined can be accessed
union definitions
Same as struct
union Number { int x; float y; }; union Number value;
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
21
10.8 Unions
Valid union operations
Assignment to union of same type: = Taking address: & Accessing union members: . Accessing members using pointers: ->
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9 10
/* Fig. 10.5: fig10_05.c An example of a union */ #include <stdio.h> /* number union definition */ union number { int x; /* define int x */ double y; /* define double y */ }; /* end union number */
22
Outline
fig10_05.c (1 of 2)
11 int main() 12 { 13 14 15 16 17 18 19 20 21 value.x = 100; /* put an integer into the union */ printf( "%s\n%s\n%s%d\n%s%f\n\n", "Put a value in the integer member", "and print both members.", "int: ", value.x, "double:\n", value.y ); union number value; /* define union value */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
22 23 24 25 26 27 28 29 30
value.y = 100.0; /* put a double into the same union */ printf( "%s\n%s\n%s%d\n%s%f\n", "Put a value in the floating member", "and print both members.", "int: ", value.x,
23
Outline
fig10_05.c (2 of 2)
31 } /* end main */
Put a value in the integer member and print both members. int: 100 double: -92559592117433136000000000000000000000000000000000000000000000.000000
Put a value in the floating member and print both members. int: 0 double: 100.000000
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
24
bitwise AND bitwise inclusive OR bitwise exclusive OR left shift right shift
Description The bits in the result are set to 1 if the corresponding bits in the two operands are both 1. The bits in the result are set to 1 if at least one of the corresponding bits in the two operands is 1. The bits in the result are set to 1 if exactly one of the corresponding bits in the two operands is 1. Shifts the bits of the first operand left by the number of bits specified by the second operand; fill from the right with 0 bits. Shifts the bits of the first operand right by the number of bits specified by the second operand; the method of filling from the left is machine dependent.
Fig. 10.6
ones complement All 0 bits are set to 1 and all 1 bits are set to 0. The bitwise operators.
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Fig. 10.7: fig10_07.c Printing an unsigned integer in bits */ #include <stdio.h> void displayBits( unsigned value ); /* prototype */ int main() { unsigned x; /* variable to hold user input */ printf( "Enter an unsigned integer: " ); scanf( "%u", &x ); displayBits( x ); return 0; /* indicates successful termination */
25
Outline
fig10_07.c (1 of 2)
18 } /* end main */ 19 20 /* display bits of an unsigned integer value */ 21 void displayBits( unsigned value ) 22 { 23 24 unsigned c; /* counter */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/* define displayMask and left shift 31 bits */ unsigned displayMask = 1 << 31; printf( "%7u = ", value ); /* loop through bits */ for ( c = 1; c <= 32; c++ ) { putchar( value & displayMask ? '1' : '0' ); value <<= 1; /* shift value left by 1 */ if ( c % 8 == 0 ) { /* output space after 8 bits */ putchar( ' ' ); } /* end if */ } /* end for */ putchar( '\n' );
26
Outline
fig10_07.c (2 of 2)
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
27
0 1 0 1
Fig. 10.8
0 0 1 1
Results of combining two bits with the bitwise AND operator &.
0 0 0 1
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* Fig. 10.9: fig10_09.c Using the bitwise AND, bitwise inclusive OR, bitwise exclusive OR and bitwise complement operators */ #include <stdio.h> void displayBits( unsigned value ); /* prototype */ int main() { unsigned number1; /* define number1 */ unsigned number2; /* define number2 */ unsigned mask; /* define mask */ unsigned setBits; /* define setBits */ /* demonstrate bitwise & */ number1 = 65535; mask = 1; printf( "The result of combining the following\n" ); displayBits( number1 ); displayBits( mask ); printf( "using the bitwise AND operator & is\n" ); displayBits( number1 & mask );
28
Outline
fig10_09.c (1 of 4)
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/* demonstrate bitwise | */ number1 = 15; setBits = 241; printf( "\nThe result of combining the following\n" ); displayBits( number1 ); displayBits( setBits ); printf( "using the bitwise inclusive OR operator | is\n" ); displayBits( number1 | setBits ); /* demonstrate bitwise exclusive OR */ number1 = 139; number2 = 199; printf( "\nThe result of combining the following\n" ); displayBits( number1 ); displayBits( number2 ); printf( "using the bitwise exclusive OR operator ^ is\n" ); displayBits( number1 ^ number2 ); /* demonstrate bitwise complement */ number1 = 21845; printf( "\nThe one's complement of\n" ); displayBits( number1 ); printf( "is\n" ); displayBits( ~number1 );
29
Outline
fig10_09.c (2 of 4)
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
49 50
30
Outline
fig10_09.c (3 of 4)
51 } /* end main */ 52 53 /* display bits of an unsigned integer value */ 54 void displayBits( unsigned value ) 55 { 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 } /* end for */ if ( c % 8 == 0 ) { /* output a space after 8 bits */ putchar( ' ' ); } /* end if */ /* loop through bits */ for ( c = 1; c <= 32; c++ ) { putchar( value & displayMask ? '1' : '0' ); value <<= 1; /* shift value left by 1 */ printf( "%10u = ", value ); /* declare displayMask and left shift 31 bits */ unsigned displayMask = 1 << 31; unsigned c; /* counter */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
74
putchar( '\n' );
31
Outline
fig10_09.c (4 of 4) Program Output
The result of combining the following 65535 = 00000000 00000000 11111111 11111111 1 = 00000000 00000000 00000000 00000001 using the bitwise AND operator & is 1 = 00000000 00000000 00000000 00000001
The result of combining the following 15 = 00000000 00000000 00000000 241 = 00000000 00000000 00000000 using the bitwise inclusive OR operator 255 = 00000000 00000000 00000000 The result of combining the following 139 = 00000000 00000000 00000000 199 = 00000000 00000000 00000000 using the bitwise exclusive OR operator 76 = 00000000 00000000 00000000
The one's complement of 21845 = 00000000 00000000 01010101 01010101 is 4294945450 = 11111111 11111111 10101010 10101010
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
32
Bit 2
0 0 1 1
Bit 1 | Bit 2
0 1 1 1
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
33
Bit 2
0 0 1 1
Bit 1 ^ Bit 2
0 1 1 0
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* Fig. 10.13: fig10_13.c Using the bitwise shift operators */ #include <stdio.h> void displayBits( unsigned value ); /* prototype */ int main() { unsigned number1 = 960; /* initialize number1 */ /* demonstrate bitwise left shift */ printf( "\nThe result of left shifting\n" ); displayBits( number1 ); printf( "8 bit positions using the " ); printf( "left shift operator << is\n" ); displayBits( number1 << 8 ); /* demonstrate bitwise right shift */ printf( "\nThe result of right shifting\n" ); displayBits( number1 ); printf( "8 bit positions using the " ); printf( "right shift operator >> is\n" ); displayBits( number1 >> 8 );
34
Outline
fig10_13.c (1 of 2)
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
25 26
35
27 } /* end main */ 28 29 /* display bits of an unsigned integer value */ 30 void displayBits( unsigned value ) 31 { 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 putchar( '\n' ); 51 } /* end function displayBits */ } /* end for */ if ( c % 8 == 0 ) { /* output a space after 8 bits */ putchar( ' ' ); } /* end if */ /* loop through bits */ for ( c = 1; c <= 32; c++ ) { putchar( value & displayMask ? '1' : '0' ); value <<= 1; /* shift value left by 1 */ printf( "%7u = ", value ); /* declare displayMask and left shift 31 bits */ unsigned displayMask = 1 << 31; unsigned c; /* counter */
Outline
fig10_13.c (2 of 2)
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
The result of left shifting 960 = 00000000 00000000 00000011 11000000 8 bit positions using the left shift operator << is 245760 = 00000000 00000011 11000000 00000000 The result of right shifting 960 = 00000000 00000000 00000011 11000000 8 bit positions using the right shift operator >> is 3 = 00000000 00000000 00000000 00000011
36
Outline
Program Output
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
37
Bitwise AND assignment operator. Bitwise inclusive OR assignment operator. Bitwise exclusive OR assignment operator. Left-shift assignment operator. Right-shift assignment operator. The bitwise assignment operators.
Fig. 10.14
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
38
Associativity left to right right to left left to right left to right left to right left to right left to right left to right left to right left to right left to right left to right right to left
Type Highest Unary multiplicative additive shifting relational equality bitwise AND bitwise OR bitwise OR logical AND logical OR conditional assignment comma
Fig. 10.15
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
39
40
Unnamed bit field with zero width aligns next bit field to a new storage unit boundary
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9 10
/* Fig. 10.16: fig10_16.c Representing cards with bit fields in a struct */ #include <stdio.h> /* bitCard structure definition with bit fields */ struct bitCard { unsigned face : 4; unsigned suit : 2; /* 4 bits; 0-15 */ /* 2 bits; 0-3 */
41
Outline
fig10_16.c (1 of 3)
11 }; /* end struct bitCard */ 12 13 typedef struct bitCard Card; 14 15 void fillDeck( Card * const wDeck ); /* prototype */ 16 void deal( const Card * const wDeck ); /* prototype */ 17 18 int main() 19 { 20 21 22 23 24 25 26 return 0; /* indicates successful termination */ fillDeck( deck ); deal( deck ); Card deck[ 52 ]; /* create array of Cards */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
27 } /* end main */ 28 29 /* initialize Cards */ 30 void fillDeck( Card * const wDeck ) 31 { 32 33 34 35 36 37 38 39 40 41 } /* end function fillDeck */ 42 43 /* output cards in two column format; cards 0-25 subscripted with 44 46 { 47 48 49 int k1; /* subscripts 0-25 */ int k2; /* subscripts 26-51 */ k1 (column 1); cards 26-51 subscripted k2 (column 2) */ 45 void deal( const Card * const wDeck ) /* loop through wDeck */ for ( i = 0; i <= 51; i++ ) { wDeck[ i ].face = i % 13; wDeck[ i ].suit = i / 13; wDeck[ i ].color = i / 26; } /* end for */ int i; /* counter */
42
Outline
fig10_16.c (2 of 3)
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
50 51 52 53 54 55 56 57
/* loop through wDeck */ for ( k1 = 0, k2 = k1 + 26; k1 <= 25; k1++, k2++ ) { printf( "Card:%3d printf( "Card:%3d } /* end for */ Suit:%2d Suit:%2d Color:%2d ", wDeck[ k1 ].face, wDeck[ k1 ].suit, wDeck[ k1 ].color ); Color:%2d\n", wDeck[ k2 ].face, wDeck[ k2 ].suit, wDeck[ k2 ].color );
43
Outline
fig10_16.c (3 of 3)
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card:
0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12
Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit:
0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1
Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card: Card:
0 1 2 3 4 5 6 7 8 9 10 11 12 0 1 2 3 4 5 6 7 8 9 10 11 12
Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit: Suit:
2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3
Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color: Color:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
44
Outline
Program Output
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
45
Example:
enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC};
Creates a new type enum Months in which the identifiers are set to the integers 1 to 12
Enumeration variables can only assume their enumeration constant values (not the integer representations)
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9
/* Fig. 10.18: fig10_18.c Using an enumeration type */ #include <stdio.h> /* enumeration constants represent months of the year */ enum months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC }; int main() enum months month; /* can contain any of the 12 months */ /* initialize array of pointers */ const char *monthName[] = { "", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; /* loop through months */ for ( month = JAN; month <= DEC; month++ ) { printf( "%2d%11s\n", month, monthName[ month ] ); } /* end for */ return 0; /* indicates successful termination */
46
Outline
fig10_18.c
10 { 11 12 13 14 15 16 17 18 19 20 21 22 23 24 } /* end main */
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 2 3 4 5 6 7 8 9 10 11 12
January February March April May June July August September October November December
47
Outline
Program Output
Copyright 19922004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.