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

Posted by Manav MN on Stack Overflow See other posts from Stack Overflow or by Manav MN
Published on 2010-01-28T14:46:57Z Indexed on 2010/04/10 18:53 UTC
Read the original article Hit count: 329

Filed under:
|
|
|
|

I am trying to establish equality of three equal variables, but the following code is not printing the obvious true 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))

© Stack Overflow or respective owner

Related posts about c

    Related posts about c++