You are not logged in. Please login at www.codechef.com to post your questions!

×

Simple C Code

1
1

Can anyone explain what is the output of the below code and how? What if there is one more ++x?

include<stdio.h>

main() { int x=3; printf("%d",++x + ++x); }

asked 25 Jul '14, 19:28

sunny210's gravatar image

2★sunny210
1011512
accept rate: 0%

output be 6

(25 Jul '14, 22:55) madhavkumar0★
1

Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

(26 Jul '14, 13:01) mjbpl5★

it will not run on the modern compiler, bt will runinthe older ones. When printf("%d",++x + ++x); is executed , ++x + ++x will return the value 10(5+5). bt if one more ++x is added to it(++x + ++x + ++x), it will return 18. Bt if after evaluting the printf ++x + ++x and the adding ++x will give 15.

(27 Jul '14, 13:01) tellmemayur1★

Ans is 15 man....because for first prefix the x value is incremented an in the meanwhile for next prefix the x value is again incremented.....and so for first ++x:4 Second ++x:5 ++x + ++x=9 and if ++x + ++x + ++x=15

(06 Aug '14, 14:05) karthiksomu0★

Answer is 9 because of prefix operatot.First it will increment the value of x to 4 then print.then current value of x is 4 again it get incremented by 1..so 4+5=9.

(10 Aug '14, 20:34) ruhikumari900★

Actually it depends on the compiler .On Dev C++ it gives 10 and on Turbo C++ the value is 9.

(10 Aug '14, 20:45) ruhikumari900★
showing 5 of 6 show all

the answer is 10. pre-increment operators are evaluated before evaluation of the actual expression. Here there are two pre-increment operators which change the value of x by incrementing it twice, as a result, its value changes to five (two increments). Then the addition operation takes place. So the new value of x is added twice (i.e. five plus five) giving an answer of 10.

Had there been three pre-increment operators,like ++x + ++x + ++x, then before the operation, the x would have been incremented three times changing its value to 6 and three additions would give the answer 18.

Another interesting thing to note is post-increment is evaluated after the expression. Consider this expression : ++x + ++x + x++

there are two pre-increment which change the value of x to five. The post increment does not change the value of x. So, there is an addition of three x's whose value is 5. So the result is 15. Then post-increment takes place making the value of x==6.

code:

x=3;

printf("%d\n",++x + ++x + x++);

printf("%d\n",x);

output is:

15 6                              // x is incremented after the evaluation of the expression

p.s. Try a few more variations and guess the answer before running the code and check your answer with your code.

link

answered 25 Jul '14, 19:53

dragonemperor's gravatar image

3★dragonemperor
89321135
accept rate: 10%

1

This is UB, check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

(26 Jul '14, 13:02) mjbpl5★

actually the answer will be 9 because first you have x which is equal to the 3 then you have ++x + ++x
lets divide this in three parts "++x" "+" "++x" (A) (B) (C) now the value of (A) is 4 because you have incremented x (=3) with 1 then (B) is the addition which will add (A) {which is equal to 4} with (C) now come the value of (C) you have already incremented the x to 4 now you have the value of x = 4 before step (C) so after the increment at (C) it will be 5 so (c) {equal to 5} now (A) (B) (C) 4 + 5 = 9

link

answered 25 Jul '14, 20:09

geeksoul's gravatar image

1★geeksoul
3013
accept rate: 0%

Please run the code in any compiler and check the output.

(25 Jul '14, 20:14) dragonemperor3★
2

the answer is 9 in turbo C

(25 Jul '14, 20:17) geeksoul1★

what about ++x + ++x + ++x ???

(25 Jul '14, 20:24) dragonemperor3★

in turbo c ?

(25 Jul '14, 20:24) dragonemperor3★

++x + ++x + ++x in turbo C is 15 as expected ( 4 + 5 + 6) and further more will be like this 4+5+6+7+8+9+10..

(25 Jul '14, 20:33) geeksoul1★
1

even in C# ++x + ++x is 9 in Visual Studio express

(25 Jul '14, 20:33) geeksoul1★
1

@geeksoul Suprising! Thanks for the info that was helpful.

(25 Jul '14, 20:39) sunny2102★

Yes, It is 15 in turbo c

(25 Jul '14, 20:50) dragonemperor3★

Thanks @sunny210 :) I am happy that I could help you :)

(25 Jul '14, 20:52) geeksoul1★

@dragonemperor even in C# it is 15 but I am still not getting how could ++x + ++x is equal to 10? which compiler you are using?

(25 Jul '14, 20:55) geeksoul1★

gcc compiler

(25 Jul '14, 21:04) dragonemperor3★

Code is UB. check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

(26 Jul '14, 13:02) mjbpl5★
showing 5 of 12 show all

modern compiler does not accept these type of formats (++x + ++x).If you use turbo compiler than answer will be 15.

link

answered 26 Jul '14, 08:53

aajain420's gravatar image

0★aajain420
17
accept rate: 0%

u r right man..

(26 Jul '14, 09:17) imshashanksj0★

for the given code the output will be 10....if there is one more ++x it will be 16. in the first case the two operands for addition are the values in x itself. so ans=2*(value present in x). now x is incremented 2 times,hence value in x =5. therefore ans=2(5)=10 if one more ++x is present , then value in x is incremented and then added with 10,now already x has been incremented twice ,so after another increment it will have 6.hence ans=10 +6=16(in the second case).

link

answered 25 Jul '14, 19:45

prem_93's gravatar image

3★prem_93
6233816
accept rate: 19%

edited 25 Jul '14, 19:51

Shouldn't the incrementation work before evaluation of the expression making the value of x=6?

(25 Jul '14, 20:09) dragonemperor3★

The code is UB. Check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

(26 Jul '14, 13:02) mjbpl5★

@ dragonemperor As you said the answer in the second case isn't 18 but 16. Any explanation will be helpful...thanks!

link

answered 25 Jul '14, 20:02

sunny210's gravatar image

2★sunny210
1011512
accept rate: 0%

Can you please check the answer in any non-gcc compiler like turbo c++ or dev cpp? I have only gcc and getting 16 as answer. My concept might be wrong.

(25 Jul '14, 20:07) dragonemperor3★

Same here. I'll check though. I presume that doesn't make any difference right!!

(25 Jul '14, 20:09) sunny2102★

Some times, different compilers give different answers if compiler is not designed according to ISO/ANSI standards. But this should not be the case in gcc though.

(25 Jul '14, 20:13) dragonemperor3★

Any idea which complier among Dev C++, Turbo C is designed according to ANSI standards?

(25 Jul '14, 20:41) sunny2102★

Neither, they have added lots of their own features. GCC is in accordance with the standard. I checked the answer on various compilers and got 16 as answer, but in turbo c, the answer was 15. Sorry for my wrong answer.

(25 Jul '14, 20:50) dragonemperor3★

@ prem_93 From your explanation for the first case the answer for the second one should be 3*6 right!! What if there is one more ++x?

link

answered 25 Jul '14, 20:05

sunny210's gravatar image

2★sunny210
1011512
accept rate: 0%

printf("%d\n",++x + ++x + x++); This will not print 18 but it prints 16.the explanation is : ADDITION OPERATOR TAKES IN ONLY TWO INPUTS AT A TIME... In the first case, there are only two operands and hence ans is 10.

But in the second case,there are 3 operands .now the operation is performed on the first two operands and the answer is stored in a temp register,this value is 10.For the second addition operation value stored in temp register and the new operand are added up.the new operand is ++x(which will be 6)(NOTE:performing ++x will not change temp value(10)) and hence answer is 10 +6=16.

(25 Jul '14, 20:14) prem_933★

if there is another ++x then this 16 value(from the previous case) is stored in the temp register and the new operand ++x(which will now be'7')is added with it and hence answer would be 16+7 =23. if this is followed by another ++x ans would be 23+8=31 and so on. i think this would help,if you still hve any doubt comment below..if you found my post helpful upvote and mark it as accepted answer..

(25 Jul '14, 20:14) prem_933★

++ is higher precedence than the Plus operator therefore after the increment the value and add the number

link

answered 26 Jul '14, 00:54

dipak7587's gravatar image

0★dipak7587
1
accept rate: 0%

Code exhibits UB.

(26 Jul '14, 13:04) mjbpl5★

For the given code answer should be 5.. Because earlier x=3(as per initialisation) Then next operation ++x means that it will increment the value first then it will store ++3 which equals 5 is stored in memory then next operation x++ means it will first store then increment that is 5 will be incremeneted but not stored in the memory. So it gives output 5.. Just execute it you will be more clear on my answer.

link

answered 26 Jul '14, 09:05

rudrika_20's gravatar image

0★rudrika_20
1
accept rate: 0%

code has UB. Check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

(26 Jul '14, 13:04) mjbpl5★

answer will be 15.

link
This answer is marked "community wiki".

answered 26 Jul '14, 09:24

imshashanksj's gravatar image

0★imshashanksj
0
accept rate: 0%

Code exhibits UB. Check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

(26 Jul '14, 13:05) mjbpl5★

the answer is 10 As we know that ihis is the case of preincement so in ++a + ++a the value will be first inc. then assign

link

answered 26 Jul '14, 11:22

pappukumar052's gravatar image

4★pappukumar052
1
accept rate: 0%

edited 26 Jul '14, 11:25

Many answers are posted for this quesion and all are wrong in the same way.

something = ++x + ++x;  /* This assignment takes place internally while calling function */

Above line of code exhibits undefined behavior. For an LOC(line of code) with undefined behavior, you can not question why, how etc. (Output differs with compiler, differs with time etc)

LOC of interest exhibits undefined behavior because variable i is being updated twice before next sequence point is reached.

This faq is worth a read for you.

C99 standard

  • 6.5 Expressions, §2

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be read only to determine the value to be stored.

  • 6.5.16 Assignment operators, §4:

The order of evaluation of the operands is unspecified. If an attempt is made to modify the result of an assignment operator or to access it after the next sequence point, the behavior is undefined.

p.s. Any LOC in C language can show one of 5 behaviors given below:

  1. Well defined: For ex value of i after i = 2 + 3 * 5;
  2. Implementation dependent: For ex value of sz in size_t sz = sizeof (int);
  3. Unspecified: For ex: Order of evaluation in, add(print("hi"), print("Hello"));
  4. Undefined: For ex: value of i afer i = 0; i = i++; :) [it can be 0 or 1 or 100 or -500]
  5. Locale specific: For example what would be printed when you do printf("\\");

If you still find something unclear, please ask.

link

answered 26 Jul '14, 13:09

mjbpl's gravatar image

5★mjbpl
41927
accept rate: 6%

the answer is 9. because (++/operator) is added before the variable which means it increment the value first the store to give a o/p.

link

answered 05 Aug '14, 23:10

anuj18's gravatar image

0★anuj18
1
accept rate: 0%

In case of turbo compiler answer wil be 15

link

answered 06 Aug '14, 17:12

proxy_s's gravatar image

3★proxy_s
3113
accept rate: 0%

firstly ++x increase value of x by 1 then add all x; their is increament in x every time so last value of x get added 1. ++x --> 4 2. ++x --> 5 now value of x is 5 '++x + ++x' is 10 and if their is again a ++x then 3. ++x --> 6 now ans is 15

link

answered 10 Aug '14, 18:49

swapdewalkar's gravatar image

2★swapdewalkar
163
accept rate: 0%

ans will be 10 as preincrement follows first change then use

link

answered 29 Aug '14, 01:39

avi_nash's gravatar image

2★avi_nash
381
accept rate: 0%

++x means first increment if x=3 then ++3 is 4. ++x + ++x=4+5=9

link

answered 29 Aug '14, 13:04

kumar123's gravatar image

0★kumar123
1
accept rate: 0%

-1

What will be the answer for this one :

int x=5;

printf("%d", ++x + x++ + ++x + ++x);

link

answered 25 Jul '14, 20:59

dragonemperor's gravatar image

3★dragonemperor
89321135
accept rate: 10%

29 6 (Converted in 6 from 5) + 6 (still 6 but now converted in 7) + 8 (converted 7 to 8) + 9 (converted 8 to 9)

(25 Jul '14, 21:07) geeksoul1★

29 is the answer

(25 Jul '14, 21:07) geeksoul1★

its 30 in gcc. Try it from right->left order.

(25 Jul '14, 21:10) dragonemperor3★

yes from right to left it is 30 (6 + 7 + 7 (converted into 8) + 10) btw can you give me the link to download gcc (I know this is not the right place but I really need it and you are right from right to left your answer is right while in left to right order my answers are right)

(25 Jul '14, 21:15) geeksoul1★

just google it.

(25 Jul '14, 21:21) dragonemperor3★

@ geeksoul Actuallly the answer is 27 on gcc. Regarding GCC go check out this link http://www.mingw.org/category/wiki/download

(25 Jul '14, 21:24) sunny2102★

@sunny210 might be result in gcc is 27 but according to right to left approach 30 is correct while according to left to right approach 29 is the best answer

(25 Jul '14, 21:28) geeksoul1★

Its 30. I tried it on gcc before posting it. Can you explain the order of evaluation in which the answer is 27?

(25 Jul '14, 21:28) dragonemperor3★

@sunny210 thanks for the link :)

(25 Jul '14, 21:28) geeksoul1★
showing 5 of 9 show all
-1

output will be 9...........as the expression is (++x + ++x) so when the first operand is fetched then x is first incremented to 4 and then read and then again when for the next x it is again incremented to 5 and fetched....and finally simply added.

If one more ++x is added to the equation it would be 15....as the next ++x would be fetched as 6.

link

answered 26 Jul '14, 11:59

ankandutta's gravatar image

1★ankandutta
0
accept rate: 0%

Code has UB. Check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

(26 Jul '14, 13:05) mjbpl5★
-2

29 is the answer for sure ..as (5+1)=6+6+8(inc the stored value 7)+9(inc 8 by 1)=29

link

answered 26 Jul '14, 12:27

san10's gravatar image

0★san10
-1
accept rate: 0%

No, the code has UB. Check Your program exhibits undefined behaviour so it may give 6 or 7 or 8 or something else as output. Best advice, never do this. http://discuss.codechef.com/questions/41774/post-increment-variable-i-and-assigned-it-to-the-same-variable-i-i?page=1#41822

(26 Jul '14, 13:06) mjbpl5★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,490

question asked: 25 Jul '14, 19:28

question was seen: 1,663 times

last updated: 29 Aug '14, 13:04