+ 4
Hello guys need your help in unary operators in c language while using increment and decrement operators.
different compiler used different methods please explain Mee how to solve such problems int y,x=3; y=++x + ++x; printed("%d %d", y,x);
6 Answers
+ 6
In C++, prefix operators are evaluated prior to the evaluation of an expression.
y = ++x + ++x;
// x is incremented twice via prefix increment operator, hence the value of x is now 5
// 5 + 5 is assigned to y
// y becomes 10
// final value of x is 5, y is 10.
+ 4
x++ means increment, ++x means decrement
ex if x=5 then value of x++ = 6,& ++x= 4;
ex 2- if x= 5 ,y= x++; then, y =4& y=++x; then y=6;
+ 3
@DILKHUSH KUMAR. x++ and ++x both are increment unary operators. But there is a big difference between them is that x++ is post increment while ++x is pre increment. You know that the priority order is : unary > arithmetic > assignment. but there is an exception x++(post increment) has lowest priority then assignment operator although it is unary operator.same is applicable in decrement operator.So order is : ( pre increment , pre decrement ) > arithmetic > ( post increment , post decrement )...so take above question.
In this statement there are total 4 operators
y=++x + ++x;
first is assignment operator(lowest priority)
second is arithmetic plus operator
third and fourth is pre increment operators ( highest priority ).
so, x is first incremented twice.
so x becomes 5.and
statement becomes y=5 + 5
so y becomes 10.
+ 3
oh sorry I forget to write assignment operator in the priority order as written my previous post...
so order is ,
(pre increment , pre decrement) > arithmetic > assignment > (post increment , post decrement)
+ 2
no akriti value of x=5 and y will become 10
+ 2
Dilkhush Kumar you got puzzled it's not that complicated it is simple try to understand