31_pdfsam_lecture2 - Another example Print an ASCII table...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Another example Print an ASCII table in decimal and hexadecimal #include <stdio.h> sschar j; ssfor(j='a'; j<='m'; j++) ssssprintf(%c %3d %3x\n, j, j, j); ssreturn 0; int main(void) { } a b c d e f g h i j k l m s97s s98s s99s 100s 101s 102s 103s 104s 105s 106s 107s 108s 109s 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d Type conversions C is very exible with type conversions C is weakly typed compared to other languages like Java Type conversions C is very exible with type conversions C is weakly typed compared to other languages like Java If an operator has operands of dierent types, they are all implicitly converted to the wider type Type conversions C is very exible with type conversions C is weakly typed compared to other languages like Java If an operator has operands of dierent types, they are all implicitly converted to the wider type Conversions also occur when assigning a value of one type to a variable of another type Careful: Information may be lost by this conversion! Example: if f is a oat and i is an int, i=f will truncate the fractional part of f Explicit casts Casting lets you change the type of a value explicitly Syntax: (newtype) value Example: float PI = 3.1415; float int_part = (int) PI; float frac_part = PI - int_part; Type conversion example Type conversions can cause subtle bugs Q: What is the value of mean after this statement? oat mean = (2 + 3 + 5) / 3; A: 3, because integer division is used. To compute correctly, use a oat constant: oat mean = (1 + 2 + 5) / 3.0; Or, use an explicit cast to change an operand to oat: oat mean = (1 + 2 + 5) / ((oat)3); ...
View Full Document

This note was uploaded on 01/10/2010 for the course CS 2022 at Cornell University (Engineering School).

Page1 / 6

31_pdfsam_lecture2 - Another example Print an ASCII table...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online