O'Reilly logo

Express Learning - Computer Fundamentals and Programming by ITL ESL, Ashok Kamthane

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Operators and Expressions
1. What is the role of operator?
Ans: An operator performs different operations on operands and produces result. An expression is to
be created with the help of operator and operands, i.e., expression is build with operator and operands.
For example, the following expression contains two operands and one operator.
a*b,
where a and b are operands and * is the operator.
Similarly, by using different operators’ expressions can be built. Operator operates on single operand
or more than one operand.
2. Explain different types of operators supported by C.
Ans: The C language supports different exhaustive operators. All of them are not elaborated. C provides
the following four types of basic operators.
1. Arithmetic,
2. Relational,
3. Logical and
4. Bitwise.
Besides these four basic operators, ‘C’ also supports additional operators such as increment, decrement,
conditional and assignment. Basic operators and others along with their symbolic representation are
shown in Table 3.1.
Table 3.1 Types of Operators
Type of Operator Symbolic Representation
Arithmetic Operators +, -, *, / and %
Relational Operators >, <, = =, >=.<= and !=
Logical Operators &&, || and !
Increment and Decrement Operator ++ and
- -
Assignment Operator =, +=, – =, *=, /=, %=
3
(Continued)
M03_ITL-ESL4791_02_SE_C03.indd 40 12/22/2012 5:00:06 PM
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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required