closed by
2,258 views
1 votes
1 votes
closed with the note: Undefined behaviour: https://stackoverflow.com/questions/1270370/printfd-d-d-n-a-a-a-output
#include <stdio.h>

        void main()

        {

            int a = 3;

            int b = ++a + a++ + --a;

            printf("Value of b is %d", b);

        }
after compiling on gcc compiler it gives 11.will you explain how is it possible?
closed by

3 Answers

1 votes
1 votes
Answer is : 11 (this is not the compiler dependent)

Here, the evaluation of b is done from left to right.

b = ++a + a++ + --a;

b =( (a=a+1)+(a=a+1)+(a=a-1) );

       ((first increment then assignment)+(first assignment then increment)+(first decrement then assignment))

b = ((4)+(4)+(3));

b = 11;
0 votes
0 votes
these quetions are compiler dependent.
0 votes
0 votes
Yes the question is compiler dependant if you run this code in Dev the output would be 13

Related questions

0 votes
0 votes
0 answers
1
Lakshman Bhaiya asked Mar 1, 2018
591 views
#include<stdio.h int main() { int a = 15; printf("%d\n",++a); printf("%d\n",a++); printf("%d\n", a); printf("%d\n",a ); return 0; }16,16,16,1615,13,15,1516,15,14,1416,16,...
2 votes
2 votes
1 answer
2
Sandeep Suri asked Aug 1, 2017
3,987 views
#include <stdio.h>int main(){ int x = 3;if (x == 2);x = 0;if (x == 3) x++;else x += 2;printf("x = %d", x);return 0;}
0 votes
0 votes
0 answers
3
Aakash1125 asked Apr 23, 2017
903 views
#include <stdio.h>void main(){int a = -5;int k = (a++, ++a);printf("%d\n", k);}how to solve this int k = (a++, ++a); expression?
0 votes
0 votes
0 answers
4
Arun Rout asked May 30, 2019
284 views
output is a=34 and x=13 can anyone plzz explain#include <iostream>using namespace std;int main(){int x = 10, a;a = x++ + ++x + x++;cout << "Post Increment Operation";cout...