Simple C Code

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);
}
1 Like

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

1 Like

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
",++x + ++x + x++);

printf("%d
",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.

4 Likes

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

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

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) ©
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 © now come the value of © you have already incremented the x to 4 now you have the value of x = 4 before step © so after the increment at © it will be 5 so © {equal to 5}
now (A) (B) ©
4 + 5 = 9

2 Likes

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.

1 Like

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

  • 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++; :slight_smile: [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.

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

  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

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