+ 1
is 2==x wrong ?? if so why?
13 Answers
+ 4
The x variable must be either one of the integral types or floating point types, then both forms of comparison ( 2 == x and/or x == 2 ) gets evaluted, otherwise it's a compilation error due to the type mismatch.
â â Some examples â â
char x1 = '2';
int x2 = 2;
long long x3 = 2LL;
double x4 = 2.0;
string x5 = "2";
int *x6 = nullptr;
// Check 2 against each type
bool b1 = 2 == x1; // legal - false
bool b2 = 2 == x2; // legal - true
bool b3 = 2 == x3; // legal - true
bool b4 = 2 == x4; // legal - true
bool b5 = 2 == x5; // illegal - type mismatch - error
bool b6 = 2 == x6; // illegal - type mismatch - error
+ 2
https://code.sololearn.com/W2pnqLaDqG6c/?ref=app
I knew it was not wrong but took 30 seconds to try it anyway.
It's not wrong at all, since it compares values with values.
P.S. SoloLearn has a code playground you can use to test these fun experiments yourself đ
+ 2
cause 2 is a number ...
and it isn't proper to start with a number.. or symbol...
I think...
+ 2
It's basically a value==value in that expression. So it's right either way.
+ 2
secondly 2 and x can't be equal...
.... ...đ
+ 2
aderinola gbenga if x has the value of 2 it will return true, if not, it will return false.
The user was asking if the expression is possible, which the answer is yes.
I think you misinterpreted the question being asked.
+ 2
OK thenđ
+ 1
aderinola gbenga its not wrong, and there are no rules against it.
+ 1
ok, thanks Andre Danielđ
0
What are you referring to? I am confused.
0
if I had to check a value of x using '==', is the above method wrong?
0
I did try it on my own but I thought it 2==x would be approached differently by the computer
0
thanks c++ soldier đđ