Вы находитесь на странице: 1из 92

Anurag Malik

Computer Science Department


Precedence of Arithmetic Operators

 x= 10.000000 y=7.000000 z=4.000000


Precedence and Associativity Table
Precedence and Associativity Table
Precedence of operators
1. If more than one operators are involved in an
expression, C language has a predefined rule of
priority for the operators. This rule of priority of
operators is called operator precedence.

2. In C, precedence of arithmetic operators (*, %, /, +, -


) is higher than relational operators(= =, !=, >, <, >=,
<=) and precedence of relational operator is higher
than logical operators(&&, || and !).
Example of precedence

 (1 > 2 + 3 && 4)This expression is equivalent to:


((1 > (2 + 3)) && 4)i.e, (2 + 3) executes first resulting
into 5then, first part of the expression (1 > 5) executes
resulting into 0 (false)then, (0 && 4) executes
resulting into 0 (false)
Associativity of operators
• If two operators of same precedence (priority) is present in
an expression, Associativity of operators indicate the order
in which they execute.

Example of associativity
• 1 = = 2 != 3
• Here, operators = = and != have same precedence. The
associativity of both = = and != is left to right, i.e, the
expression on the left is executed first and moves towards
the right.
• Thus, the expression above is equivalent to :
• ((1 = = 2) != 3)i.e, (1 = = 2) executes first resulting into 0
(false)then, (0 != 3) executes resulting into 1 (true)
A comma operator
What is the output of these C code?
// PROGRAM 1 // PROGRAM 2
#include<stdio.h> #include<stdio.h>

int main(void) int main(void)


{ {
int a; int a;
a = 1, 2, 3; a = (1, 2, 3);
printf("%d", a); printf("%d", a);
return 0; return 0;
} }

Comma works as an operator in PROGRAM 1. Precendence of Comma operator is


least in operator precedence Table. So the assignment operator takes precedence
over comma and the expression “a = 1, 2, 3” becomes equivalent to “(a = 1), 2, 3”.
That is why we get output as 1 in the first program.

In PROGRAM 2, brackets are used so comma operator is executed first and we


get the output as 3
What is the output of this C code?
#include<stdio.h>
int main()
{ int num1 = 1, num2 = 2;
int res;
res = (num1, num2);
printf("%d", res);
return 0;
}

a) 1 b) 2 c) 4 d) Either 1 or 2
In this case value of rightmost operator will be assigned to
the variable. In this case value of num2 will be assigned to
variable res.
Answer: b
What is the output of this C code?
#include<stdio.h>
#include< conio.h>
void main()
{ clrscr();
printf("Computer","Programming");
getch(); }
A. Computer
B. Programming
C. Computer Programming
D. None of the above
Output: Computer
You might feel that answer of this statement should be
“Programming” because comma operator always returns
rightmost operator, in case of printf statement once comma is
read then it will consider preceding things as variable or values
for format specifier.
Using Comma Operator inside Switch cases.
What is the output of this C code?
#include<stdio.h>
#include< conio.h>
void main()
{ int choice = 2 ;
switch(choice)
{ case 1,2,1: printf("\nCS");
break;
case 1,3,2: printf("\n EC");
break;
case 4,5,3: printf("\n ME");
break; }}
• A. CS B. EC C. ME D. None of the above
• Output : EC
Chaining of comparison operators in C
What is the output of this C code?
#include <stdio.h>
int main()
{
int a = 10, b = 20, c = 30;
if (c > b > a)
printf("TRUE");
else
printf("FALSE");
return 0;
}
There is no chaining of comparison operators in C
// (c > b > a) is treated as ((c > b) > a), associativity of '>'
// is left to right. Therefore the value becomes ((30 > 20) > 10)
// which becomes (1 > 10)
The output of following program is “FALSE”.
Use of pre/post Increment operator in expression
What is the output of this C code?
#include<stdio.h>
#include<conio.h>
void main()
{ int i = 0, j = 0;
j = i++ + ++i;
printf("%d\n", i);
printf("%d\n", j);}
 A. 1, 1 B. 2, 2 C. 1,2 D. 2,1
 Above line of code have total four operators –
 Assignment Operator Post Increment Operator
 Pre Increment Operator Arithmetic Operator
 Now we need to arrange all the operators in the decreasing order of the Precedence –
 Operator ------ Precedence Rank
 Pre Increment 1 Post Increment 2
 Arithmetic Operator 3 Assignment Operator 4
 Now we have arranged all the operators in the decreasing order of the precedence.
Step 1 : j = i++ + 1; Step 2 : j = 1 + 1; (post-increment pending on i)
Step 3 : j = 2; (Addition) Step 4 : j = 2; (Assignment)
 Step 5 : Pending increment on i will be executed Ans: B Output : 2 2
What is the output of this C code?
#include<stdio.h>
void main()
{int a,b,x=10,y=10;
a = x--;b = --y;
printf("Value of a : %d",a);
qprintf("Value of b : %d",b);
}
A. 10, 10 B. 10, 9 C. 9,10 D. 10,11
Answer B
Output: Value of a : 10Value of b : 9
What is the Output of this C code?
#include <stdio.h>
int main()
{
int x = 2, y = 0;
int z;
z = (y++, y);
printf("%d\n", z);
return 0;
}
a) 0
b) 1
c) Undefined behaviour
d) Compilation error
Answer: b
What is the output of this C code?
#include <stdio.h>
int main()
{
int x = 2, y = 0, L;
int z;
z = y = 1, L = x && y;
printf("%d\n", L);
return 0;
}
 a) 0
