The Joy of
Programming
S.G. Ganesh
Some Interesting Questions on Operators in C C has a very rich set of operators. In this month’s column, we’ll look at some interesting questions about various operators in C. The issues discussed about C are also applicable to languages based on C, such as Java and C++.
1
. Which operator in C can result in a ‘divide by zero’ error other than the / (division) operator? 2. The conditional operator (? :) is equivalent to if-then-else, which is a ternary operator. Why is there no if-then binary (?) operator? 3. Your nephew has scored 453 out of 500 marks in the SSLC exam, and you write a trivial program to check the percentage—what does it print? int main() { int marks = 453, total = 500; float percent = (marks/total)*100; printf(“percentage is = %3.2f!”, percent); }
4. You want a method that multiplies an integer by 9; will the following work? int mul_by_nine (int x) { return (x << 3 + x); }
5. What is the output of the following program: int main(){
printf(“%d \n”, 1 < 2 < 3);
printf(“%d \n”, 3 > 2 > 1);
}
6. What is wrong with the following program? #include
int main(){
int i = 2;
i = -i;
assert (i == -2);
i = +i;
assert (i == 2);
}
Why did this program fail with this assertion: “Assertion failed: i == 2, file tem.c, line 8”? 1.The modulus operator (%) can result in a ‘divide
by zero’ error since it is typically implemented using the division (/) operator. 2. Unlike if-then-else, the conditional operator can be part of expressions; in other words, the conditional operator has a type. The type of the conditional operator is the second and the third operands (they should be of the same type; otherwise, they get promoted to the same type). If there were a binary (?) operator, then it cannot take part in expressions, because it wouldn’t have any type if the condition becomes false! So, it is not possible to have something like a binary ‘if’ operator (?) in C! 3. It prints: your percentage of marks is 0.00! The integer division 432/500 results in 0, and 0 * 100 is 0. The integer value 0 is converted and stored in floating point value and hence the output is 0.00. Note that the % symbol will also not be printed because it has a special meaning in a format string (you have to make it as %% to print the percentage symbol). 4. No, it won’t. The logic in this code is to left-shift the int by 3- which is equivalent to ‘multiply by 8 - and add x once’ so that it becomes ‘multiply by 9’. However, this code has a bug: the operator + has higher precedence than the << operator, so the expression becomes (x << (3 + x)), which is wrong. If you use explicit parenthesis, it will avoid this problem. 5. It prints: 1 0 The mathematical operations/semantics are not directly translated as such in C programs. In mathematics, 1 < 2 < 3 and 3 > 2 > 1 are both true. However, in C, ( 1 < 2 ) is 1 and ( 1 < 3 ) is true; hence 1 is printed. But ( 3 > 2 ) is 1 (‘is true’?) and (1 > 1 ) is false (0) and hence 0 is printed. 6. The unary plus (+) operator is the only dummy operator in C. Its occurrence is just ignored by the compiler; it has no effect in the expressions. Note that the unary minus (–) operator changes the sign of the value, but unary plus is not a complementary operator to unary minus in that it doesn’t have any effect on the value on which it operates. By: S G Ganesh is a research engineer in Siemens (Corporate Technology). His latest book is “60 Tips on Object Oriented Programming”, published by Tata McGraw-Hill in December last year. You can reach him at [email protected].
www.openITis.com
|
LINUX For You
|
August 2008
83