## Thursday, 29 May 2014

### C/C++ Puzzles: PART - 35

C Program Tricked to Believe X equals X+2

The puzzle has been discussed previously, but only one solution was provided. But now I have added a few more solutions to this puzzle. The challenge is to get a true value for the following conditional statement.

x==x+2

You are free to use any feature of the C preprocessor or Compiler. If you can't get a solution then check the programs shown below.

Solution 1

#define x 2|0
#include<stdio.h>

int main()
{
if(x == x+2)
{
printf("\n Hello");
}
else
{
printf("\n Hi");
}
}

Solution 2

#include<stdio.h>

int main()
{
float x=1.0/0;
if(x==x+2)
{
printf("\nHello");
}
else
{
printf("\nHi");
}
}

Solution 3

#include<stdio.h>

int main()
{

float x = 1e100;
if(x==x+2)
{
printf("\nHello");
}
else
{
printf("\nHi");
}
}

Solution 4

#include<stdio.h>

struct {}*x=0;

int main()
{
if(x==x+2)
{
printf("\nHello");
}
else
{
printf("\nHi");
}
}

Solution 5

#include<stdio.h>

int (*x)[0x20000000];

int main()
{

if(x==x+2)
{

printf("\nHello");
}
else
{
printf("\nHi");
}
}

Solution 6

#include<stdio.h>

int main()
{
/////////////////////////////////////
// we initialize tr = 1            //

int tr = 1;
int x = 111;

////////////////////////////////////
// Can You change the value of tr??/
tr = (x == x + 2);

/////////////////////////////////////
// The value of tr is not changed  //
if(tr==1)
{
printf("\nHello");
}
else
{
printf("\nHi");
}
}