Government Polytechnic Ahemdabad, EC Deparrtment [Programming in C - 4331105]
Chapter-2: Operators, Expressions, Input and output Functions
Q-1: What is operator ? List various operators available in C.
An operator is a symbol that tells the computer to perform certain
mathematical orlogical operation.
The variables and constants on which the operators act are known as operands.
Ex : a+b ; where a and b are operands and „+‟ is a operator.
Operators are always associated with operands. Depending upon number
of operandsthere are basically three types of operators.
1. Binary Operator : It has two operands. Ex : a + b
2. Unary Operator : It as one operand. Ex : -a
3. Ternary Operator : It has three operands. Ex : (a>b)? a :b
Various operators available in C are as follows :
(1) Arithmetic operators
(2) Relational operators
(3) Logical operators
(4) Assignment operators
(5) Increment and Decrement operators
(6) Conditional operators
(7) Bitwise operators
(8) Special operators
-----------------------------------------------------------------------------------------------------------
Q-2 Explain Arithmatic Operator with example
Arithmetic Operators are used to perform the arithmetic expression. There are five
arithmetic Operators as listed below:
Operator Symbol Description Example
Addition + adds two operands a+b
Subtraction - subtracts two operands a-b
Multiplication * multiplication two operands a*b
Division / divides one operand by another operand a/b
Modulo % divides one operand by another operand and a%b
Division produce reminder of the division
Example :- if a = 16 and b = 3, then
a + b = 19
a – b = 13
a * b = 48
a/b=5
a%b=1
Point to Remember :
Division :- (1) If both operands are integer than the answer is always integer (truncated result)
(2) if any one or both operand is real than the result is always real
e.g. 16/3 = 5 Here 16 and 3 both are integer so answer is integer
16.0 / 3 = 5.33 Here 16.0 is real so answer is real
16 / 3.0 = 5.33 Here 16.0 is real so answer is real
16.0 / 3.0 = 5.33 Here 16.0 and 3.0 both are real so answer is real
[Prepared By : L.J.Vora, LEC, G.P.Ahmedabad]
Government Polytechnic Ahemdabad, EC Deparrtment [Programming in C - 4331105]
Modulo Division – (1) Modulo Operator can be performed on integer operands only
(2) The Sign of the answer of modulo operator is always the sign of its left
operand
Example :- 17 % 5 = 2 17 % -5 = 2
-17 % 5 = -2 -17 % -5 = -2
Addition/Subtraction/Multiplication :- (1) If both operands are integer then answer is integer
(2) If any one operand or both operands are real then answer is
real
Q-3 Explain Relational Operator with example
The Relational Operators are used for comparison.
Syntex :- e1 Relational operator e2
e1 and e2 are either constant, variables or arithmetic expression
The output of the relational expression is always true or false
Relational operators:
Operator Symbol Description Example
Equal to == Return TRUE(1) if both the operands are equal a==b
Not equal to != Return TRUE(1) if both the operands are not equal a!=b
Less than < Return TRUE(1) if operand1 is less than operand2 a<b
Less than or <= Return TRUE(1) if operand1 is less than or equal a<=b
equal to to operand2
Greater than > Return TRUE(1) if operand1 is greater than a>b
operand2
Greater than or >= Return TRUE(1) if operand1 is greater than or a>=b
equal to equal to operand2
Example :- for a = 10 and b = 6
a >b 10 > 6 True
a<b 10 < 6 False
a==b 10 = = 6 False
a!=b 10 ! = 6 True
a<=b 10 < = 6 False
a>=b 10 > = 6 True
Q-4. Explain Logical Operator with example
Logical operators are used when we want to combine more than one condition.
Logical Operator compare two conditions and return True or False
Operator Symbol Example
Logical AND && A && B
Logical OR || A || B
Logical NOT ! !A
[Prepared By : L.J.Vora, LEC, G.P.Ahmedabad]
Government Polytechnic Ahemdabad, EC Deparrtment [Programming in C - 4331105]
1) Logical AND (&&) :
The truth table of logical AND(&&) operator is given below :
A B A && B
FALSE FALSE FALSE
FALSE TRUE FALSE
TRUE FALSE FALSE
TRUE TRUE TRUE
If all the condition is true then it returns true otherwise false.
2) Logical OR (||) :
The truth table of logical OR(||) operator is given below :
A B A || B
FALSE FALSE FALSE
FALSE TRUE TRUE
TRUE FALSE TRUE
TRUE TRUE TRUE
If all the condition is false then it returns false otherwise true.
3) Logical NOT (!) :
The truth table of logical NOT(!) operator is given below :
A !A
TRUE FALSE
FALSE TRUE
Example :- if x =15 and y = 9
x < y && y ! = 9 x > y || x = = 15
15 < 9 && 9 ! = 9 15 > 9 || 15 = = 15 -----Putting value of x and
y
False && False True || True
False True
Q- 5. Explain Assignment operators with example
The assignement operator is used to assign a constant, value of variable or value of
expression to a variable on its left.
Syntex ;- variable = expression;
e.g. a = 5; -----here, constant value 5 is assigned to variable a
x = x + 1; ---- here, value of expression x + 1 is assigned to variable x
a = b; ---- here, value of variable b is assigned to variable a
Operator Symbol Example Description
Assignment = a=b assign the value of operand2 (value of b) to
operand1 (value of a)
[Prepared By : L.J.Vora, LEC, G.P.Ahmedabad]
Government Polytechnic Ahemdabad, EC Deparrtment [Programming in C - 4331105]
Q-6 : Explain Conditional Operator with example
The conditional operator is also known as ternary operator. It is known as ternary operator
because it has three operands.
Syntex : - (Condition) ? Statement-1 : statement -2;
First the condition is checked. If the condition is true then the statement followed the ?
(statement-1) is executed otherwise the statement followed the : (statement-2) is
executed.
Example :
(a>b)? printf(“a is maximum”): printf(“b is maximum”);
Here if the value of a is greater than b then it prints a is maximum otherwise
it prints b is maximum.
---------------------------------------------------------------------------------------------------------------
Q-7: Explain operator precedence and associativity. (For Understanding only.
Generally not asked in examination)
Each operator in C has a precedence associated with it.
The precedence is used to evaluate an expression which contains more than
oneoperator.
The high level precedence operators are evaluated first.
The operator of the same precedence are evaluated either from „left to right‟ or from
„right to left‟ depending on the level. This is known as associativity property
of anoperator.
Rules of Precedence and Associativity.
1. Precedence rules applied when operators are different.
2. Associatively rules applied when operators have same level.
The table as shown below provides complete list of operators.
Operator Description Associativity Rank
() Function call Left to right 1
[] Array elements reference
+ Unary plus Unary
- minus Increment
++ Increment Right to left 2
-- Decrement Logical
! negation
Ones complementPointer
~
reference Address
*
&
Size of an object
sizeof
* Multiplication
/ Division Left to right 3
% Modulus
+ Addition Left to right 4
- Subtraction
[Prepared By : L.J.Vora, LEC, G.P.Ahmedabad]
Government Polytechnic Ahemdabad, EC Deparrtment [Programming in C - 4331105]
< Less than
<= Less than or
> equal toGreater Left to right 5
>= than
Greater than or equal to
== Equa Left to right 6
!= lity
Ineq
ualit
y
& Bitwise AND Left to right 7
^ Bitwise XOR Left to right 8
| Bitwise OR Left to right 9
&& Logical AND Left to right 10
|| Logical OR Left to right 11
?: Conditional expression Right to left 12
= 13
*= /= %= Assignment Operators Right to left
+= -= &=
^= \=
<<= >>=
, Comma operator Left to right 14
Q- 8 Evaluate fllowing arithmetic expression
(1) 50 % 2 /2 (2) -2.0 + -3.0 (3) 21 / (int) 7.5 (4) 55 % 10 -5/2 + 4
= 0 /2 = -5.0 = 21/7 = 5 – 5/2 + 4
=0 =3 = 5–2
= 3+4
= 7
Such type of questions may be asked in examination
----------------------------------------------------------------------------------------------------------------------------- ----
Q-9 Write following C assignment statements
Answer
−𝒃+√𝒃𝟐 −𝟒𝒂𝒄
(1) 𝑿 = X = sqrt((-b+sqrt(b*b-4*a*c))/2*a
𝟐𝒂
(𝒂+𝟐𝒃)
(2) 𝑷 = 𝒅 P = (a+2*b)/(2.7*(c-d/b)+1)
𝟐.𝟕(𝒄− )+𝟏
𝒃
(3) 𝒛 = 𝒂 + 𝒃𝟐 z = a + b*b
(𝒊+𝒋−𝟒𝒊+𝒋−𝒑𝟐 )
(4) (i+j-4*i+j-p*p)/(k/l)
𝒌/𝒍
Such types of question may asked in examination
********************************************************************
[Prepared By : L.J.Vora, LEC, G.P.Ahmedabad]