Sunteți pe pagina 1din 1

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

www.openITis.com | LINUX For You | August 2008 83

S-ar putea să vă placă și