The post heading gives an expression which is a contradiction and not possible. In general yes it is a contradiction, but can you write a code such that the below code prints “Hello World.” ?

```/* Add source code in the following code such that "Hello World." is printed. */

#include <stdio.h>
#include <math.h>

int main (void)
{

if (x == x + 5)
{
printf ("Hello ");
}
if (y != y)
{
printf ("World.\n");
}

return 0;
}
```

The answer is yes, we can make it possible to get “Hello World.” printed.

Declare x and y as floating point variables and assign INF to x and NAN to y and run the the code.

```
/* Add source code in the following code such that "Hello World." is printed.*/
#include <stdio.h>
#include <math.h>

int main (void)
{
float x = INFINITY;
float y = NAN;
if (x == x + 5)
{
printf ("Hello ");
}
if (y != y)
{
printf ("World.\n");
}

return 0;
}
```

And voila! you get both of the expressions true!

### What’s Going on?

(x == x + 5) is true because x = INFINITY and infinity propagates through arithmetic expressions as expected normally, therefore adding anything to infinity is infinity, which we can compare. Although when comparing if a variable is infinite should be done through isinf. The same could be generated by making x = 1.0/0.0

(y != y) is also true above, because y = NAN. There are two kinds of NAN behaviour, one is called the quiet NAN and another the signalling NAN. For C the macro NAN is defined if and only if the specific implementation supports quiet NANs (C99 Section 7.12 Paragraph 5). Quiet NAN is when a result of an expression is not a number and it does not raises an error and propagate through arithmetic expression, making the results NAN which involves NAN. A signalling NAN will raise a floating point error. (C99 Section 5.2.4.2.1 Paragraph 3)

For any ordered pair of values exactly any of the three relationships, less than, greater than, equal to, is true. NAN is unordered and is not less than, greater than or equal to any other numbers including itself. Therefore y == y is false. (C99 7.12.14 Paragraph 1). Also note that relation operators may raise floating point exceptions when they involve a NAN as an operand. In gcc implementation relation operators other than == and != will raise a floating point exception.

## 4 thoughts on “C Q&A #3: When ((x == x + 5) && (y != y)) is true.”

1. trunks175 says:

I have never heard on NAN before :( . What exactly is it?

1. phoxis says:

“A NaN is an encoding signifying Not-a-Number”, from C99/C11 Section 5.2.4.2.2 Paragraph 3.
Basically it is an internal representation of a calculation result when the result is not a defined value or which cannot be represented. Like square root of a negative number is NaN. Check more in http://en.wikipedia.org/wiki/NaN

1. trunks175 says:

Thanks! :)

2. Abdul in New Zealand says:

funny , I remember a similar strange thing could happen in php as well
ty for the post