Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
Arrays H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 13, 2011) Washington State University.
Modular Programming (2) H&K Chapter 6 Instructor – Gokcen Cilingir Cpt S 121 (July 8, 2011) Washington State University.
Strings (II) H&K Chapter 9 Instructor – Gokcen Cilingir Cpt S 121 (July 20, 2011) Washington State University.
Iteration in C H&K Chapter 5 Instructor – Gokcen Cilingir Cpt S 121 (July 1, 2011) Washington State University.
Arrays (III) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 15, 2011) Washington State University.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
Selection structures in C (II) H&K Chapter 4 Instructor – Gokcen Cilingir Cpt S 121 (June 30, 2011) Washington State University.
Arrays (II) H&K Chapter 8 Instructor – Gokcen Cilingir Cpt S 121 (July 14, 2011) Washington State University.
Dynamic Data Structures H&K Chapter 14 Instructor – Gokcen Cilingir Cpt S 121 (July 26, 2011) Washington State University.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Variables and constants Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Multidimensional Arrays C++ also allows an array to have more than one dimension. For example, a two-dimensional array consists of a certain number of.
1 Chapter 10 Various Topics User defined Types Enumerated Types Type Casting Syntactic Sugar Type Coercion.
1 Simple Data Types: Built-in and Use-Defined. 2 Chapter 10 Topics  Built-In Simple Types  Integral and Floating Point Data Types  Using Combined Assignment.
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
Chapter 7 Simple Date Types Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 3A Integral Data (Concepts)
(4-2) Selection Structures in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 16, 2015) Washington State University.
Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.
Chapter 2: Using Data.
1 Chapter 10 Simple Data Types: Built- In and User- Defined Dale/Weems.
9. Types Intro Programming in C++ Computer Science Dept Va Tech August, 2002 © Barnette ND & McQuain WD 1 Data Types data type:a collection of.
CISC105 – General Computer Science Class 9 – 07/03/2006.
(5-1) Selection Structures III in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 21, 2015) Washington State University.
Simple Data Types: Built-In and User-Defined
(4-3) Selection Structures II in C H&K Chapter 4 Instructor - Andrew S. O’Fallon CptS 121 (September 18, 2015) Washington State University.
(9-1) Strings I H&K Chapter 8 Instructor - Andrew S. O’Fallon CptS 121 (October 19, 2015) Washington State University.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
(3-1) Functions II H&K Chapter 3 Instructor - Andrew S. O’Fallon CptS 121 (September 9, 2015) Washington State University.
(3-2) File Processing with Functions Instructor - Andrew S. O’Fallon CptS 121 (September 11, 2015) Washington State University.
Chapter 7 C supports two fundamentally different kinds of numeric types: (a) integer types - whole numbers (1) signed (2) unsigned (b) floating types –
(7-2) Arrays I H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (October 9, 2015) Washington State University.
Streams and File Processing in C++ Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
(6-2) Iteration in C II H&K Chapter 5 Instructor - Andrew S. O’Fallon CptS 121 (February 19, 2016) Washington State University.
Classes: A Deeper Look D & D Chapter 9 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
(1-3) Basics of a Linked List I Instructor - Andrew S. O’Fallon CptS 122 (June 9, 2016) Washington State University.
LESSON 06.
Jan 2016 Solar Lunar Data.
(2-1) Data Structures & The Basics of a Linked List I
Chapter 3: Understanding C# Language Fundamentals
C++ Simple Data Types Simple types Integral Floating
(2-1) Data Structures & The Basics of a Linked List I
Simple Data Types and Function Calls
C Structures, Unions, Bit Manipulations and Enumerations
QUIZ.
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Variables and Constants
Presentation transcript:

Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University

C. Hundhausen, A. O’Fallon 2 Data Types We already know that: Data type = set of values + set of operations on those values We also know all values stored in a computer are represented as sequences of 0’s & 1’s

C. Hundhausen, A. O’Fallon 3 Internal Representation of int and double (1) As we already learned, int and double have different internal formats:

C. Hundhausen, A. O’Fallon 4 Internal Representation of int and double (2) C supports a variety of different integer formats: Type# bits in Microsoft Visual C Short16 unsigned short16 int32 unsigned int32 long32 unsigned long32

C. Hundhausen, A. O’Fallon 5 Internal Representation of int and double (3) Likewise, C supports a variety of different double formats: Type# bits in Microsoft Visual C float32 double64 long double64

