If Statement MCQ 5 : Comparison operators in C Programming
Multiple Choice Question on If Statement : Comparison operators Or Relational operators
Before Solving this Question You Should know : Operator Precedence and Priority Basic.
Problem Statement :
#include<stdio.h> void main() { int num1=10,num2=20,num3; num3 = num1 > 2 + num2!=3; printf("%d",num3); }
Options : Guess the Output
- 20
- 1
- 10
- 0
Explanation :
num3 = num1 > 2 + num2!=3;
[box]
First Rule of Thumb :
Always Count How many Operators are there in the Expression and make priority table[/box]
- In the Expression total number of Operators are 4
- Decide the priority of each operator and arrange them in the order of priority.
- Create Table and Decide Priority but make sure that expression does not contain Parenthesis.
Operator | Priority Rank |
+ | 1 |
> | 2 |
!= | 3 |
= | 4 |
- Addition Operator has Highest Priority so “+” Operation evaluated first
Steps of Solving Expression :
Step 1 : num3 = num1 > 2 + num2 !=3; Step 1 : num3 = num1 > 2 + 20 !=3; [Put Value of num2 ] Step 2 : num3 = num1 > 22 != 3; [Do Addition ] Step 3 : num3 = 0 != 3; [Relational Opr.">"] Step 4 : num3 = 1; [0 != 3 is True]
Answer :
1