Download presentation
Presentation is loading. Please wait.
1
Expressions and Arithmetic
2
Assignments Due – Lab 1 Reading – Chapter 3 – 3.1-3.4
3
Precision Describes number of columns to use when displaying number –%5.2 lf – number is right-justified –%-5.2lf – number is left-justified If number is too small – minimum number of columns will be used Decimal numbers are rounded Decimal point occupies a column as does – (for negative numbers)
4
Precision – Example #include int main(void) { double x = 12345.6789; printf("10.2 - %5.2lf\n", x); printf("4.2 - %4.2lf\n", x); printf("5.6 - %5.6lf\n", x); printf("15.6 - %15.6lf\n", x); printf("0.3 - %0.3lf\n", x); return(0); }
5
Precision – Example #include int main(void) { double x = 12345.6789; printf("10.2 - %5.2lf\n", x); //12345.68 printf("4.2 - %4.2lf\n", x); //12345.68 printf("5.6 - %5.6lf\n", x); //12345.678900 printf("15.6 - %15.6lf\n", x); // 12345.678900 printf("0.3 - %0.3lf\n", x); //12345.679 return(0); }
6
Arithmetic Expressions c = a + b; d = 12/6; Binary Operators –+ –- –* –/ –% Unary Operator –-
7
Examples 10 % 8 32 % 10 55 % 11
8
Expression Type Depends on operands –int + int -> int –double + double -> double –int + double -> double –int / int -> ???
9
Precedence/Associativity Parentheses Unary operators *, /, % +, - (binary) Binary – evaluated left to right
10
Assignment total = cost + tax; sum = sum + item; -> sum += item; mynum = 5; mynum = 12;
11
Errors Syntax –printf(“Hello, World!”) Runtime –x = 0; –y = 32/x; Logic –printf(“Hell, World!”);
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.