Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Data Types H&K Chapter 7 Instructor - Andrew S. O’Fallon CptS 121 (March 4, 2016) Washington State University."— Presentation transcript:

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

2 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

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

4 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

5 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

6 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!

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

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

9 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

10 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

11 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

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

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

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

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

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

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

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


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

Similar presentations


Ads by Google