Pre and post increment operators

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

Questions by Ruhani Chawlia   answers by Ruhani Chawlia

Showing Answers 1 - 36 of 36 Answers

thucdx

  • Jul 30th, 2012
 

the actual answer is 42.

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.    1              >----->-------.file>--"undef.c"<

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

  3.    3              >-----.LC0:<

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

  5. "<

  6.    4      25642C20-<

  7.    4      69203D20-<

  8.    4      25640A00-<

  9.    5              >----->-------.text<

  10.    6              >-----.globl main<

  11.    8              >-----main:<

  12.    9              >-----.LFB2:<

  13.   10 0000 55       >---->-------pushq>--%rbp<

  14.   11              >-----.LCFI0:<

  15.   12 0001 4889E5   >---->-------movq>---%rsp, %rbp<

  16.   13              >-----.LCFI1:<

  17.   14 0004 4883EC10 >---->-------subq>---$16, %rsp<

  18.   15              >-----.LCFI2:<

  19.   16 0008 C745F805 >---->-------movl>---$5, -8(%rbp)<

  20.   16      000000<

  21.   17 000f 8345F801 >---->-------addl>---$1, -8(%rbp)<

  22.   18 0013 8345F801 >---->-------addl>---$1, -8(%rbp)<

  23.   19 0017 8B45F8   >---->-------movl>----8(%rbp), %eax<

  24.   20 001a 0FAF45F8 >---->-------imull>---8(%rbp), %eax<

  25.   21 001e 8945FC   >---->-------movl>---%eax, -4(%rbp)<

  26.   22 0021 8B55F8   >---->-------movl>----8(%rbp), %edx<

  27.   23 0024 8B75FC   >---->-------movl>----4(%rbp), %esi<

  28.   24 0027 BF000000 >---->-------movl>---$.LC0, %edi<

  29.   24      00<

  30.  

  31.   25 002c B8000000 >---->-------movl>---$0, %eax<

  32.   25      00<

  33.   26 0031 E8000000 >---->-------call>---printf<

  34.   26      00<

  35.   27 0036 B8000000 >---->-------movl>---$0, %eax<

  36.   27      00<

  37.   28 003b C9       >---->-------leave<

  38.   29 003c C3       >---->-------ret<

  39.  

  Was this answer useful?  Yes

Nikita

  • Sep 2nd, 2012
 

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

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

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.

ex:

Code
  1. main()

  2.            {

  3.            int x,y;

  4.            x=5;

  5.            y=++x * ++x ;

  6.            printf("%d %d ", y,x);

  7.            getch();

  8.            }

  9.  

o/p:49,7

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.


ex:
main()
{
int x,y;
x=5;
y=x++ * x++;
printf("%d %d ", y,x;
getch();
}

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:

  Was this answer useful?  Yes

vinay

  • Sep 24th, 2014
 

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

  Was this answer useful?  Yes

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

Code
  1. #include <stdio.h>

  2.  

  3. void main(void){

  4.   int a=5;

  5.   int c = ++a * ++a;

  6.   printf("%d

  7. ", c);

  8.   printf("%d

  9. ",a);

  10.   c = a++ * ++a;

  11.   printf("%d

  12. ", c);

  13.   printf("%d

  14. ",a);

  15. }

  16.   Output :

  17.  

  18. 49

  19. 7

  20. 64

  21. 9

  22.  

  Was this answer useful?  Yes

Ankit Kumar

  • May 20th, 2015
 

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

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

Arijit Sarkar

  • Aug 17th, 2015
 

What is the answer of ++x * x++ * ++x for x=2?

Code
  1. int x=2, y;

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

  3. printf("%d",x);

  4.  

  Was this answer useful?  Yes

@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.

  Was this answer useful?  Yes

Mr.Rao

  • Dec 8th, 2015
 

y = 3 * 3 * 5
Ans: 45

  Was this answer useful?  Yes

anoop kumar pandey

  • Jan 18th, 2016
 

64

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

Vivek

  • Mar 30th, 2016
 

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

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

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?

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

VENNSPUSA.AKHIL REDDY

  • May 17th, 2017
 

X=5
Y=64

  Was this answer useful?  Yes

shashank

  • Jun 3rd, 2017
 

X = 5

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

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

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

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

  5. 4. y =64;

  6. 5. x=5;

  7.  

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

@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.

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

Akshaykumar

  • Jul 28th, 2017
 

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

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

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

  Was this answer useful?  Yes

Vishal

  • Sep 16th, 2017
 

The ans is 30,7

  Was this answer useful?  Yes

Akhil

  • Oct 3rd, 2017
 

Its giving 49 as mentioned in the question

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

Pujitha

  • Jun 27th, 2019
 

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

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions