Note that the priority of pre increment is higher than the multiplication.

So, from left to right of (++i * ++i)
1. ++i executed first, result 6 and i = 6.
2. the second (++i) executed, result 7 and i = 7.
3. the product of 6 and 7 is 42.

The behavior of "++i * ++i" is undefined, and the result will vary from implementation to implementation. Refer to the C language standard, section 6.5, paragraphs 2 and 3. The behavior of modifying the value of an object through the evaluation of an expression more than once between sequence points is undefined. The behavior of using the value of an object in one expression when its being modified in another expression without an intervening sequence point is also undefined.

So, the result of expressions like ++i * ++i is undefined, but so is the result of expressions like a[i] = i++ (or a[i++] = i).

Why is this the case? With few exceptions, C does *not* guarantee that expressions are evaluated from left to right, nor does it guarantee that side effects are applied *immediately* after an expression has been evaluated. For example, given the expression

z = ++x * y++

the following sequence of operations is allowed:

t0 = y
t1 = x + 1
z = t1 * t0
y = y + 1
x = x + 1

gcc on SLES 10 also gives me 49 as opposed to the expected 42. See the generated assembly below to understand why; i gets updated twice *before* the multiplication happens.

Code

1>----->-------.file>--"undef.c"<

2>----->-------.section>-------.rodata<

3>-----.LC0:<

40000 7A203D20 >---->-------.string>"z = %d, i = %d

Start solving from right to left their are two incr. so value=7 and then place it to expression.

Amita Negi

Sep 6th, 2012

Priority of pre increment is high so:
1. firstly ++i is executed then i=6;
2. then again ++i is executed and i=7;
3. and then the multiplication is done so i*i or 7*7 is 49.

Jaspal

Sep 27th, 2012

it increments i by 1i.e i becomes 6 in first subexpression and becomes in 7 in second subexpression.finally 42 is answer

Malli

Aug 21st, 2013

Why does ++x * ++x give 49 when x=5?

If you directly give this in printf stmt it gives you the result as 42,7
x=5
printf("%d %d ",++x * ++x,x)

But if you assign this to some variable and then you print, it gives the resutls as 49 which is correct.

Solution: first x=5;
then ++x pre increment operator will have higher preference than = operator.So x will get incremented twice and stored in the memory location as 7.
so now the value will be picked up from the memory location and Y will be calculated as 7*7 which is 49.
Hence the o/p is 49,7.

solution:
Since = will have higher preference than post incrementing operator x++, fisrt assignment will take place.
so y=5*5=25;

Then because of 2 post incremnations in the stmt, finally x will get incremented to 7 in the memory locations.

Hence the o/p is 25,7

Always remember the calculation of the expression when u assign to a variable differs when u directly printing it in the printf stmt.
Hope this helps:

vinay

Sep 24th, 2014

ans: 42
i=5;
++i * ++i
6 7

Mahesh

Apr 24th, 2015

It does not create a copy of the variable, Also it is acting on the same variable a, So You need to consider number of preincrement in the sentence acting on same variable and consider that as final value for all the values of a , a++ , ++a .... , Sometimes it also depends on compiler implementation so its better to verify on that compiler

Ans is 49 correct
In pre increment ++i (the process flows as 1. Increment > 2. Processing > 3. Assignment)
So first ++ means 5 becomes 6 and then again ++ means 6 becomes 7
hence 7*7 = 49

Kritika SK

Jun 12th, 2015

According to precedence of operators in C, pre-increment operator has higher precedence than binary multiplication(*) operator. So in this case first both increment are perormed first and then multiplication of the values. Initially value of i is 5. After incrementing two times its value becomes 7. 7*7=49. This is how we get 49 out of this expression.

@Arijit Sarkar: the behavior is *undefined*; the answer could be anything from 12 to 64. Operator precedence only controls the grouping of operators and operands; it *does not* control the order in which subexpressions are evaluated or the order in which side effects are applied. Do not assume that expressions are always evaluated left to right, and do not assume that the side effects of ++ and -- are applied immediately.

Mr.Rao

Dec 8th, 2015

y = 3 * 3 * 5
Ans: 45

anoop kumar pandey

Jan 18th, 2016

64

ragu

Feb 4th, 2016

++i * ++i
first evaluate pre-Increment operation
then replace with recent value then do the arithmetic opeartions
first ++i=6
second ++i=7
so 7*7=49

Answer depends upon the programming language that is being used
In C/C++/C# answer would be 7*7=49
But if you code in Java answer would be 7*6=42

