Programming logic puzzle

1,264.4K Views

What will be the output of the following program?


int main() {

union {

int i;

int j;

void f() {

printf("i=%d, j=%d\n", i, j);

}

};

i = 3;

j = i+++ ++i;

f();

return 0;

}

Share
Add Comment

  • 3 Answer(s)

    Error message….

    dyj Expert Answered on 5th August 2016.
    Add Comment

    ERROR
    i+++ at line 20 is not executable.
    pardon me if I am wrong.

    Shubham Patra Guru Answered on 15th August 2016.
    Add Comment

    i = 5, j = 8

    This statement,: j = i+++ ++, i is actually

    j = i++ + ++i;

    i = 3 at first, then starting from left we look at i++, so the expression takes the values as 3 because i++ is a post-increment and afterwards increments i to 4. Then it looks at the other part of the expression which is ++i so for this it does a pre-increment and i becomes 5 and then 3 + 5 is added together and assigned to j.

    radkan Scholar Answered on 29th March 2017.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.
  • More puzzles to try-