#include <stdio.h>
int main()
{
    int i=5;
    printf("%d %d %d %d %d", --i,i--,++i,--i,i);

    return 0;
}

Recommended Answers

All 7 Replies

#include <stdio.h>
int main()
{
int i=5;
printf("%d %d %d %d %d", --i,i--,++i,--i,i);

return 0;
}

Undefined Behaviour.

commented: Excellent link +11

it is easy
i is 5
then -- i which means (i) is 4
then i-- which will appear 4 then i will be 3 then
++i which means increase by one so (i) is 4 then
-- i becomes i is 3, then print last (i) which is 3

the result is 4 4 4 3 3
please reply if I am mistaken

> please reply if I am mistaken
You are mistaken.
Read the link Aia posted.

Trying to rationalise what you expect to happen, or merely posting what your current compiler gives you is no way to go.

Thank you I learnt somthing from you
but I was thinking the program will run as
this following:

#include<stdio.h>
int main()
{
int i=5;
printf("%d",--i);
printf("%d",i--);
printf("%d",++i);
printf("%d",--i);
printf("%d",i);

return 0;
}

That would be a completely different program, which is fine.

Use that FAQ to also look up "sequence points". You'll see that each side effect is separated by a sequence point, so there is no issue about the order of things and your program is well defined.

But to say it always has the same output as the one-liner in the original post would be wrong.

Did you run the program before asking?

Did you run the program before asking?

Running a given program is not a garantee that is correct, legal code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.