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
II-42 Programming Concepts
4. What are unary operators? Describe their uses.
Unary operators operate on only one operand. Examples of unary operators are increment, decrement,
unary minus and & operator. Out of these, the increment operator increases the value of operand by
one (++a), the decrement operand decrements the value of operand by one (−−a), the unary minus opera-
tor is used to invert the sign of the operand (−a) and the & operator is used to dereference the operator.
Unary operators and their meanings are given in the Table 3.2.
Table 3.2 Unary Arithmetic Operators
Operator Meaning or Action
Minus
++ Increment
−− Decrement
& Address operator
sizeof()
Gives the size of operator
5. Describe logical operators with their return values.
Ans: Logical operators perform the logical operations on the operands. Logical AND, OR, NOT etc.
are the examples of logical operators. The value returned by the logical operators is either zero or one.
When more than one condition is to be checked at a time, then logical operators are to be used. Result
of operation is 1 if condition is true, else 0.
For example, on logical operators
(a && b), where && is logical AND
(a || b), where || is logical OR
! a,where ! is logical NOT
Table 3.3 describes the three logical operators together with examples and their return values.
Table 3.3 Logical Operators
Operator Description or Action Example Return Value
&& Logical AND 5>3 && 5<10 1
|| Logical OR 8>5 || 8<2 1
! Logical NOT 8!=8 0
6. List the bitwise logical or binary operators supported by C.
Ans: C supports a set of bitwise operators for bit manipulation as listed in Table 3.4. These operators
operate only on integer operands.
Table 3.4 Bitwise Operators
Operator Meaning
>>
Right Shift
<<
Left Shift
^
Bitwise XOR (Exclusive OR)
~
One’s Complement
&
Bitwise AND
|
Bitwise OR
M03_ITL-ESL4791_02_SE_C03.indd 42 12/22/2012 5:00:07 PM
Operators and Expressions II-43
7. Distinguish between logical and bitwise operators.
Ans: The logical operators perform the operations such as AND and OR with a single result that is
either zero or non-zero, whereas bitwise operators operate on the bit stream obtained from the binary
equivalent of the operand.
8. What are the relational operators?
Ans: Relational operators are used to perform comparison between two operands such as less than,
greater than, equality and inequality. These operators are used to check the conditions. In case condition
is true, the result returned would be 1 otherwise 0.
Assume that a = 5 and b = 2.
Result of the following expressions would as follows:
(a > b) result of this expression is 1.
(a < b) result of this expression is 0.
One can use different relational operators as listed below and see the results.
>, <, >=, <=, !=
The relational operators together with their descriptions, examples and their return values are
given in Table 3.5.
Table 3.5 Relational Operators
Operator Description or Action Example Return Value
>
Greater than 5>4 1
<
Less than 10<9 0
<=
Less than or equal to 10<=10 1
>=
Greater than equal to 11>=5 1
= = Equal to 2==3 0
!= Not equal to 3!=3 0
9. What is the difference between ‘= and ==’?
Ans: The operator ‘= is the assignment operator used to assign the value. The operator == is equal-
ity operator use for comparing the values of the two operands / expressions. These two operators are
completely different.
10. What are the symbols used for (a) OR, (b) AND, (c) XOR and (d) NOT operations?
Ans:
Bitwise OR uses | operator.
Bitwise AND uses & operator.
Bitwise NOT uses ! operator.
Bitwise XOR uses ^ operator.
11. What is the order of precedence of arithmetic operators such as *,  /, +, and − etc ?
Ans: Precedence refers to the priority allotted to the different operators for processing. When an
expression contains many operators then the high priority operators are evaluated first before lower
priority operators.
Among arithmetic operators, the operators such as multiplication (*), division ( / ) and modulus ( % )
have highest priority and similar precedence. The operator + and – are having lowest precedence.
M03_ITL-ESL4791_02_SE_C03.indd 43 12/22/2012 5:00:07 PM
II-44 Programming Concepts
Example: 8 + 9 * 2
− 10
The operator * is highest priority. Hence multiplication operation is performed first.
The evaluation of expression is = 8 + 18 − 10
In the above expression + and are having same priority. In such situation, associativity rule for
operators to be followed i.e. left to right. The left most operation is evaluated first.
Further the evaluation of expression is = 26 10 and at last the result is 16.
In case expressions are within the bracket then they are evaluated first. In case same precedence
operates appear in the expression and that too inside the parenthesis, then the associativity rule is
followed for evaluation.
The result of the following expression is 1. Because evaluation starts from left to right.
((((2 + 3) − 4) * 5) − 4)
Following Table 3.6 shows the assessment of few arithmetic expressions by means of the precedence
and associativity rule.
Table 3.6 Arithmetic Expressions Using Precedence and Associativity Rule
Expression Corresponding expression Result
3 + 5
4 (3 + 5) 4
4
4 * 5 + 7 (4 * 5) + 7 27
2 * 10 + 10/2 (2 * 10) + (10/2) 25
2 + 3
2 + 6 * 5 + 5/2 ((2 + 3) 2) + (6 * 5) + (5/2)
35
12. What are the differences between division and modulus division operations?
Ans: The division operator (/) returns the quotient of the division of two operands and the modulus
division operator (%) returns the remainder of the division operation of the two operands.
Example: Assume that x = 10 and y = 3.
x/y operation gives result 3 and x%y gives result 1.
13. What are the limitations on modulus operator?
Ans: Modulus operator is to be applied only on integers, but not on floating numbers.
14. What are the ASCII codes? List the codes for digits 1 to 9, A to Z and a to z.
Ans: ASCII codes are the corresponding physical representation of all the characters used by C
language; it has different values ranging from 0 to 255 for different characters. The ranges of the ASCII
codes for the characters listed below are as follows:
1–9 ASCII value from 48 to 57.
A–Z ASCII value from 65 to 90.
a–z ASCII value from 97 to 122.
15. What is an assignment operator?
Ans: Assigning a constant value to a variable or value of one variable to another is done with assign-
ment operator. For example, int x=5; here 5 is assigned to x and this is carried out by the operator =.
The equal (=) is used for assignment and hence the name assignment operator.
16. List the assignment operators.
Ans: The list of assignment operators are given in Table 3.7.
M03_ITL-ESL4791_02_SE_C03.indd 44 12/22/2012 5:00:07 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset