Can (a==1 && a==2 && a==3) ever evaluate to true in C or C++?

  • A+
Category:Languages

We know it can in Java and JavaScript.

But the question is, can the condition below ever evaluate to true in C or C++?

if(a==1 && a==2 && a==3)      printf("SUCCESS"); 

EDIT

If a was an integer.

 


Depends on your definition of "a is an integer":

int a__(){ static int r; return ++r; } #define a a__()  //a is now an expression of type `int` int main() {     return a==1 && a==2 && a==3; //returns 1 } 

Of course:

int f(int b) { return b==1&&b==2&&b==3; } 

will always return 0; and optimizers will generally replace the check with exactly that.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: