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

    return 0;
}

Edited by Reverend Jim: Fixed formatting

5
Contributors
7
Replies
8
Views
9 Years
Discussion Span
Last Post by Aia
0

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

0

> 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.

0

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;
}

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.

0

Did you run the program before asking?

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.