×

# 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 main() { int x=3; printf("%d",++x + ++x); }  asked 25 Jul '14, 19:28 2★sunny210 101●1●5●12 accept rate: 0% output be 6 (25 Jul '14, 22:55) 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) 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) 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) 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) showing 5 of 6 show all

 1 modern compiler does not accept these type of formats (++x + ++x).If you use turbo compiler than answer will be 15. answered 26 Jul '14, 08:53 17 accept rate: 0% u r right man.. (26 Jul '14, 09:17)
 0 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). answered 25 Jul '14, 19:45 3★prem_93 623●3●8●16 accept rate: 19% Shouldn't the incrementation work before evaluation of the expression making the value of x=6? (25 Jul '14, 20:09) 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★
 0 ++ is higher precedence than the Plus operator therefore after the increment the value and add the number answered 26 Jul '14, 00:54 1 accept rate: 0% Code exhibits UB. (26 Jul '14, 13:04) mjbpl5★
 0 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. answered 26 Jul '14, 09:05 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★
 0 answer will be 15. link This answer is marked "community wiki". answered 26 Jul '14, 09:24 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★
 0 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 answered 26 Jul '14, 11:22 1 accept rate: 0%
 0 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: Well defined: For ex value of i after i = 2 + 3 * 5; Implementation dependent: For ex value of sz in size_t sz = sizeof (int); Unspecified: For ex: Order of evaluation in, add(print("hi"), print("Hello")); Undefined: For ex: value of i afer i = 0; i = i++; :) [it can be 0 or 1 or 100 or -500] Locale specific: For example what would be printed when you do printf("\\"); If you still find something unclear, please ask. answered 26 Jul '14, 13:09 5★mjbpl 419●2●7 accept rate: 6%
 0 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. answered 05 Aug '14, 23:10 0★anuj18 1 accept rate: 0%
 0 In case of turbo compiler answer wil be 15 answered 06 Aug '14, 17:12 3★proxy_s 31●1●3 accept rate: 0%
 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 answered 10 Aug '14, 18:49 16●3 accept rate: 0%
 0 ans will be 10 as preincrement follows first change then use answered 29 Aug '14, 01:39 2★avi_nash 38●1 accept rate: 0%
 0 ++x means first increment if x=3 then ++3 is 4. ++x + ++x=4+5=9 answered 29 Aug '14, 13:04 0★kumar123 1 accept rate: 0%
 -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. answered 26 Jul '14, 11:59 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 answered 26 Jul '14, 12:27 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 community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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,470

question asked: 25 Jul '14, 19:28

question was seen: 1,628 times

last updated: 29 Aug '14, 13:04