CptS 121 Fall 09 Lecture 81 - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

CptS 121 Fall 09 Lecture 81

Description:

CptS 121 Fall 09 Lecture 8-1. HK Chapter 7.1 7.3: Data Types ... unsigned short. 16. Short # bits in Microsoft Visual C. Type. 4. CptS 121 L8-1 10/12/09 ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 15
Provided by: eecs8
Category:
Tags: cpts | fall | lecture | unsigned

less

Transcript and Presenter's Notes

Title: CptS 121 Fall 09 Lecture 81


1
CptS 121 Fall 09 Lecture 8-1
  • HK Chapter 7.1 7.3 Data Types
  • Lecture Outline
  • Representation of numeric and char types
  • Enumerated Types
  • Quiz (on material covered last week)

2
Internal Representation of int and double
  • As we already learned, int and double have
    different internal formats

3
Internal Representation of int and double (cont.)
  • C supports a variety of different integer formats

4
Internal Representation of int and double (cont.)
  • Likewise, C supports a variety of different
    double formats

5
Internal Representation of int and double (cont.)
  • 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 lt 10.0 trial 0.1)
  • It's better to use integers as loop counters!

6
Internal Representation of int and double (cont.)
  • 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 /

7
Internal Representation of int and double (cont.)
  • 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 /

8
Internal Representation of char
  • 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

9
Internal Representation of char (cont.)
  • It is possible to cast between char and int
  • int char_codefor (char_code (int) 'A'
  • char_code lt (int) 'Z'
  • char_code)
  • printf("c", (char) char_code)
  • Yields the following
  • ABCDEFGHIJKLMNOPQRSTUVWXYZ

10
Enumerated Types
  • 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
Enumerated Types (cont.)
  • 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
Enumerated Types (cont.)
  • 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 (cont.)
  • We can make direct comparisons
  • if (household lt sporting_goods) / true /
  • if (electronics ! health_beauty) / true /
  • if (garden lt household) / false /
  • and "scroll" through items
  • inventory_t this_inventory household
  • while (this_inventory lt sporting_goods)
  • print_inventory(this_inventory)
  • this_inventory

14
Enumerated Types (cont.)
  • 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 /
Write a Comment
User Comments (0)
About PowerShow.com