Hello guys need your help in unary operators in c language while using increment and decrement operators. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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);

23rd Jun 2017, 2:12 PM
Mohit Sharma
Mohit Sharma - avatar
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.
23rd Jun 2017, 2:35 PM
Hatsy Rei
Hatsy Rei - avatar
+ 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;
8th Jul 2017, 3:14 PM
DILKHUSH KUMAR
DILKHUSH KUMAR - avatar
+ 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.
9th Jul 2017, 6:04 AM
Devesh Kumar
Devesh Kumar - avatar
+ 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)
9th Jul 2017, 3:37 PM
Devesh Kumar
Devesh Kumar - avatar
+ 2
no akriti value of x=5 and y will become 10
7th Jul 2017, 10:46 AM
Mohit Sharma
Mohit Sharma - avatar
+ 2
Dilkhush Kumar you got puzzled it's not that complicated it is simple try to understand
8th Jul 2017, 3:28 PM
Mohit Sharma
Mohit Sharma - avatar