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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

ECE 103 Engineering Programming Chapter 54 Recursion Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
1 CS 161 Introduction to Programming and Problem Solving Chapter 9 C++ Program Components Herbert G. Mayer, PSU Status 10/20/2014.
Principles of Programming Fundamental of C Programming Language and Basic Input/Output Function 1.
True or false A variable of type char can hold the value 301. ( F )
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Data Types.
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
CCSA 221 Programming in C CHAPTER 14 MORE ON DATA TYPES 1 ALHANOUF ALAMR.
Functions in C. Consider #include main() { int i; for(i=1; i
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
C STRUCTURES. A FIRST C PROGRAM  #include  void main ( void )  { float height, width, area, wood_length ;  scanf ( "%f", &height ) ;  scanf ( "%f",
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
ECE 103 Engineering Programming Chapter 9 gcc Compiler Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 8: User-Defined Simple Data Types, Namespaces, and the string Type.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
ECE 103 Engineering Programming Chapter 53 Generic Algorithms Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
CS 161 Introduction to Programming and Problem Solving Chapter 18 Control Flow Through C++ Program Herbert G. Mayer, PSU Status 10/8/2014 Initial content.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Chapter2 Constants, Variables, and Data Types. 2.1 Introduction In this chapter, we will discuss –constants (integer, real, character, string, enum),symbolic.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
ECE 103 Engineering Programming Chapter 29 C Strings, Part 2 Herbert G. Mayer, PSU CS Status 7/30/2014 Initial content copied verbatim from ECE 103 material.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
C++ Lesson 1.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Computing and Statistical Data Analysis Lecture 2
Programmazione I a.a. 2017/2018.
Introduction to C Programming
Data Types Chapter 8.
Lecture 8.
ECE 103 Engineering Programming Chapter 32 Array Parameters
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
ECE 103 Engineering Programming Chapter 18 Iteration
ECE 103 Engineering Programming Chapter 22 Selection
Variables and Constants
Presentation transcript:

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 PSU ECE

Syllabus Typedef Enum Type Example

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.

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

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

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

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

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.

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.

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.

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;

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;

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

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

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