0

The following C code executes correctly but not as expected. Post increment operator here in z=z++ is creating confusion here. I may not be able to figure out silly mistake/concept, Can I have a brief explanation or some helpful link please.

 #include<stdio.h>
    int main()
    {
        int x=5,y=6,z=7;
        if(x-y)

            z=z++;
            z=--z;

        printf("%d",z);
    }
0

1 Answer 1

0

You are not allowed to do z=z++; because between 2 sequence points you are not allowed to assign a variable 2 times.

This one is a full expression in which you assign z 2 times. So it can be interpreted ambigously and the result of the C abstract machine is undefined behavior.

The same for z=--z.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, i got your point. You also mean that the answer will depend on compiler.
Just one doubt, if I have to pick one value from 6, 7 and 8. Which one will be more general and expected answer?
@skyconfusion This is undefined behavior. You can't rely on any value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.