Output of a++ + ++a + a++

What is the answer of a++ + ++a + a++

int a=5;

c = a++ + ++a + a++;

//compiler produces the answer 28!!!
//How

Questions by crackboy   answers by crackboy

Showing Answers 1 - 75 of 134 Answers

austin1030

  • Feb 11th, 2008
 

Actually, the answer is 19

given a = 5;

first a++ gives 5 but increment by 1 after accessing variable 'a'

then, ++a give 7 since 'a' is 6 after first access

last, a++ give another 7 because it does not increment not until 'a' access

so , c = 5 + 7 + 7 = 19

Here the value of a=8 if a=5;
and c=18 if c=a++ + ++a + a++
because a will evaluate thrice so a=8 and for c first take a++ + ++a here 5+6=11
and one post increment of a
so a=7 again 11+a++=11+7=18 and one a++=8;

From the C Language standard, section 6.5:

----------
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. 72)  Furthermore, the prior value shall be read only to determine the value to be stored. 73)

...

72) A floating-point status flag is not an object and can be set more than once within an expression.

73) This paragraph renders undefined statement expressions such as

    i = ++i + 1;
    a[i++] = i;

while allowing

    i = i + 1;
    a[i] = i;
-------------

Emphasis added.

The expression "a++ + ++a + a++" violates this restriction, so the behavior is undefined; any result is considered "correct", whether it's 16, 18, 28, 42, a suffusion of yellow, etc.  The compiler isn't even obligated to finish translating the program. 

Exactly how a particular result is produced will vary from platform to platform, and may depend on everything from the surrounding code to compiler options. 

Follow the same rule for the second equation by breaking it down.
a++ (a is now 5, does it appear before +, yes then return 6).
equation is now 6 + ++a + a++
++a (a is now already 6 because was incremented in the previous equation so this returns 7 because 6++, but remember it does not return to the original value as it is already as it is incremented on the spot and a remains 6)
equation is now 6 + 7 + a++
a++ (a is now 6, does i appear before +, no then return 6 as it is not incremented yet.
equation is 6 + 7 + 6 = 19.

  Was this answer useful?  Yes

jaspreet kaur

  • Jul 14th, 2011
 

in unix enviornment its answer is 18.

  Was this answer useful?  Yes

satya

  • Jul 22nd, 2011
 

hey...... it wont print 28 in any programming language. please try again. It will print 18 in C/c++ and java it will print 19.

  Was this answer useful?  Yes

GARGI GANGULY

  • Sep 10th, 2011
 

At First a is incremented a=6; but its a post increment so value count as a 5. then a is used as pre increment the value become 7 and last one is also 7, because it is also post increment. So the format is c=7+7+5. Output is 19.

  Was this answer useful?  Yes

rinsdominic

  • Dec 28th, 2011
 

The precedence of unary operator ++ is from right to left
therefore first evaluate the section ++a + a++
ie, from right side value of a is 5 and in left value of a is 6 because preincrement and calculate the value 6+5=11
then perform the postincrement therefore value of a become 7 then perform the rest in the left portion
ie the current value of a is added with the intermediate result ie, 7+11= 18 .. then perform the post increment of a therefore a become 8
try this

Code
  1. #include<stdio.h>

  2. int main()

  3. {

  4.  int a=5,c;

  5.  c = a++ + ++a + a++;

  6.  printf("Value of a= %d",a);

  7.  printf("

  8. value of c= %d",c);

  9.  return 0;

  10. }

  • Dec 29th, 2011
 

Sorry for the first misunderstanding....
First evaluate the section a++ + ++a ie, from left side value of a is 5 and in right value of a is 6 because preincrement and calculate the value 5+6=11 then perform the postincrement of left a therefore value of a become 7 then perform the rest in the right portion ie the current value of a is added with the intermediate result ie, 11+7= 18 .. then perform the post increment of a therefore a become 8 try this

Code
  1.  

  2.     #include<stdio.h>

  3.     int main()

  4.     {

  5.      int a=5,c;

  6.      c = a++ + ++a + a++;

  7.      printf("Value of a= %d",a);

  8.      printf("

  9.    value of c= %d",c);

  10.      return 0;

  11.     }

  Was this answer useful?  Yes

vamshi

  • Jan 27th, 2012
 

Answer is 20
How means : It first performs pre-increment i.e a=6, now it performs addition i.e 6+6+6=18, than it has to perform post increment so the value of a is 20.

  Was this answer useful?  Yes

ankur

  • Feb 1st, 2012
 

for a++ + ++ a

first a value will become 6 so addition would be 12 now since a is already 6 it value will increase to seven then addition will take place so now addition will be 12+7=19 and the value is a is 8 because of increment after evaluation in case of a++

  Was this answer useful?  Yes

Just to drive home the point I made earlier...

The order in which operands are evaluated and side effects are applied is *unspecified*; given a statement like "y = x++ + ++x", theres no guarantee that "x++" is evaluated before "++x", or that the side effects of the two expressions are applied in any specific order.

Ive written a small C program that compares the results of "y = x++ + x++;" to "y = x++; y = y + x++;". If the ++ operator behaved the way everyone else has been describing, the results *should* be the same. But if you look at the results of the program (attached below as part of the code snippet), youll see that in many cases, the results *arent* the same.

Again, expressions of the form "x++ + x++" *will* invoke undefined behavior, and the results will vary based on your compiler, any optimization settings, surrounding code, etc. This code was built on Ubuntu 10.04 using gcc 4.4.3 on an AMD x86 processor.

Code
  1. /************************** undef.c **************************/

  2.  

  3. #include <stdio.h>

  4.  

  5. #ifndef START_VAL

  6. #define START_VAL 0

  7. #endif

  8.  

  9. #ifndef AUTOOP1

  10. #define AUTOOP1 ++

  11. #endif

  12.  

  13. #ifndef AUTOOP2

  14. #define AUTOOP2 ++

  15. #endif

  16.  

  17. #ifndef OP

  18. #define OP +

  19. #endif

  20.  

  21.  

  22. #define EXPAND(x) x

  23. #define PASTE(a,b) EXPAND(a)b

  24.  

  25. #define POSTIFY(x,y) PASTE(x,y)

  26. #define PREIFY(x,y) PASTE(y,x)

  27.  

  28. #if defined(DIR1)

  29.   #if DIR1==0

  30.     #define AUTO1(x) PREIFY(x,AUTOOP1)

  31.   #else

  32.     #define AUTO1(x) POSTIFY(x,AUTOOP1)

  33.   #endif

  34. #else

  35.   #define AUTO1(x) POSTIFY(x,AUTOOP1)

  36. #endif

  37.  

  38. #if defined(DIR2)

  39.   #if DIR2==0

  40.     #define AUTO2(x) PREIFY(x,AUTOOP2)

  41.   #else

  42.     #define AUTO2(x) POSTIFY(x,AUTOOP2)

  43.   #endif

  44. #else

  45.   #define AUTO2(x) POSTIFY(x,AUTOOP2)

  46. #endif

  47.  

  48. #define STRINGIFY(x) #x

  49. #define STREXPAND(x) STRINGIFY(x)

  50. #define AUTOSTR(x) STRINGIFY(x)

  51.  

  52. int main(void)

  53. {

  54.   int x = START_VAL;

  55.   int y;

  56.  

  57.   printf("%-20s%8s%8s\n", "Statement", "    y", "    x");

  58.   printf("%-20s%8s%8s\n", "---------", "-----", "-----");

  59.  

  60.   /* well-defined */

  61.   y = AUTO1(x);

  62.   y = y OP AUTO2(x);

  63.   printf("%-20s   %5d   %5d\n", "y = " AUTOSTR(AUTO1(x)) "; y = y " AUTOSTR(OP) " " AUTOSTR(AUTO2(x)), y, x);

  64.  

  65.   /* undefined */

  66.   x = START_VAL;

  67.   y = AUTO1(x) OP AUTO2(x);

  68.   printf("%-20s   %5d   %5d\n\n", "y = " AUTOSTR(AUTO1(x)) " " AUTOSTR(OP) " " AUTOSTR(AUTO2(x)), y, x);

  69.   return 0;

  70. }

  71.  

  72. /************* permute.sh **************/

  73. #!/bin/sh

  74.  

  75. for a1 in ++ --

  76. do

  77.   for d1 in 1 0

  78.   do

  79.     for a2 in ++ --

  80.     do

  81.       for d2 in 1 0

  82.       do

  83.         for op in + -

  84.         do

  85.           gcc -o undef undef.c -DAUTOOP1=${a1} -DAUTOOP2=${a2} -DDIR1=${d1} -DDIR2=${d2} -DOP=${op} -DSTART_VAL=0

  86.           ./undef

  87.         done

  88.       done

  89.     done

  90.   done

  91. done

  92.  

  93. /************************ Results ***************************/

  94.  

  95. Statement                  y       x

  96. ---------              -----   -----

  97. y = x++; y = y + x++       1       2

  98. y = x++ + x++              0       2

  99.  

  100. Statement                  y       x

  101. ---------              -----   -----

  102. y = x++; y = y - x++      -1       2

  103. y = x++ - x++              0       2

  104.  

  105. Statement                  y       x

  106. ---------              -----   -----

  107. y = x++; y = y + ++x       2       2

  108. y = x++ + ++x              2       2

  109.  

  110. Statement                  y       x

  111. ---------              -----   -----

  112. y = x++; y = y - ++x      -2       2

  113. y = x++ - ++x              0       2

  114.  

  115. Statement                  y       x

  116. ---------              -----   -----

  117. y = x++; y = y + x--       1       0

  118. y = x++ + x--              0       0

  119.  

  120. Statement                  y       x

  121. ---------              -----   -----

  122. y = x++; y = y - x--      -1       0

  123. y = x++ - x--              0       0

  124.  

  125. Statement                  y       x

  126. ---------              -----   -----

  127. y = x++; y = y + --x       0       0

  128. y = x++ + --x             -2       0

  129.  

  130. Statement                  y       x

  131. ---------              -----   -----

  132. y = x++; y = y - --x       0       0

  133. y = x++ - --x              0       0

  134.  

  135. Statement                  y       x

  136. ---------              -----   -----

  137. y = ++x; y = y + x++       2       2

  138. y = ++x + x++              2       2

  139.  

  140. Statement                  y       x

  141. ---------              -----   -----

  142. y = ++x; y = y - x++       0       2

  143. y = ++x - x++              0       2

  144.  

  145. Statement                  y       x

  146. ---------              -----   -----

  147. y = ++x; y = y + ++x       3       2

  148. y = ++x + ++x              4       2

  149.  

  150. Statement                  y       x

  151. ---------              -----   -----

  152. y = ++x; y = y - ++x      -1       2

  153. y = ++x - ++x              0       2

  154.  

  155. Statement                  y       x

  156. ---------              -----   -----

  157. y = ++x; y = y + x--       2       0

  158. y = ++x + x--              2       0

  159.  

  160. Statement                  y       x

  161. ---------              -----   -----

  162. y = ++x; y = y - x--       0       0

  163. y = ++x - x--              0       0

  164.  

  165. Statement                  y       x

  166. ---------              -----   -----

  167. y = ++x; y = y + --x       1       0

  168. y = ++x + --x              0       0

  169.  

  170. Statement                  y       x

  171. ---------              -----   -----

  172. y = ++x; y = y - --x       1       0

  173. y = ++x - --x              0       0

  174.  

  175. Statement                  y       x

  176. ---------              -----   -----

  177. y = x--; y = y + x++      -1       0

  178. y = x-- + x++              0       0

  179.  

  180. Statement                  y       x

  181. ---------              -----   -----

  182. y = x--; y = y - x++       1       0

  183. y = x-- - x++              0       0

  184.  

  185. Statement                  y       x

  186. ---------              -----   -----

  187. y = x--; y = y + ++x       0       0

  188. y = x-- + ++x              2       0

  189.  

  190. Statement                  y       x

  191. ---------              -----   -----

  192. y = x--; y = y - ++x       0       0

  193. y = x-- - ++x              0       0

  194.  

  195. Statement                  y       x

  196. ---------              -----   -----

  197. y = x--; y = y + x--      -1      -2

  198. y = x-- + x--              0      -2

  199.  

  200. Statement                  y       x

  201. ---------              -----   -----

  202. y = x--; y = y - x--       1      -2

  203. y = x-- - x--              0      -2

  204.  

  205. Statement                  y       x

  206. ---------              -----   -----

  207. y = x--; y = y + --x      -2      -2

  208. y = x-- + --x             -2      -2

  209.  

  210. Statement                  y       x

  211. ---------              -----   -----

  212. y = x--; y = y - --x       2      -2

  213. y = x-- - --x              0      -2

  214.  

  215. Statement                  y       x

  216. ---------              -----   -----

  217. y = --x; y = y + x++      -2       0

  218. y = --x + x++             -2       0

  219.  

  220. Statement                  y       x

  221. ---------              -----   -----

  222. y = --x; y = y - x++       0       0

  223. y = --x - x++              0       0

  224.  

  225. Statement                  y       x

  226. ---------              -----   -----

  227. y = --x; y = y + ++x      -1       0

  228. y = --x + ++x              0       0

  229.  

  230. Statement                  y       x

  231. ---------              -----   -----

  232. y = --x; y = y - ++x      -1       0

  233. y = --x - ++x              0       0

  234.  

  235. Statement                  y       x

  236. ---------              -----   -----

  237. y = --x; y = y + x--      -2      -2

  238. y = --x + x--             -2      -2

  239.  

  240. Statement                  y       x

  241. ---------              -----   -----

  242. y = --x; y = y - x--       0      -2

  243. y = --x - x--              0      -2

  244.  

  245. Statement                  y       x

  246. ---------              -----   -----

  247. y = --x; y = y + --x      -3      -2

  248. y = --x + --x             -4      -2

  249.  

  250. Statement                  y       x

  251. ---------              -----   -----

  252. y = --x; y = y - --x       1      -2

  253. y = --x - --x              0      -2

  254.  

  Was this answer useful?  Yes

govind bang

  • Feb 6th, 2012
 

i think answer is 20 bcoz a++ means a+1
so a++ = 6

  Was this answer useful?  Yes

Asarudeen

  • Feb 9th, 2012
 

C = a++ + ++a + ++a

Compiler starts from leftmost operand

Code
  1. 1+5 = 6

  2.  

  3. a = 6

  4.  

  5. 1+6 =7

  6.  

  7. a = 6+7

  8.  

  9. a =13

  10.  

  11. 1+13 =14

  12.  

  13. Now

  14.  

  15. c = 6+ ++7 ++13

  16.  

  17. c = 28

  Was this answer useful?  Yes

peter

  • Mar 25th, 2012
 

Which operation in the following expression will be performed first?

c = a++ / b + 5;

  Was this answer useful?  Yes

Arvind singh

  • Aug 17th, 2013
 

Code
  1. 18

  Was this answer useful?  Yes

phani

  • Jan 3rd, 2014
 

c=19 and a=8

  Was this answer useful?  Yes

anudeep jaini

  • Feb 18th, 2014
 

19

  Was this answer useful?  Yes

piyush

  • Feb 3rd, 2015
 

int a=5,c;
c = ++a + ++a + a++;
then c=?

  Was this answer useful?  Yes

pushpendra

  • Feb 12th, 2015
 

18, because in modern compiler . first we did pre increment so a = 6 , add all c = 18, after executing this instruction a = 8

  Was this answer useful?  Yes

Niranjan

  • Feb 19th, 2015
 

20

  Was this answer useful?  Yes

chakreshprasad

  • Aug 17th, 2015
 

Hi,
What should be ans of the following:
int i = 5;
a = ++i + i++;
printf("%d", a);
?
Compiler says 13. But, how?

  Was this answer useful?  Yes

The answer can quite literally be *anything*; the behavior is undefined. With few exceptions, the order in which expressions are evaluated and in which side effects are applied is *unspecified*; you cannot assume that expressions are evaluated left to right, and you cannot assume that the side effects of the ++ and -- operators are applied immediately after evaluation. The C language standard explicitly calls expressions like this out as undefined behavior.

  Was this answer useful?  Yes

neha

  • Sep 2nd, 2015
 

It is 19
Evaluate like this:- 5++ + 7 + 7++
First is post increment so 5 is printed first then increment is Performed over a

Now a=6
on pre-increment it becomes 7
Again a post increment is done in which 7 is printed and a becomes 8 now

so, 5+7+7=19

  Was this answer useful?  Yes

Rahul Gupta

  • Sep 12th, 2015
 

There are 2 type of operator is involve in this expression firs is unary increment which rank is higher than second operator binary addition So, first increment is solved from right to left because is associativity is right to left.
Then after perform the addition and one thing more it is a single statement for computer so don't break it in multiple lines
int a=5,b;
b=a++ + ++a + a++;
solution is
a++ + ++a + 5 because of a++ is post fix so it participate in exp with its current value
a++ + 6 + 6 because ++a is prefix so it increase 1 value and second 6 is because used same variable
6+6+6
so the result will be 18.

  Was this answer useful?  Yes

Praveen

  • Sep 14th, 2015
 

The answer is 18, because first pre-increment happens then expression is evaluated and then post increment occurs. so first a=6 >> (6++)+6+(6++) = 18 and finally a=8

  Was this answer useful?  Yes

sandeep singh rawat

  • Oct 13th, 2015
 

The logic behind such problems is associativity and precedence and it is easy to learn also PUMA SRE BIT3 LOG2 TAC. I did not provide the solution to this problem because there are so may questions like this you can put and getting the solution will not teach you anything.

  Was this answer useful?  Yes

Ripon Hossain Jewel

  • Nov 24th, 2015
 

Code
  1.     #include<stdio.h>

  2.     int main()

  3.     {

  4.      int a=5,c;

  5.      c = a++ + ++a + a++;

  6.      printf("Value of c= %d",c);

  7.      return 0;

  8.     }

  Was this answer useful?  Yes

khasim

  • Nov 26th, 2015
 

18

  Was this answer useful?  Yes

vijayanandhini mano

  • Dec 12th, 2015
 

19

  Was this answer useful?  Yes

Alejandro Visiedo

  • Mar 5th, 2016
 

The output is not 28, its 19.

  Was this answer useful?  Yes

rakesh

  • Jun 22nd, 2016
 

{
i=5;
a=++i + i++; // 6 + 7
printf("%d",a); //13
}

  Was this answer useful?  Yes

priya

  • Jun 29th, 2016
 

19

  Was this answer useful?  Yes

Praveen S

  • Aug 7th, 2016
 

Answer is 22 not 28 .

Code
  1. + does addition for 2 at a time :

  2.  

  3. its (a++ + ++a) + a++;

  4. thus answer is (7 + 7) + 8 = 22 //Not 28 .

  Was this answer useful?  Yes

bhargav

  • Aug 30th, 2016
 

given int a=5;
if a=5 then
value of a value of a value of a value of a
initial after a++ after ++a after a++
a=5 a++=5 ++a=7 a++=7
a=6 now a=7 now a=8 now
then the sattement a++ + ++a +a++=5+7+7=19

  Was this answer useful?  Yes

Ashish

  • Mar 15th, 2017
 

a++ + ++a + a++
5+6 + 1+5 + 5+6
11 + 6 + 11
=28
It is my answer, correct me if wrong

  Was this answer useful?  Yes

The answer is that the behavior of "a++ + ++a + a++" is *undefined* - there is no right answer (or, more properly, any result is equally correct). Except in a few cases, C does not force left-to-right evaluation, nor does it require that the side effect of the ++ operator be applied immediately after evaluation. So if x is initialized to 5, then the expression can be evaluated as "5 + 6 + 5", or "5 + 6 + 6", or "6 + 7 + 5", or "5 + 7 + 7", or pretty much anything else.
From the C language standard (section 6.5, para 2): "If a side effect on a scalar object is unsequenced relative to either a different side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined."
None of the expressions a++, ++a, and a++ are sequenced with respect to each other. Each has a side effect on a. Per the clause above, the behavior is undefined. Youll get *a* result, it just may not be what you expect.

  Was this answer useful?  Yes

rajat

  • Sep 25th, 2017
 

According to precedence of ++ postfix and prefix operators postfix ++-- are executed prior to prefix ones

  Was this answer useful?  Yes

Pramodh Venkatraman

  • Dec 18th, 2017
 

++a gives the value of i as 6 and i++ gives the value of 7.
++5 + 6++ = 6+7=13.

  Was this answer useful?  Yes

Sharath

  • Sep 13th, 2019
 

int a=5,b;
b=a++ + ++a;
after this a=7,b=12
int a=5,b;
b=++a +a++;
after this a=7,b=13
Can you explain how come it is ?

  Was this answer useful?  Yes

susruth

  • Sep 25th, 2019
 

Code
  1. #include<stdio.h>

  2. int main()

  3. {

  4. int a=5;

  5. int k= --a + a--;

  6. printf("k= %d   a=%d

  7. ".k,a);

  8. return 0;

  9. }

  Was this answer useful?  Yes

vishal

  • Jun 2nd, 2022
 

19 is the correct answer
here is my perfect code try and find it all other answers is incorrect

Code
  1. #include <stdio.h>

  2.  

  3. int main()

  4. {

  5. int a=5;int c;

  6.  

  7. c = a++ + ++a + a++;

  8.  printf("%d",c);

  9. }

  10.  

  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