and Statements
Type conversion changing a value from one type to another Void Integral Floating Point Derived Boolean Character Integer Real Imaginary Complex no fractional part fractional part 2 tj
Suppose we had the following expression: voltage * current where: voltage was a variable of type int (5) current was a variable of type float (2.5) what would the expression evaluate to? 3 tj
Implicit Type Conversion Type conversions done automatically by the compiler Each type has a RANK bool < char < short < int < long < long long < float < double < long double complex types match the floating types 4 tj
Implicit Type Conversion int * float float 1) int converted to float 2) multiplication 3) result is of type float char + long int long int 1) char converted to long int 2) addition 3) result is of type long int 5 tj
Implicit Type Conversion No Side Effect int days; float rate; days * rate float days remains an int 6 tj
Explicit Type Conversion Cast or casting Force a type conversion Use the unary operator type cast (desired_type) var 7 tj
Explicit Type Conversion int a = 5; int b = 2; a/b (float) a / b a / (float) b (float) (a/b) 8 tj
Explicit Type Conversion int a = 5; int b = 2; a/b 2 (float) a / b 5.0 / 2 5.0 / 2.0 2.5 Answers a / (float) b 5 / 2.0 5.0 / 2.0 2.5 (float) (a/b) (float) (5/2) (float) 2 2.0 9 tj
Explicit Type Conversion No Side effect int a = 5; int b = 2; (float) a / b a = 5 a / (float) b b = 2 (float) (a/b) a = 5, b = 2 10 tj
Assignment Type Conversion Assignment operator = value evaluate right side expression side effect left side is assigned the value int a = 5; int b = 6; int c = 7; a = b + c; evaluate right side (b + c) value is 13 side effect a is assigned the value 13 11 tj
Assignment Type Conversion Regardless of any implicit or explicit type conversions the assignment operator side effect cannot change the type of a variable int a = 5; float b = 12.0; int c = 5; a = b / c; c is promoted to type float right side is evaluated 12.0 / 5.0 2.4 value is demoted to match the receiving variable (side effect) a = 2 12 tj
Assignment Type Conversion Regardless of any implicit or explicit type conversions the assignment operator side effect cannot change the type of a variable int a = 5; float b = 6; int c = 7; b = a + c; right side is evaluated 12 int value is promoted to match the receiving variable 12.0 side effect: b = 12.0 13 tj
Statements Statement Causes the processor to do something 11 types of statements Null Expression Return Compound Conditional Labeled Switch Iterative Break Continue Goto 14 tj
Statements Statement Null Statement Causes nothing to happen ; while(1){ ; } 15 tj
Statements Statement Expression Statement An expression with a semi-colon added Causes the processor to evaluate the expression Causes the processor to complete any side effects Processor discards the expression Special note: the side effect of the assignment operator is to store a value into a variable 16 tj
Statements Statement Expression Statement a = 5; value is 5, side effect is a holds the value 5 a = b = 5; same precedence, operate R to L b = 5 value is 5, side effect is b holds the value 5 a = 5 value is 5, side effect is a holds the value 5 17 tj
Statements Statement Expression Statement a = 5; a++; value is 5 side effect is a takes the value 6 the value is then discarded 18 tj
Statements Statement Return Statement Terminates all functions (including main) int main(void) { return 1; } 19 tj
Statements Statement Compound Statement Block of code containing zero or more statements These statements are considered a single entity int main(void) { return 1; } // multiple statements 20 tj
Statements Statement Pre-processor commands vs statements #define int_rate 0.25 #define int_rate 0.25; // pre-processor command // error payment = int_rate * balance; payment = 0.25; * balance; creates a compiler error at the payment line but you never see the expansion very difficult to catch 21 tj