Types - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Types

Description:

right hand side does not match result of the left hand side ... Assignment simply follow the type on the left hand side (hence) maybe very troublesome. ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 18
Provided by: hiy
Learn more at: https://www.cise.ufl.edu
Category:
Tags: lefthand | types

less

Transcript and Presenter's Notes

Title: Types


1
Types
  • CGS 3460, Lecture 21
  • Feb 27, 2006
  • Hen-I Yang

2
Announcement
  • Quiz 3 will be held on Mar 8th (Wed) instead of
    Mar 10th (Fri).
  • Quiz 3 includes homework 3, textbook up to Sec
    7.1, and slides up to last Friday (2/24)
  • Always put return 0 as the last statement in your
    code
  • TA will post a couple sample codes to help you
    learn nested loop.
  • It is strongly discouraged to simply use hundreds
    of statements for Homework 3.

3
Previously
  • Quiz 2
  • int

4
Agenda
  • int
  • float
  • char
  • sizeof
  • Type conversion

5
float
  • float, double, long double
  • 3 parts sign, exponent and fraction
  • float 1.17 x 10-38 3.4 x 1038
  • 6 digits precision
  • double 2.22 x 10-308 1.79 x 10308
  • 15 digits precision
  • long double 3.36 x 10-4931 - 1.19 x 104932
  • Its all about number of bits.
  • float (single precision) uses 32 bits
  • Usually long double ranges from 80 to 132 bits
  • By default, the values are stored as double
    precision number

6
float (II)
  • The range of values are machine dependent
  • When specified as a constant expression
  • 57.0F for float
  • 57.0L for long double
  • More conversion specifications
  • lf for double
  • Lf for long double

7
char
  • ASCII (7 bits 128 characters)
  • char a 0
  • C treats characters as small integers
  • It is possible to use char in a lot of places
    where you use int
  • signed/unsigned

8
Char (II)
9
Escape sequence
  • The escape sequences we knew
  • Octal escape sequence
  • Hexadecimal escape sequence

Reading/Writing characters
  • Conversion specification c
  • getchar(), putchar(ch)
  • They are faster

10
sizeof()
  • Tell you how many bytes a variable occupies

11
Type Conversion
  • Allows mixing of basic types.
  • Compiler takes care of the dirty work. (implicit
    conversions)
  • Coercion
  • happens when (usual arithmetic conversions)
  • operands are of different types
  • right hand side does not match result of the left
    hand side
  • argument declaration and invocation
  • return type

12
Problem with Type Conversion
  • int ? float
  • may lose precision
  • float ? int
  • not a good idea
  • long ? int
  • overflow problem

13
Type Conversion (II)
  • Strategy convert operands to the narrowest
    type that can safely accommodate both values, aka
    promotion.
  • Promotion if either operand is a floating type
  • float ? double ? long double
  • Promotion if both operands are of integer type
    int ? unsigned int ? long ? unsigned long
  • Try not to mixed singed and unsigned integer
  • Assignment simply follow the type on the left
    hand side (hence) maybe very troublesome.
  • Check out example on page 127.

14
Casting (Explicit Conversion)
  • (type-name) expression
  • Forced type conversion
  • Useful, but also dangerous
  • Use of casting to get the value you want
  • Use of casting to avoid overflow
  • Casting allows programmers to avoid the
    restriction setup by type system, but also expose
    to all sort of vulnerability because of the
    removal of such restrictions

15
typedef
  • Typedef int Bool
  • Define new type
  • After typedef, new type can be used just like
    basic type
  • Can define a type with a more proper name (more
    informative)
  • Enhance portability
  • More powerful than define macro,
  • Change once, effective all
  • can be used anywhere as synonym of original type
  • Obey scope rule

16
Summary
  • int
  • float
  • char
  • sizeof
  • Type conversion
  • typedef

17
Before you go
  • Read Chapter 7.
  • Exercise 7.15, 7.17
  • Homework 3 due Mar 7, Tuesday at 1159 pm
  • Quiz 3 will be held on Mar 8th (Wed).
  • Quiz 3 covers
  • homework 3,
  • textbook up to Sec 7.1, and
  • slides up to last Friday (2/24)
Write a Comment
User Comments (0)
About PowerShow.com