P.Lecture - 1.31.08

# P.Lecture - 1.31.08 - Announcements Assignment 2 Offi...

This preview shows pages 1–11. Sign up to view the full content.

Announcements Assignment 2 Office Hours today Topics for today Type conversions and casting Typedef and sizeof rrays 1D arrays 1D

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

View Full Document
Usual Arithmetic Conversions in Expressions romotions eg: + B Promotions, e.g: A + B In mixed type expressions its safer to convert int to float Convert the narrower operand to the type of the wider operand Case 1:Either Operand is Floating Type Case 2:Neither Operand is Floating Type long double unsigned long int double long int float unsigned int nt Don’t mix signed and unsigned integers int
ixing Signed and Unsigned Mixing Signed and Unsigned hat happens? What happens? int i = 10; nsigned int u = 10; unsigned int u = 10; if(i < u) { rintf(“i is less than u ”); printf( i is less than u\ n) ; } s converted to an unsigned int before the i is converted to an unsigned int before the comparison hich looks like what? Which looks like what?

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

View Full Document
Conversion During Assignment For assignment, the value on the right side is converted to the type of the variable on the left side • Great if the type on the left side is at least as wide as the expression Converting from a “larger” type to a “smaller” type may not always produce a meaningful result: int i; float f; i=f; – This assignment is meaningful only if the value of f—when converted to an int—is between the smallest and largest values of an int – If f is too large or too small, i will be assigned an apparently meaningless number Converting a floating-point to an int is done by dropping the fractional part (not by rounding to the nearest integer): int i; i = 842.97; /* i is now 842 */ i = -842.97; /* i is now -842 */
onversion During Assignment Conversion During Assignment char c = ‘a’; /*97*/ The expression on the right side is converted the type of the int i; float f; ouble d; i = c; = i; to the type of the variable on the left side efore e assignment double d; f = i; d = f; i = 842.97; before the assignment is done i = 842.97; c = 10000; //WRONG i = 1.0e20; //WRONG f = 1.0e100; //WRONG

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

View Full Document
Casting Casting is used for explicit type conversion A cast expression has the form ( target type name ) expression /* it’s a unary operator */ Example 1 (get just the fractional part of a float): float f = 3.5, fracPart; fracPart = f (int) f; /* fracPart is now 0.5 */ Example 2: int i = 5, j = 2; float f; f = i/j; /* f is now 2.0 */ f = (float) i/j; /* f is now 2.5 why ? */
hat does this do? What does this do? t i; int i; float f; (int) f; i = (int) f; Why would I want to do it?

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

View Full Document
hat’s wrong with this? What s wrong with this? ng int i; long int i; int j = 1000; j * j; i = j j; Is this a good fix? (long int) (j * j); i = (long int) (j * j); Why is this a good fix? i = (long int) j * j;
Example /* program to print the square roots of numbers from 1 to 100, and the whole number part and fractional part separated */ #include <stdio.h> #include <math.h> int main(void) { ouble i; double i; for(i=1.0; i<101.0; i++) { /* for loop index can be real */ printf("The square root of %lf is %lf\n", i, sqrt(i)); printf("Whole number part: %d ", (int)sqrt(i)); printf("Fractional part: %lf\n", sqrt(i) (int)sqrt(i)); printf("\n"); } return 0; }

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

View Full Document
Type Definitions typedef lets you create an alias for a data type,e.g.:
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 48

P.Lecture - 1.31.08 - Announcements Assignment 2 Offi...

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

View Full Document
Ask a homework question - tutors are online