Computer Programming CS F111 BITS Pilani Dubai Campus NAND KUMAR
Basics of C Programming BITS Pilani Dubai Campus
Write a program that 1. Asks 5 marks from the user, find the average of the marks and print it with 2 digit precision. 2. Asks 2 Numbers and precision value, display the sum with the specified precision 3. Asks the user to enter the radius of a circle and calculates the Area of the circle as Area=PI * radius * radius display the Area with 4 digit precision
Reading a character Function : getchar(); getc(stdin); Syntax variable_name = getchar(); variable_name = getc(stdin); Example: char ans; ans = getchar(); ans = getc(stdin); It accepts any character keyed in.
Writing a character Function : putchar(int c); putc(int c, stdout); Syntax: putchar(var_name); putc(var_name, stdout); Example: char ans; ans = Y ; putchar(ans); Q. What is the effect of putchar( \n );
Arithmetic Operators Operator Meaning + Addition or unary plus - Subtraction or unary minus * Multiplication / Division % Modulo Division
Arithmetic Expression Let int x = 15; int y = 6; Example: x-y is 9 x+y is 21 x*y is 90 x/y is 2 (decimal part truncated) x%y is 3 (remainder of the division) Q. If x and y are declared as float then? Q. If x is integer and y is float then?
Operator / and % 7.0 / 2.0 = 3.5 7.0 / 2 = 3.5 7 / 2.0 = 3.5 7 / 2 = 3 0 / 2 = 0 2 / 0 is Undefined 2 / -3 Varies 7.0 % 2.0 = error error: invalid operands to binary % 7.0 % 2 = error error: invalid operands to binary % 7 % 2.0 = error error: invalid operands to binary % 7 % 2 = 1 For % operation, when the operands are negative, the output depends the compiler GCC behaves the following way -7 % -2 = -1-7 % 2 = -1 7 % -2 = 1 7 % 2 = 1 7 % 0 is Undefined
What is Precedence and Associativity? Operators have rules that are used to determine how expressions are evaluated. Precedence and associativity deal with the evaluation order within expressions Precedence rules specify the order in which operators of different precedence level are evaluated Associativity rules decides the order in which multiple occurrences of the same level operator are applied.
Rules of Evaluation of Expression 1. First, parenthesized sub expression from left to right are evaluated. 2. If parentheses are nested, the evaluation begins with the innermost sub expression. 3. The precedence rule is applied in determining the order of application of operators in evaluating sub-expressions. 4. The associativity rule is applied when two or more operators of the same precedence level appear in a sub expression.
Precedence of Arithmetic Operators Two priority levels of operators High priority *, /, % Low priority +, - Consider the expression 1+2*3 * has higher priority than + So the value of the expression is 7
Precedence of Arithmetic Operators Priority can be overruled by parenthesis (1+2)*3 Expression inside parenthesis are evaluated first so (1+2)*3 gives 9 Now consider the expression 1+2-3+4-5 Note: + and have the same precedence Associativity rule is used to determine how to evaluate the above expression. Associativity for Arithmetic Operators is L to R Try: x = 10-16/4 + 4 * 3-4
Expression with multiple operators Rules for evaluating expressions Parenthesis Rule Evaluated separately. In case of nested parenthesis, evaluation order is from innermost to outermost Operator Precedence Rule Unary +,- *, /, % Binary +,- : first : last : next Associativity Rule Unary operators at same precedence : right associativity Binary operators at same precedence: left associativity Find the order of evaluation: x * y * z + a / b - c * d
Examples: Evaluate the following expressions z = 2*3/4+4/4+8-2+5/7 z =?? y = 4/5*6+2/7+4 y =?? Let int a,b; float c,d; a=5;b=7; c=4.0;d=3.0; x = a/b*(c+d)/a-b+c/d x =??
What if the expression has multiple operators? Consider area = PI * radius * radius ;
What if the expression has multiple operators? Formula for velocity is v = (p 2 -p 1 ) / (t 2 -t 1 )
What if the expression has multiple operators? Consider an expression a=z- (a+b/2) + w * -y
Write the following formulas in C 1) b 2-4ac 2) a+b c+d 3) 1 1+x 2 Specify multiplication explicitly using * Use parenthesis to control the order of operator evaluation