Arithmetic Instructions
Integer and Float Conversions
Type Conversions in Assignments k is an integer variable a is real
Operator Precedence
Precedence and Associativity When an expression contains two operators of equal priority or precedence the tie between them is settled using the associativity of the operators. Associativity can be of two types— – Left to Right eg. a=3/2*5 – Right to Lefteg. a=b=c
A First Book of ANSI C, 4th edition9 Operator Precedence and Associativity Example: * 7 % 2 * 4 = % 2 * 4 = * 4 = = * 7 % 2 * 4 =
Order of Operations
Assume x=2.0 and y=6.0. Evaluate the statement float z= x+3*x/(y-4);
Writing Algebric Expressions in C
( 3 * ( z + y ) )
Types of Program Errors
Slide 19 Program Errors Syntax errors – Violation of the grammar rules of the language – Discovered by the compiler Error messages may not always show correct location of errors Run-time errors – Error conditions detected by the computer at run-time Logic errors – Errors in the program’s algorithm – Most difficult to diagnose – Computer does not recognize an error
Syntax Error Example #include int main() { char ch, ch1, ch2 /* <<< semicolon removed */ scanf("%c %c %c", &ch, &ch1, &ch2); printf("ch = %c\n", ch); printf("ch1 = %c\n", ch1); printf("ch2 = %c\n", ch2); return 0; }
Logical Error Example #include int main() { /* We want to multiply two numbers! Find where is the error??? */ int a=10, b=2; int result = a/b; /* Logic error! */ printf("result = %d", result); return 0; }
Runtime Error Example #include int main() { int i=10; i = i/0; // Divide by Zero printf("Hello"); return 0; }
Practice Questions