Examveda
Examveda

what will be the output when following code is executed?
void main()
{
   int a=10, b;
   b = a++ + ++a;
   printf("%d %d %d %d", b, a++, a, ++a);
}

A. 12 10 11 13

B. 22 12 12 13

C. 22 11 11 11

D. 22 14 12 13

E. 22 13 14 14

Answer: Option E

Solution(By Examveda Team)

Step 1:
Initialize a with the value 10.

Step 2:
Declare b without initialization (the initial value is undefined).

Step 3:
Evaluate a++ (post-increment):
  - The current value of a is 10.
  - After evaluating a++, a becomes 11.

Step 4:
Evaluate ++a (pre-increment):
  - ++a increments a to 12 and returns the updated value, which is 12.

Step 5:
Sum the values obtained in steps 3 and 4: 10 + 12 equals 22.

Step 6:
Assign the sum (22) to b.

Step 7:
The printf function follows the stack Last-In-First-Out (LIFO) concept, which means that the expressions are evaluated from right to left.

Step 8:
The first expression to be evaluated is ++a:
  - ++a increments a to 13 and returns the updated value, which is 13.

Step 9:
The next expression to be evaluated is a++:
  - a++ returns the current value of a (13) and then increments a to 14.
  - The returned value is 13.

Step 10:
Finally, the last expression to be evaluated is a, which has the current value of 14.

Therefore, the output of the program will be:
22 13 14 14

This output corresponds to the value of b, followed by the evaluated values of a++, a, and ++a, respectively, in the printf statement.

This Question Belongs to C Program >> Operators And Expressions

Join The Discussion

Comments ( 26 )

  1. Subhankar Lenka
    Subhankar Lenka :
    11 months ago

    b = 11+11=22
    12(new value of a)
    * printf exicution from right to left and printing from left to right
    so ++a=13
    a=13
    a++= 1st 13 printed then increment i.e. a=14
    so ans = 22 13 13 13

  2. Gunvatta
    Gunvatta :
    1 year ago

    b = 10++ =11
    ++11 =12(new value of a)
    i.e. b = 10+12 =22
    a++ = 12 (first use then incremented )
    a = 13 incremented.
    ++a = 14 (first incremented then used)

    Hence = 22 12 13 14

  3. Gunvatta
    Gunvatta :
    1 year ago

    The correct ans is 22 12 13 14 which is not in options

  4. CSE_58_RAJ KUMAR
    CSE_58_RAJ KUMAR :
    3 years ago

    a=10;
    b=(++a)+(++a)+(++a)-(--a);
    printf("%d",b);

  5. Narendra B
    Narendra B :
    3 years ago

    A) a=30 b=30

  6. Narendra B
    Narendra B :
    3 years ago

    A) a=30 b=30

  7. YOKESWARAN S
    YOKESWARAN S :
    3 years ago

    logic says 22,13,13,13
    b=10+12=22
    then printf ( b, a++, a, ++a); associativity left to right
    ++a =13 bcz it is preincrement
    then a is same as before value a= 13
    after that post increment a= 13 will be
    next we got above b=22
    therefore answer is 22 13 13 13

  8. Potta Yegnajayasimha
    Potta Yegnajayasimha :
    4 years ago

    a=5,b=a+=10 + ++ a + ++ a;
    What are the values of a,b?
    A)a=30,b=30
    B)a=17,b=21
    C)a=12,b=14

  9. Juniper
    Juniper :
    4 years ago

    The answer is option E.
    b will get evaluated as:-
    a++ (post increment), so its 10 (initially), then it becomes 11.
    11 is received by ++a, so it pre increments it and becomes 12.
    so b=10+12=22.
    Now, printf() follows the stack LIFO concept. So, the expression at the end is first evaluated, followed by the second last and so on. Here, The last expression given is ++a, This is pre increment. Which means, that the increment will be done first followed by the assignment. So, a=13. SO it gets incremented first, but not yet assigned. Then comes a. this also will be assigned later. Till now, a is 13, but not assigned yet. Thereafter comes a++. It is post increment so it assigns first, increments later. So, the assigned value is 13. So now, in place of a++, 13 will come. After assigning, it gets incremented to 14. b is already 22. So, now final assignment will begin. b will be assigned 22, a will be assigned 14, and ++a will have the value 14( as the pre increment was already done, it wont be done again)
    (NOTE: This process vary depending on compiler to compiler. This is strictly as per GCC)
    So, the answer will be 22 13 14 14 (OPTION E)

  10. Jitendra Sahoo
    Jitendra Sahoo :
    4 years ago

    Answer is 22 13 14 14

  11. Desh Deepak
    Desh Deepak :
    6 years ago

    Int a=14,b;
    b=(++a)+(a++)+(--a)
    Give the value of b,a

  12. Deepanshi Chauhan
    Deepanshi Chauhan :
    6 years ago

    logic says 22 13 13 13, but compiler says 22 13 14 14

  13. Rohan Shingan
    Rohan Shingan :
    6 years ago

    how??

  14. Ravindra Mulane
    Ravindra Mulane :
    6 years ago

    Ans: 22 12 14 14
    As precedence value of post increment is higher than pre increment.
    Hence, first a++ will be executed then ++a and then a.
    Try, Compile the program to find right answer.
    Please team exam veda update right option.

  15. MITTU SHARMA
    MITTU SHARMA :
    6 years ago

    ok its right 22 13 13 13
    thanks @Anmol Bhatia for explaination

  16. MITTU SHARMA
    MITTU SHARMA :
    6 years ago

    its correct answer is 22 12 13 14
    b = a++ + ++a; 10+12=22
    output:
    b =22
    a++ = 12 after assign its incremented with 1
    a = 13
    ++a = 14

  17. Shraddha Baviskar
    Shraddha Baviskar :
    6 years ago

    but in code bloks its output 22 13 14 14 why?

  18. Anmol Bhatia
    Anmol Bhatia :
    7 years ago

    b=a++ + ++a
    b=10+12=22
    a=12
    printf is first scanned from right to left
    ++a=13
    a=13
    a++=13
    now it will print
    b and then all the a values which in dis case are all 13
    so ans is
    22 13 13 13

  19. Saurabh Jare
    Saurabh Jare :
    7 years ago

    22 13 13 13

  20. Umang Mahant
    Umang Mahant :
    7 years ago

    thanks... Varsha Pandey

  21. Varsha Pandey
    Varsha Pandey :
    7 years ago

    22 13 13 13 is correct because in printf in execute from right to let so 1st ++a=13, a=13,a++=13,b=22 and one cycle of a++ is remaining so that will not be executed because there is no further a given.

  22. D&D
    D&D :
    7 years ago

    Answer would be 22 12 13 14

  23. KARAN PANDEY
    KARAN PANDEY :
    8 years ago

    22 13 14 14

  24. Shailendra Raghuvanshi
    Shailendra Raghuvanshi :
    8 years ago

    Ans is b

  25. Darshan Bodige
    Darshan Bodige :
    8 years ago

    Couldn't understand the soln plzz elaborate

  26. Arun Kumar
    Arun Kumar :
    8 years ago

    i cant understand clearly...
    please explain with each step...

Related Questions on Operators and Expressions