(x == x+2) make it equal for x - Programming Puzzles

Define value of x in such a way that the expression (x == x+2) would evaluate to true. No rule for this programming puzzle.

Solutions

Jump to C, CSharp, JavaScript, PHP Solution.
You can submit your own solution in comment section in same/different programming language. If your solution is better or in different language we'll add here.

C

#include<stdio.h>

int main()
{
	double x = 1.0/0.0;
	printf("%d",x==x+2);
	return 0;
}
#include<stdio.h>
#define x 2|0

int main()
{
	printf("%d",x==x+2); //Basically, the expression is expanded to (2|0 == 2|(0+2)). 
	return 0;
}
#include<stdio.h>

int main() 
{ 
	float x = 1e10; 
	printf("%d\n", x == x + 2); 
}

CSharp

using System;

namespace testcs
{
    class Program
    {
        static int y;
        static int x
        {
            get
            {
                return y -= 2;
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine(x == x + 2);
        }
    }
}

JavaScript

<html>
<body>
<script>
    x = {
        value: 0,
        valueOf: function () {
            this.value += 2;
            return this.value;
        }
    };

    alert(x == x + 2);
</script>
</body>
</html>

PHP

<?php
  $x = 1e17;
  echo $x==$x+2;

Discussion

Read Community Guidelines
You've successfully subscribed to Developer Insider
Great! Next, complete checkout for full access to Developer Insider
Welcome back! You've successfully signed in
Success! Your account is fully activated, you now have access to all content.