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

  1. 20
  2. 1
  3. 10
  4. 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]

  1. In the Expression total number of Operators are 4
  2. Decide the priority of each operator and arrange them in the order of priority.
  3. Create Table and Decide Priority but make sure that expression does not contain Parenthesis.
Operator Priority Rank
+ 1
> 2
!= 3
= 4
  1. 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