Assignment Statements variable = expression; Expression can involve multiple operators: *, /, +, - The expression is always evaluated first and then its value is assigned to the variable on the left. Rules for evaluation (like Algebra) subexpressions inside parentheses are evaluated separately operators are evaluated according to the hierarchy: unary +, - *, /, % (remainder division) binary +, - Operators in same expression and at same precedence level are evaluated in left to right order. (Exception: unary operators in right to left order- Multiple unary operators are extremely rare!). -b + c/d -5.0 + 2.0 * 6.0 Operators are evaluated in order: _, _, _ value is ? -b / c * d -5.0 / 2.0 * 6.0 Operators are evaluated in order: _, _,_, value is ? Integer division versus real division The value of 5 / 2 (/ divides 2 integers) is different from the value of 5.0 / 2.0 (/ divides 2 reals). 5 / 2 is 2; 5.0 / 2.0 is 2.5 For 5 / 2.0 or 5.0 / 2, the integer is “promoted” to a real number and / divides 5.0 and 2.0 yielding 2.5. This happens because the operands of an arithmetic operator must both be the same type. 3.0 + 5 / 2 Operators are evaluated in order: _, _ Value is ? 3.0 * 5 / 2 Operators are evaluated in order: _, _ Value is ? 3 * 5 / 2 Operators are evaluated in order: _, _ Value is ? Mixed assignment int m, n; double x; m = 15; n = 10; x = m / n; What is value of x? (Note the expression is evaluated first, then the assignment takes place. / means integer division because both its operands are type int.) m = 15; x = 1.5; n = m + x; What is the value of n? Operator % - remainder operator Both operands must be integers or type int 15 % 10 yields the integer remainder of 15 divided by 10. Value is __ 10 % 15, value is __ 4 % 3, value is ___ 7 % 3, value is ___ 9 % 2, value is ___ 10 % 2, value is ___ 11 % 2, value is ___ Formatting output To display, a real value that represents money, use the placeholder %.2f. Value is rounded to 2 decimal places. printf(“%.2f”, 4.9653); 4.97 printf(“%.1f”); 5.0 To display a value in a specified number of columns (say 6), use the placeholder %6d for an integer %6.1f for real number to 1 decimal place printf(“%6d”, 25); 25 printf(“%6.1”, 5.123); 5.1 Reading two or more data items with scanf printf(“Enter hours and rate>”); scanf(“%lf%lf”, &hours, &rate); 1st number stored in hours, second in rate. Should be at least one space between numbers. Don’t forget &’s! char first, second; printf(“Enter your two initials>”); scanf(“%c%c”, &first, &second); or scanf(“ %c %c”, &first, &second); The above form will ignore any whitespace (blanks or return characters) before either letter. Enter your two initials> E K