15
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

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

Embed Size (px)

Citation preview

Page 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

ECE 103 Engineering ProgrammingChapter 48

Typedef and Enum Type

Herbert G. Mayer, PSU CSStatus 6/4/2014

Initial content copied verbatim fromECE 103 material developed by

Professor Phillip Wong @ PSU ECE

Page 2: 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

Syllabus Typedef Enum Type Example

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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 */

Page 6: 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

6

Example:#include <stdio.h>

/* Brand of computer */#define BRAND_A

#ifdef BRAND_Atypedef short int INTEGER;#endif

#ifdef BRAND_Btypedef int INTEGER;#endif

int main (void){

INTEGER x;printf("%d\n", sizeof(x));return 0;

}

Page 7: 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

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;

}

Page 8: 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

8

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.

Page 9: 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

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.

Page 10: 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

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:

enum tag variable_name;

Once an enumerated variable is declared, it is assigned a value using the assignment operator.

Page 11: 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

11

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;

Page 12: 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

12

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;

Page 13: 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

13

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 */

Page 14: 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

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 UO1 OSU2 PSU

Page 15: 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

15

Example: C90 code to simulate a Boolean type#include <stdio.h>

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;

}