Home ProgrammingC Language Comparison Operator- C Interview Question
C-Interview-Question-and-Answer

Comparison Operator- C Interview Question

by Log Raj
823 views

Hello readers, Lets start our another c interview question related to Comparison Operator. This question has connection with comparison and logical operators. To solve this question you must have proper knowledge of precedence of the operators in C language. Once you go through this article you can break any attack from c interviewer.

Question:

What will be the output of following code snippet?

void main(){
int a=3;
a=!a>14;
printf("%d",a);
getch();
}

Answer:

In the above expression there are two operators in left side. These are logical not ! and a relational greater than operator. i want to cover few lines explaining each of them.

Logical NOT (!) operator

First thing you must know about any Logical operator is, It always yield binary value. Second thing you must keep in mind is that the operand/s of the logical operators are always treated as boolean. Non zero value is treated is 1 and 0 is treated as 0. That is either 0 or 1. In case of logical not operator it will return 1 if operand is zero (0). And this operator will return 0 if the operand is non zero. In the above case operand is 3 which is non zero so it will return 0. If we used an expression like !0 in any expression it will return 1 exactly.

Relational Operator :

Relational operator is half arithmetic and half logical operator because it’s operands are always numerical and It always yield the logical value. That is operands of the Comparison Operator are numbers but the result is always binary. If comparison is true then output is 1 otherwise output is 0.

Precedence:

Precedence of the logical operator is greater than that of relational operator therefor the evaluation of Logical operators happen first.

So !3 returns 0. After this evaluation above expression looks like 0>14. obviously now we can expect answer. Since 0>14 is false therefor the output will be 0. Therefor you will find 0 as output in output console.

That’s how we get output 0.

There are few more interview questions related to operators in c. So you can enjoy your reading here.

Please give us suggestions and comments to encourage. Feel free to comment or drop mail.

 

You may also like

Comparison Operator- C Interview Question

by Log Raj time to read: 1 min
0