Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two '==' equality operators in same 'if' condition are not working as intended

I am trying to establish equality of three equal variables, but the following code is not printing the obvious correct answer which it should print. Can someone explain, how the compiler is parsing the given if(condition) internally?

#include<stdio.h>
int main()
{
        int i = 123, j = 123, k = 123;
        if ( i == j == k)
                printf("Equal\n");
        else
                printf("NOT Equal\n");
        return 0;
}

Output:

manav@workstation:~$ gcc -Wall -pedantic calc.c
calc.c: In function ‘main’:
calc.c:5: warning: suggest parentheses around comparison in operand of ‘==’
manav@workstation:~$ ./a.out
NOT Equal
manav@workstation:~$

EDIT:

Going by the answers given below, is the following statement okay to check above equality?

if ( (i==j) == (j==k))
like image 357
manav m-n Avatar asked Jan 28 '10 14:01

manav m-n


People also ask

What are the two equality operators?

The equality operators, equal to ( == ) and not equal to ( != ), have lower precedence than the relational operators, but they behave similarly. The result type for these operators is bool . The equal-to operator ( == ) returns true if both operands have the same value; otherwise, it returns false .

What is the effect of === operator?

Identical Operator === The comparison operator called as the Identical operator is the triple equal sign “===”. This operator allows for a much stricter comparison between the given variables or values. This operator returns true if both variable contains same information and same data types otherwise return false.

What is == and === in Javascript?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.

What are the two equality operators in C?

For the equality ( == ) and inequality ( != ) operators, the result of the comparison indicates whether the two pointers address the same memory location.


4 Answers

  if ( (i == j) == k )

  i == j -> true -> 1 
  1 != 123 

To avoid that:

 if ( i == j && j == k ) {

Don't do this:

 if ( (i==j) == (j==k))

You'll get for i = 1, j = 2, k = 1 :

 if ( (false) == (false) )

... hence the wrong answer ;)

like image 155
Kornel Kisielewicz Avatar answered Nov 06 '22 14:11

Kornel Kisielewicz


You need to separate the operations:

  if ( i == j && i == k)
like image 44
Vincent Ramdhanie Avatar answered Nov 06 '22 14:11

Vincent Ramdhanie


Expression

i == j == k

is parsed as

(i == j) == k

So you compare i to j and get true. Than you compare true to 123. true is converted to integer as 1. One is not equal 123, so the expression is false.

You need expression i == j && j == k

like image 22
Tadeusz Kopec for Ukraine Avatar answered Nov 06 '22 14:11

Tadeusz Kopec for Ukraine


I'd heed the compiler's warning and write it as (i==j) && (j==k). It takes longer to write but it means the same thing and is not likely to make the compiler complain.

like image 36
FrustratedWithFormsDesigner Avatar answered Nov 06 '22 12:11

FrustratedWithFormsDesigner