Sei sulla pagina 1di 18

(9-1) Data Types

H&K Chapter 7
Instructor - Andrew S. OFallon
CptS 121 (March 10, 2014)
Washington State University

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 0s & 1s

C. Hundhausen, A. OFallon

Internal Representation of int and


double (1)

As we already learned, int and double


have different internal formats:

C. Hundhausen, A. OFallon

Internal Representation of int and


double (2)

C supports a variety of
different integer
formats:

C. Hundhausen, A. OFallon

Type

# bits in
Microsoft Visual
C

Short

16

unsigned short

16

int

32

unsigned int

32

long

32

unsigned long

32

Internal Representation of int and


double (3)

Likewise, C supports a variety of different double


formats:
Type

# bits in
Microsoft Visual
C

float

32

double

64

long double

64

C. Hundhausen, A. OFallon

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

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

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

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

Internal Representation of char

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

10

(2)

C. Hundhausen, A. OFallon

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

11

C. Hundhausen, A. OFallon

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;

12

C. Hundhausen, A. OFallon

Enumerated Types (3)

13

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

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

14

C. Hundhausen, A. OFallon

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

15

C. Hundhausen, A. OFallon

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;

16

C. Hundhausen, A. OFallon

References
J.R.

Hanly & E.B. Koffman, Problem Solving


and Program Design in C (7th Ed.), AddisonWesley, 2013
P.J. Deitel & H.M. Deitel, C How to Program
(5th Ed.), Pearson Education , Inc., 2007.

17

C. Hundhausen, A. OFallon

Collaborators
Chris

18

Hundhausen

C. Hundhausen, A. OFallon

Potrebbero piacerti anche