Operators and Expressions II-41
Type of Operator Symbolic Representation
Bitwise Operators &, |, ^, >>, << and ~
Comma Operator ,
Conditional Operator ?:
Arithmetic operators: They are used to perform arithmetic operations such as addition, subtraction,
multiplication and division between integers/floating numbers.
Relational operators: These operators provide relationship between two expressions. If the relation is
true then it returns a value 1 otherwise 0 for false. They are used to perform relational operations such
as finding the greater or smaller of the operands, checking the equality of the operands etc.
Logical operators: Logical relationship between the two expressions is tested with logical operators.
Each relational operator takes two expressions as operands and results either the integer value 1(true)
or the integer value 0(False). The operands could be constants, variables, and expressions.
Bitwise operators: ‘C’ supports a set of bitwise operators for bit manipulation as listed in the Table 3.1.
‘C’ supports six-bit operators. These operators can operate only on integer operands such as int, char,
short, long etc.
Assignment operator: Assigning a value to a variable is done with assignment operator. For example
int x = 5; here 5 is assigned to x and this is carried out by the operator =. The equal (=) operator is used
for assignment and hence the name assignment operator. We can associate assignment operator with
other binary operators. Such a type of association of operators is called compound assignment operators.
Unary Operators: Unary operator performs operation on an operand. Unary operators are increment
operator (++), decrement (- -), minus (-) etc.
Besides there are some other operators in C, which are not covered in this question.
3. What are the uses of comma (,) and conditional (?) operators?
Ans: The ‘comma operator’ is used to separate two or more expressions.
It is not essential to enclose the expressions with comma operators within the parentheses. We need to
use comma operator in variable declaration, printf, scanf functions, in function prototypes etc.
For example, comma operators are used in the following expressions.
int a, b, c;
printf("%d %d", a, b);
The ‘conditional operator’ contains condition followed by two statements or values. The condition
operator is also called the ternary operator. If the condition is true, the first statement is executed;
otherwise, the second statement is executed. The condition operator is used when there are two different
values to be used based on some condition.
An example on conditional operator is as follows:
Expr 1? Expr 2: Expr 3;
Expr 1 is evaluated first and if it is true, then Expr 2 is evaluated and its value is the result. If Expr 1
is false, then Expr 3 is evaluated and its value becomes the final answer.
(x>y)?printf("%d", x): printf("%d", y):
In the above example in case x is greater than y, then first printf gets evaluated and if is false, then
second printf statement gets evaluated.
Table 3.1 (Continued)
M03_ITL-ESL4791_02_SE_C03.indd 41 12/22/2012 5:00:07 PM