Department of Computer and Information Science, School of Science, IUPUI - PowerPoint PPT Presentation

About This Presentation
Title:

Department of Computer and Information Science, School of Science, IUPUI

Description:

Otherwise, if either operand is unsigned, the other is converted to unsigned, ... truncates fractions part, if the number is too big to fit, the result is undetermined ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 6
Provided by: dalero
Category:

less

Transcript and Presenter's Notes

Title: Department of Computer and Information Science, School of Science, IUPUI


1
Department of Computer and Information
Science,School of Science, IUPUI
A First C Program (mixing datatypes)
Dale Roberts, Lecturer Computer Science,
IUPUI E-mail droberts_at_cs.iupui.edu
2
Data Type Conversion
  • Rule 1
  • char, short ? int
  • float ? double
  • Rule 2 (double ? long ? unsigned ? int)
  • If either operand is double, the other is
    converted to double, and the result is double
  • Otherwise, if either operand is long, the other
    is converted to long, and the result is long
  • Otherwise, if either operand is unsigned, the
    other is converted to unsigned, and the result is
    unsigned
  • Otherwise, the operand must be int

3
Examples
  • Example c char, u unsigned, i int, d
    double, ffloat,
  • s short, l long,
  • Expression Final Data Type Explanation
  • c s / i int short?int, int/int,
    char?int, int-int
  • u 3 i unsigned int(3)?unsigned,
  • unsignedunsignedunsigned,
  • int?unsigned, unsigned-unsignedunsigned
  • u 3.0 i double unsigned?double,
    doubledouble,
  • int?double, double-doubledouble
  • c i int char?int
  • c 1.0 double char?int (rule 1),
    int?double(rule 2)
  • 3 s l long short?int, intint, int?long,
    longlong

4
Data Type Conversion (cont.)
  • Note
  • Conversion of int to long preserves sign, so does
    short
  • Var expr
  • f d / round off /
  • i f
  • / truncates fractions part, if the number is too
    big to fit, the result is undetermined /
  • i l s i and c i / may eliminate high
    order bits /

5
  • If a specific type is required, the following
    syntax may be used, called cast operator.
  • (type) expr
  • Example
  • float f2.5
  • x (int)f 1
  • / the result is 3, Q will f value be changed?
    /
  • Unsigned int to int
  • there is not actual conversion between int and
    unsigned int.
  • Example(Assuming 2s complement machine and int
    is 2 bytes long)
  • unsigned i 65535 int j
  • j i / j will be 1 /
  • j -2
  • unsigned i 1 j / i 65535 /
Write a Comment
User Comments (0)
About PowerShow.com