What will be the answer for this one :
int x=5;
printf("%d", ++x + x++ + ++x + ++x);
What will be the answer for this one :
int x=5;
printf("%d", ++x + x++ + ++x + ++x);
++ is higher precedence than the Plus operator therefore after the increment the value and add the number
modern compiler does not accept these type of formats (++x + ++x).If you use turbo compiler than answer will be 15.
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.
answer will be 15.
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
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.
29 is the answer for sure …as
(5+1)=6+6+8(inc the stored value 7)+9(inc 8 by 1)=29
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
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.
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:
If you still find something unclear, please ask.
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.
In case of turbo compiler answer wil be 15
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
ans will be 10 as preincrement follows first change then use
++x means first increment if x=3 then ++3 is 4.
++x + ++x=4+5=9
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.
Shouldn’t the incrementation work before evaluation of the expression making the value of x=6?
Same here. I’ll check though. I presume that doesn’t make any difference right!!
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.
Please run the code in any compiler and check the output.
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.