+ 1

what is wrong in this code

#include<iostream> using namespace std; int main() { int x=0, y=0; cout<<++(x+y); }

23rd Jul 2022, 1:21 PM
Isbah
5 Answers
+ 3
"error: lvalue required as increment operand" Isbah the error message tells you what is wrong. In C++ the preincrement operator can be used only on lvalues. An lvalue is any identifier that may be used on the left-hand side of an assignment operator (=, +=, -=, *=, ... etc.). You may not assign (x+y) = 1, therefore you may not use ++(x+y) as an expression. Instead, simply add 1 with the normal addition operator, like (x+y+1), to get the equivalent result.
23rd Jul 2022, 1:59 PM
Brian
Brian - avatar
+ 4
Isbah int main() { int x=0, y=0; int z = (x+y); printf("%d",++z); } answer: 1
23rd Jul 2022, 2:28 PM
BroFar
BroFar - avatar
+ 3
++ is a unary operator.. can be applied to only a variable. Not to any constant or block... Just use cout << ( x+ y) ; oh.. what are trying there actually???
23rd Jul 2022, 1:26 PM
Jayakrishna 🇼🇳
0
#include <iostream> int main() { int x=0,y=0; std::cout<<++x + y++; return 0; } Increment operators didn't works on literals i guess :D
24th Jul 2022, 12:24 AM
Ujjawal Gupta
Ujjawal Gupta - avatar
0
The ++ is only accepted for a variable.
24th Jul 2022, 11:37 PM
DrDicht86
DrDicht86 - avatar