C. Hundhausen, A. O’Fallon 6 Internal Representation of int and double (4) Beware of round-off errors! – Don't rely on two floating-point values being equal: for (trial = 0; trial != 10.0; trial += 0.1) { … } – Even the following may not execute the same number of times on all computers: for (trial = 0; trial < 10.0; trial += 0.1) { … } – It's better to use integers as loop counters!

C. Hundhausen, A. O’Fallon 7 Internal Representation of int and double (5) Conversions between int and double – When we assign an int to a double or a double to an int, C performs an automatic conversion: int k = 5, m = 4, n; double x = 2.5, y = 4.1, z; z = k + x; /* 7.5: k is converted to double prior to + */ z = k / m; /* k / m is evaluated first; result (1) is then converted to double (1.0) */ n = x * y; /* x * y is evaluated first (10.25). Result is then converted to int (10). Fractional part is lost */

C. Hundhausen, A. O’Fallon 8 Internal Representation of int and double (6) Conversions between int and double (cont.) – Note that explicit casting is always an option: int k = 5, m = 4, n; double x = 2.5, y = 4.1, z; z = (double) k + x; /* 7.5 */ z = (double) k / (double) m; /* 1.25 */ n = (int) x * (int) y; /* 8 */ – But such casts do not change the internal representation of a variable: printf(“%.2f\n", (double) k); /* 5.00 */ printf("%4d\n",k); /* 5 */ /* After these statements are executed, k is still stored as the int 5 */

C. Hundhausen, A. O’Fallon 9 Internal Representation of char (1) As we have learned, char variables are stored in 8 bit ASCII (American Standard Code for Information Interchange) format – '0'.. '9': 48 – 57 – 'A'.. 'Z': 65 – 90 – 'a'.. 'z': 97 – 122 – Printable characters: 32 – 122 – Non-printable characters: 0 – 31 and 127 – See Appendix A for the details

C. Hundhausen, A. O’Fallon 10 Internal Representation of char (2) It is possible to cast between char and int : int char_code; for (char_code = (int) 'A'; char_code <= (int) 'Z'; ++char_code) { printf("%c", (char) char_code); } Yields the following: ABCDEFGHIJKLMNOPQRSTUVWXYZ

C. Hundhausen, A. O’Fallon 11 Enumerated Types (1) Often, we'd like to define our own custom data types: – days of the week – months of the year – household budget categories – business inventory categories – etc. C enumerated types allow us to do this: typedef enum { clothing, household, electronics, garden, health_beauty, sporting_goods } inventory_t; Note: clothing gets integer value 0, household gets integer value 1,... sporting_goods gets integer value 5

C. Hundhausen, A. O’Fallon 12 Enumerated Types (2) Once we've defined an enumerated type, we can declare variables of that type: inventory_t inventory_kind; and use the type in switch statements: void print_inventory(inventory_t inv_kind) { switch (inv_kind) { case clothing: printf("clothing"); break;

C. Hundhausen, A. O’Fallon 13 Enumerated Types (3) case household: printf("household"); break; case electronics: printf("electronics"); break; case garden: printf("garden"); break; case health_beauty: printf("health and beauty"); break; case sporting_goods: printf("sporting goods"); break; }

C. Hundhausen, A. O’Fallon 14 Enumerated Types (4) We can make direct comparisons: if (household < sporting_goods) /* true */; if (electronics != health_beauty) /* true */ if (garden < household) /* false */ and "scroll" through items: inventory_t this_inventory = household; while (this_inventory <= sporting_goods) { print_inventory(this_inventory); this_inventory++; }

C. Hundhausen, A. O’Fallon 15 Enumerated Types (5) We can even cast enumerated types to int : int household_val; inventory_t this_inventory; household_val = (int) household /* 0 */..and cast integers to the enumerated type: this_inventory = (inventory_t)(electronics + 1); /* garden */

C. Hundhausen, A. O’Fallon 16 Other Common Enumerated Types? typedef enum boolean { FALSE, TRUE } Boolean; typedef enum month { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } Month;

C. Hundhausen, A. O’Fallon 17 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (8 th Ed.), Addison- Wesley, 2016 P.J. Deitel & H.M. Deitel, C How to Program (7 th Ed.), Pearson Education, Inc., 2013.

C. Hundhausen, A. O’Fallon 18 Collaborators Chris Hundhausen