rohan,,,,

May 19th, 2016

With the 1st increment the value of i becomes 6... and with 2nd increment the value becomes 7 and its gets updated in the 1st operand also... so 7*7=49

shivam_sharma

Jun 16th, 2016

The ans of ++x * ++x is same as ++x * x according to all those with ans 49 @ x=5 for the former ; this means that pre-increment or no increment are same. How is this possible?

anurag

Jul 7th, 2016

For pre-increment first we add the number of times it is pre-incremented so
5+2=7
Then 7*7=49

VENNSPUSA.AKHIL REDDY

May 17th, 2017

X=5
Y=64

shashank

Jun 3rd, 2017

X = 5

Code

y =++x *x++*++x;

1. y=++x * x++*3;

2. y=4* x++*4;

3. y=4*4*4;( x will post increment and pend untill this statement get executed).

Does it not bother anyone that people are getting completely different answers for this? Is anyone actually reading the other comments? When I execute Arijits expression "y = ++x * x++ * ++x;" on my system, I get y = 36 and x = 5. Not 64, not 45. Different people get different results BECAUSE THE BEHAVIOR IS UNDEFINED. Modifying an object more than once without an intervening sequence point gives unpredictable results. *Any* expression of the forms "x = x++", "x++ * x++", "a[i] = i++", or "a[i++] = i" is *erroneous*, and the behavior is *undefined*. Yes, you will get *a* result, but it may or may not be the result you expect.

yogendra singh

Jun 11th, 2017

output will be 30,7 post increment have highest precedence so it will evaluated 1st. at a time only one operator can evaluated. here the solution
x=5; y=x++ * x++ y=5 * x++ /* 5 is assigned and value of x becomes 6
y= 5 *6 /* 6 is assigned and value of x is incremented and becomes 7
here the output y=30,x=7

@yogendra singh - precedence does not control order of evaluation, it only controls which operands are grouped with which operators. There is no guarantee that the second x++ will be evaluated before the first one, nor is it guaranteed that the side effect of the ++ operator is applied immediately after evaluation. Once again, the behavior is *undefined*. There is no correct answer.

Pre increment means first update the value then use this value.
according to question ++i*++i where i=5
Its left ascending operator its increment ++i, is value now 6 then use this 6 as is new value and provide this value to right is. again here pre increment so this value should be updated then use so final expression is 6*7, its give 42.

Jitendra Shelar

Jul 23rd, 2017

Answer for x=5 and y=x++ * x++ is 30,7
Ans for x=2 and y=++x * x++ * ++x is 5,60

Akshaykumar

Jul 28th, 2017

++i=6 and i=6 then ++i=7 so 6*7=42 when i=5 firstly

sagar puri

Aug 1st, 2017

See we are available with i=5; so
++i * ++i = 49
as
++i executes first, result 6 and i= 6;
the second (++i) executed , and the result is 7 so the initialization done for int i is 7
therefore i*i =
7*7= 49

Ankit jangid

Aug 10th, 2017

as per rule y=++x * x++ * ++x; if we tabke x=2;
y= 3 * 3 * 4;
y= 36
and x = 5

Dnyaneshwar

Sep 7th, 2017

i=5; so
++i * ++i ...
++i executes first, result 6 and i= 6;
the second (++i) executed ,and value of i = 7 .
and in our expression there is only one i variable and only one block created for i variable and in that block last value is 7. that means when 7 is assign to i old value of i is updated with 7.
therefore i*i =
7*7= 49

Vishal

Sep 16th, 2017

The ans is 30,7

Akhil

Oct 3rd, 2017

Its giving 49 as mentioned in the question

Sheetal Khatri

Nov 13th, 2017

At first pre increment operator will increment the value of i, here first ++i will make the value 6 then 2nd ++i will make the value 7, now this value will replace the expression. so 7*7=49
Note that ++i (pre increment operator) will first increment and then evaluate.

Ankush Batra

Nov 1st, 2018

the ans is 49 only
bcoz i cant be 2 values(6, 7) at a time so, i should be 7 at last
thats why 7*7 = 49.

Pujitha

Jun 27th, 2019

i=5;
++i * ++i; (6*7)(if the i is the value 7)
7*7;
49

## Pre and post increment operators

Ruhani ChawliaProfile Answers by Ruhani Chawlia Questions by Ruhani Chawlia

Questions by Ruhani Chawlia answers by Ruhani Chawlia

## Related Answered Questions

## Related Open Questions