Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2
Nah, this one is not undefined behaviour, just when you use x twice in the same expression. The meaning of ++x is: First increment x, then return the new value of x. x++ means: Make a copy of x, increment the original value of x and return the old copy of x. Using parenthesis doesn't make a difference here. y = 2*(++x); -> y = 2*3, x = 3. z = 2*(x++); -> z = 2*3, x = 4.
5th May 2021, 1:25 PM
Dennis
Dennis - avatar
+ 2
Using parenthesis is not going to change the behaviour of x++. It is going to return the old value of x, with or without them. x will only be changed after the fact.
5th May 2021, 1:29 PM
Dennis
Dennis - avatar
+ 2
In z, the x is incremented in post-increment form so the value of x is firstly used then increment. In y, there is pre-increment in x so firstly increase the value then use it. Example: x=2; y=2*(++x); --- i.e. 2*(3) because first increment then use z=2*(x++); --- i.e. 2*(3) because first use then increment
7th May 2021, 12:50 AM
Akash Singh
Akash Singh - avatar
5th May 2021, 12:01 PM
Atul [Inactive]
+ 1
After the operation of z variable x's value will be 4. I have done some more changes in the code just check it
5th May 2021, 12:12 PM
Atul [Inactive]
5th May 2021, 12:49 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
It is pre-increment and post-increment concept In variable y ,x value(2) is pre-incremented by 1 (i.e; 3) and assigned to variable y so it becomes 2*3=6 ........ After that x value(i.e; 3 ) is assigned to variable z so it becomes 2*3=6 and then x post-incremented by 1 then the value becomes 4 ...... Hopes it helps you
6th May 2021, 1:24 PM
RAMASWAMY KAMMARI
+ 1
x++ is a post-increment, in which the value of the variable 'x' is first used prior to the increment. ++x is a pre-increment, in which the value is incremented first prior to use of the variable. In the second line, y = 2 * (++x), the value of x is 3, because the expression uses a pre-increment operator and thus the value of y is 6. However in the third line, z = 2 * (x++), the value of x is still 3, since the expression uses a post-increment operator and hence the value is z is also 6. So those are the reasons why the output shows 6,6 and not 6,8.
6th May 2021, 9:44 PM
Marvin
Marvin - avatar
0
Hope this helps you
5th May 2021, 12:01 PM
Atul [Inactive]
0
CarrieForle Is my answer incorrect?
5th May 2021, 1:18 PM
Atul [Inactive]
0
Prince I suppose that the behavior of post increment is always the same in an expression, regardless of the usage of parentheses around it. The increment is done only after the expression is evaluated.
6th May 2021, 6:22 PM
Calvin Thomas
Calvin Thomas - avatar
0
Y=2*(++x) here it generate 6 and x=3 becoz of pre increment of x Now Y=2*(x++) =2*3=6 again it will generate the same as above due to post increment of x .In post increment first assign the value than increment it so after assigning it will remain same as 3 but if it ask about the value of x then it become 4..
7th May 2021, 2:03 AM
Ankita Kumari
- 1
Nah, this one is not undefined behaviour, just when you use x twice in the same expression. The meaning of ++x is: First increment x, then return the new value of x. x++ means: Make a copy of x, increment the original value of x and return the old copy of x. Using parenthesis doesn't make a difference here. y = 2*(++x); -> y = 2*3, x = 3. z = 2*(x++); -> z = 2*3, x = 4.
7th May 2021, 2:54 AM
atharva.9
atharva.9 - avatar