1 / 16

Data Types H&K Chapter 7

Data Types H&K Chapter 7. Instructor – Gokcen Cilingir Cpt S 121 ( July 12, 2011) Washington State University. Data Types. Data type = set of values + set of operations on those values Ex: int data type in C ( Microsoft Visual C represents this type by 32 bits). Set of values:

mandar
Download Presentation

Data Types H&K Chapter 7

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. Data Types H&K Chapter 7 Instructor – GokcenCilingir Cpt S 121 (July 12, 2011) Washington State University

  2. Data Types • Data type = set of values + set of operations on those values • Ex: intdata type in C (Microsoft Visual C represents this type by 32 bits) Set of values: “integers” in range [-2,147,483,648 , +2,147,483,647] int • Operations: • arithmetic operators: +, -, *, /, % (mod) • relational operators: >, <, <=, >= • equality operators: ==, != • …

  3. Internal Representation of int and double (1) • We learned that all values stored in a computer are represented as sequences of 0’s & 1’s. • Other than the sequential structure of memory, for many applications, programmer doesn’t know/care about the underlying representations of the data types. • As you already learned int and double have different internal formats: • real number = mantissa * 2 exponent integer Binary fraction between [0.5 ,1] or [-1,-0.5]

  4. Internal Representation of int and double (2) • If we’re dealing with very big or very small numbers, we may want to check into how many bits are used to represent int , double • C supports a variety of different integer and double formats:

  5. Internal Representation of int and double (3) • Representation/ round-off errors • Just as certain fractions cannot be represented exactly in the decimal number system (e.g the fraction 1/3 is 0.3333…), some fractions cannot be represented as binary numbers in the mantissa of the type double format. Because of this: • Don't rely on equality operators while dealing with floating-point values: for (trial = 0; trial != 10.0; trial += 0.1) {…} • Don't rely on comparison operators while dealing with floating-point values, either. Following may not execute the same number of times on all computers: for (trial = 0; trial < 10.0; trial += 0.1) {…} • Therefore, it's better to use integers as loop counters!

  6. Internal Representation of int and double (4) • 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; //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 to 10.25. //Result is then converted to int 10 //Fractional part is lost during this conversion

  7. Internal Representation of int and double (5) • 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 happen on the fly and so 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 */

  8. 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 control characters: 0 – 31 and 127 (sending a control character to an output device causes the device to perform a special operation such as advancing the cursor to the next line) • See Appendix A for the details

  9. Internal Representation of char (2) • It is possible to cast between char and int: intchar_code; for (char_code = (int) 'A'; char_code <= (int) 'Z'; ++char_code) { printf("%c", (char) char_code); } Yields the following: ABCDEFGHIJKLMNOPQRSTUVWXYZ • Casting is sometimes done to increase readability of the code (even if it doesn’t effect the result). In the case of conversion between char to int, as you’ve already seen, we don’t need explicit casting. Above code would work just fine without the explicit casts.

  10. Enumerated Types (1) • Often, we may need to define our own custom data types, specifying the possible values of it. Examples: • days of the week • months of the year • household budget categories • business inventory categories • etc. • C enumerated types allow us to do this. For example enumerated type inventory_t has 6 possible values typedefenum { clothing, household, electronics, garden, health_beauty, sporting_goods } inventory_t; Note 1:clothing gets integer value 0, household gets integer value 1, . . . sporting_goods gets integer value 5. Note 2:Remember, we don’t really care how they are represented by C, just like in the case of char data type. All we need to know is that each will get unique values and these values will be ordered in the order we’ve listed them.

  11. Enumerated Types (2) • Once we've defined an enumerated type, we can declare variables of that type: inventory_tinv_kind; • We can initialize these variables just like we do in any data type: inventory_tthis_inventory = household; //Recall a similar situation with an char type:char x = ‘a’; • We can make direct comparisons: if (household < sporting_goods) // true if (electronics != health_beauty) // true // Recall a similar situation with an char type: if(‘a’ < ‘t’) inventory_t invent2 = household; inventory_t invent1 = sporting_goods; if (invent2 < invent1 ) // true ;

  12. Enumerated Types (3) • We can use the type in switch statements: void print_inventory(inventory_tinv_kind) { switch (inv_kind) { case clothing: printf("clothing"); break; 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; } }

  13. Enumerated Types (4) • We can "scroll" through items: inventory_tthis_inventory = household; while (this_inventory <= sporting_goods) { print_inventory(this_inventory); this_inventory++; } • Recall a similar situation with an inttype (to understand what’s happening here) char ch = ‘a’; while (ch <= ‘z’) { print_char(counter); ch++; }

  14. Enumerated Types (5) • We can even cast enumerated types to int: inthousehold_val; inventory_tthis_inventory; household_val = (int) household // 0 ..and cast integers to the enumerated type: this_inventory = (inventory_t)(electronics + 1); // garden

  15. Other Common Enumerated Types? typedefenum { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } Month; typedefenum { FALSE, TRUE } Boolean;

  16. References • J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6th Ed.), Addison-Wesley, 2010 • P.J. Deitel & H.M. Deitel, C How to Program (5th Ed.), Pearson Education , Inc., 2007.

More Related