b) 1
c) Undefined behaviour due to order of evaluation can be
different
d) Compilation error
 Answer: b
What is the output of this C code?
#include <stdio.h>
int main()
{
int y = 2;
int z = y +(y == 10);
printf("%d\n", z);
}
a) 12
b) 20
c) 2
d) Either 12 or 20

Answer: c
Which is the correct option?
 Which of the following comments about the ++ operator are
correct ?
 A. it is a unary operator
 B. the operand can come before or after the operator
 C. Both of these
 Ans : C
Which of the following operators takes only integer operands?
 A. +
 B. *
 C. /
 D. %
 Answer : D
What is the output of this C code?
#include <stdio.h>
int main()
{ int x = 2, y = 0;
int z = (y++) ? 2 : y == 1 && x;
printf("%d\n", z);
return 0;
}
a) 0
b) 1
c) 2
d) Undefined behaviour

Answer: b
Which is the correct option?
 In a 'C'expression involving || operator, evaluation
A. Will be stopped if one of its components evaluates to false
B. Will be stopped if one of its components evaluates to true
C. Takes place from right to left
D. None of these
 Ans: B
 In C programming language, which of the following type of
operators have the highest precedence
A. Relational operators B. Equality operators
C. Logical operators D. Arithmetic operators
 Ans:D
 In C programming language, which of the following
operators has the highest precedence?
 A. Unary + B. * C. >= D. = =
 Ans A
Which
Coercion
is the correct option?
A. Takes place across an assignment operator
B. takes place if an operator has operands of different data
types
C. means casting
D. both (a) & (b)
Ans: D
Many programming languages support the conversion of a
value into another of a different data type. This kind of type
conversions can be implicitly or explicitly made. Implicit
conversion, which is also called coercion, is automatically
done. Coercion is the automatic conversion between
compatible types and it is done by compiler.
 What is the output of this C code?
int main()
{ int i = 7;
i = i / 4;
printf("%d\n", i);
return 0; }
 A. Run time error B. 1 C. 3 D. Compile time error
 Answer B

 What is the output of this C code?


void main()
{ int x = 4.3 % 2;
printf("Value of x is %d", x);
}
 A. Value of x is 1.3 B. Value of x is 2
 C. Value of x is 0.3 D. Compile time error
Answer D
 What is the value of x in this C code?
void main()
{
int x = 4 *5 / 2 + 9;
printf(“%d”,x);
}
 A. 6.75 B. 1.85 C. 19 D. 3
 Answer C

 What is the output of this C code?


void main()
{ int y = 3;
int x = 7 % 4 * 3 / 2;
printf("Value of x is %d", x);
}
 A. Value of x is 1 B. Value of x is 4
 C. Value of x is 3 D. Compile time error
 Answer B
 Which of the following is not an arithmetic
operation?
 A. a *= 20; B. a /= 30; C. a %= 40; D. a != 50;
 Answer D

 Which of the following data type will throw an


error on modulus operation(%)?
 A. char B. short C. float D. int
 Answer C
 What is the output of this C code?
void main()
{
int a = 5;
int b = ++a + a++ + --a;
printf("Value of b is %d", b);
}
 A. Value of x is 16 B. Value of x is 21
C. Value of x is 18 D. Undefined behaviour
Answer C
 What is the output of this C code?
int main()
{ int a = 20;
double b = 15.6;
int c;
c = a + b;
printf("%d", c);
}
 A. 35 B. 36 C. 35.6 D. 30
 Answer A
 What is the output of this C code?
int main()
{
int a = 20, b = 15, c = 5;
int d;
d = a == (b + c);
printf("%d", d);
}
 A. 1 B. 40 C. 10 D. 5
 Answer A
 What is the output of this C code?
void main()
{ int k = 8;
int x = 0 == 1 && k++;
printf("%d%d\n", x, k);
}
 A. 0 9 B. 0 8 C. 1 9 D. 1 8
 Answer B
 What is the output of this C code?
void main()
{ char a = 'a';
int x = (a % 10)++;
printf("%d\n", x);
}
 A. 6 B. Junk value C. Compile time error D. 7
Answer C
 What is the output of this C code?
void main()
 {
1 < 2 ? return 1: return 2;
}
 A. returns 1 B. returns 2 C. varies D. Compile time error
 Answer
 What is the output of this C code?
int main()
{ int x = 2, y = 1;
x *= x + y;
printf("%d\n", x);
return 0;
}
 A. 5 B. 6 C. Undefined behaviour D. Compile time error
 Answer B
 What is the output of this C code?
int main()
{ int x = 2, y = 2;
x /= x / y;
printf("%d\n", x);
return 0;
}
 A. 2 B. 1 C. 0.5 D. Undefined Behaviour
 Answer B
Thank You

Вам также может понравиться