Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103."— Presentation transcript:

1 ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

2 Syllabus Typedef Enum Type Example

3 2 Typedef The typedef statement allows a programmer to provide a synonym (or alias) for either a built-in or user-defined data type. Definition: typedef datatype NewName;  datatype is the existing name of the built-in or user-defined data type.  NewName is the new alias name.  A semicolon is needed at the end.

4 3 The original data type is not changed in any way and can still be used normally in a program. typedef statements are often defined globally so that all functions have access to the alias. Reasons for using typedef :  To provide a more descriptive name for an existing data type that clarifies its intended use  To provide a shorter name for an existing data type  To promote code portability

5 4 Example: /* Variable declarations – original version */ int player_hp; unsigned long long int x; char * sptr; /* Typedef definitions */ typedef int healthpoints; typedef unsigned long long int uLLong; typedef char * pstring; /* Variable declarations – typedef version */ healthpoints player_hp; uLLong x; pstring sptr; /* Potentially ambiguous */

6 5 Example: #include /* Brand of computer */ #define BRAND_A #ifdef BRAND_A typedef short int INTEGER; #endif #ifdef BRAND_B typedef int INTEGER; #endif int main (void) { INTEGER x; printf("%d\n", sizeof(x)); return 0; }

7 6 Example: #include typedef char * pstring; void disp_msg (pstring s) { printf("Message: %s\n", s); } int main (void) { char str[20] = "Good night."; pstring s = str; disp_msg("Get some sleep!"); disp_msg(s); return 0; }

8 7 Enumerated Data Types An enumerated type is a data type in which the programmer specifies the allowed values. Definition: enum tag {Label1, Label2, …};  tag is the name of the enumerated type. Each label defines an allowed value.  Braces delimit the body of the enumeration.  A semicolon is needed after the ending brace.

9 8 Labels do not need quotation marks – they are not strings. Example: enum console {WII_U,XBOX_ONE,PS4}; Once a label is defined, it cannot be used again in a different enumerated type definition. Enumerated types are often defined globally so that all functions have access to the definitions.

10 9 An enumerated type associates an integer constant with each label.  By default, the first value is associated with 0, the second with 1, and so on.  The programmer can directly set which integers are associated with the label values. Declaration for a variable of an enumerated type: enum tag variable_name; Once an enumerated variable is declared, it is assigned a value using the assignment operator.

11 10 Example: /* Enumerated type definitions */ enum boolean {false, true}; enum video {VHS, DVD, BLURAY, STREAMED}; /* Variable declarations */ enum boolean state; enum video vtype; /* Assignments */ state = true; vtype = BLURAY;

12 11 A typedef can alias away the “ enum ” part to make enumerated declarations look like a native type. Example: /* Enumerated type definitions (with typedef) */ typedef enum colors {red, green, blue} color_t; typedef enum error_codes {ERR_MATH=100, ERR_FILE=200} ERROR; /* Variable declarations */ color_t cvalue; ERROR ecode; /* Variable assignments */ cvalue = blue; ecode = ERR_FILE;

13 12 If a typedef is combined with an enumeration definition, the tag is optional. Example: typedef enum {red, green, blue} color; /* no tag */ typedef enum error_codes {ERR_MATH=100, ERR_FILE=200} ERROR; /* Variable declarations */ color cvalue; /* OK */ ERROR ecode; /* OK */ Note: If the tag is omitted, then enumerated variable declarations must also omit the enum keyword. Example: enum color cvalue; /* Causes compiler error */ enum error_codes ecode; /* Still legal */

14 13 printf() displays the value of an enumerated variable as an integer, not as the label. Example: #include enum school {UO, OSU, PSU}; int main (void) { enum school university; for (university = UO; university <= PSU; university++) if (university == PSU) printf("%d PSU\n", university); else if (university == OSU) printf("%d OSU\n", university); else printf("%d UO\n", university); return 0; } Actual Output: 0 UO 1 OSU 2 PSU

15 14 Example: C90 code to simulate a Boolean type #include typedef enum boolean {false, true} boolean_t; int main (void) { boolean_t done = false; int c; while (!done) { c = getchar(); if (c == '@') done = true; else printf("%c", c); } return 0; }


Download ppt "ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103."

Similar presentations


Ads by Google