-2
#include<stdio.h>
int main()
{    
int a=2;
 if(a==3,4)
  printf("hello");
return 0;
}

Warning: Condition is always true Why is it always true??

Ayush Nigam
  • 421
  • 7
  • 15
  • This has compiled sir. – Ayush Nigam Jul 23 '15 at 04:40
  • And what value of `a` would cause the number 4 to be false? – awm Jul 23 '15 at 04:41
  • And what is your intention when you write `if(a==3,4)`? If you write strange codes someday compiler may freak out. – Mohit Jain Jul 23 '15 at 06:01
  • Well i am really upset to hear that. But here's something to say:I tend to prioritize emotional realism above the known laws of time and space, and when you do that, it's inevitable that strange things happen. Which can be quite enjoyable, I think. – Ayush Nigam Jul 24 '15 at 17:01

2 Answers2

4

The , doesn't work like you think it does.

What a , does is evaluate all the expressions that are separated by the , in order, then return the last.

So what your if statement is actually doing is checking a==3 which returns false, but it discards this result. Then it checks if(4), which returns true.

Essentially your code is:

#include<stdio.h>
int main()
{    
    int a=2;
    if(4)
        printf("hello");
    return 0;
}
Loocid
  • 6,112
  • 1
  • 24
  • 42
-2

a==3,4

should be

a==3.4

Decimal are symbolised with a dot (.) and not a comma(,). A comma here splits instructions, just as it does in a for statement:

for(int a=0, int b=10 ; b<=0 ; a++,b--)

kashikai
  • 135
  • 6