1 / 15

ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type

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. Syllabus. Typedef Enum Type Example. Typedef

babu
Download Presentation

ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 103 Engineering ProgrammingChapter 48Typedef 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. 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 datatypeNewName; 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. 2

  4. 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 3

  5. 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 */ 4

  6. Example: #include <stdio.h> /* 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; } 5

  7. Example: #include <stdio.h> 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; } 6

  8. Enumerated Data Types An enumerated type is a data type in which the programmer specifies the allowed values. Definition: enumtag {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. 7

  9. 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. 8

  10. 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: enumtagvariable_name; Once an enumerated variable is declared, it is assigned a value using the assignment operator. 9

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

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

  13. If a typedefis combined with an enumeration definition, the tag is optional. Example: typedefenum{red, green, blue} color; /* no tag */ typedefenumerror_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 */ enumerror_codesecode; /* Still legal */ 12

  14. printf()displays the value of an enumerated variable as an integer, not as the label. Example: #include <stdio.h> 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 13

  15. Example: C90 code to simulate a Boolean type #include <stdio.h> typedefenumboolean {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; } 14

More Related