Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University
2 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) int Set of values: “integers” in range [-2,147,483,648, +2,147,483,647] Operations: arithmetic operators: +, -, *, /, % (mod) relational operators: >, = equality operators: ==, != …
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 Binary fraction between [0.5,1] or [-1,-0.5] integer
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: Internal Representation of int and double (2) Type# bits in Microsoft Visual C short16 unsigned short 16 int32 unsigned int32 long32 unsigned long 32 Type# bits in Microsoft Visual C float32 double64 long double64
5 Representation/ round-off errors ◦ Just as certain fractions cannot be represented exactly in the decimal number system (e.g the fraction 1/3 is …), 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! Internal Representation of int and double (3)
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 //Result is then converted to int 10 //Fractional part is lost during this conversion Internal Representation of int and double (4) C. Hundhausen, A. O’Fallon
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 */ Internal Representation of int and double (5) C. Hundhausen, A. O’Fallon
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 : int char_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.
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 typedef enum { 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_t inv_kind; We can initialize these variables just like we do in any data type: inventory_t this_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 ;
C. Hundhausen, A. O’Fallon12 Enumerated Types (3) We can use the type in switch statements: void print_inventory(inventory_t inv_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; }
C. Hundhausen, A. O’Fallon, G.Cilingir13 Enumerated Types (4) We can "scroll" through items: inventory_t this_inventory = household; while (this_inventory <= sporting_goods) { print_inventory(this_inventory); this_inventory++; } Recall a similar situation with an int type (to understand what’s happening here) char ch = ‘a’; while (ch <= ‘z’) { print_char(counter); ch++; }
C. Hundhausen, A. O’Fallon14 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’Fallon15 Other Common Enumerated Types? typedef enum { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC } Month; typedef enum { FALSE, TRUE } Boolean;
16 References J.R. Hanly & E.B. Koffman, Problem Solving and Program Design in C (6 th Ed.), Addison- Wesley, 2010 P.J. Deitel & H.M. Deitel, C How to Program (5 th Ed.), Pearson Education, Inc., 2007.