• C

PRE-INCREMENT and POST-INCREMENT...

Can anybody explain the output of this one:

#include<stdio.h>

int main(){
    int i=5,j;
    j= ++i + ++i + ++i;
    printf("%d %d",i,j);
    return 0;
}

I knew all these, but haven't touched C for a while, help would be appreciated.

Thanks
tpatAsked:
Who is Participating?
 
Infinity08Commented:
>> Is  the following also compiler dependent.

Yes, because you're again modifying the same value (a) more than once between sequence points.
0
 
Infinity08Commented:
>> Can anybody explain the output of this one:

Not according to the C standard, no. It is not valid to modify the same value more than once between sequence points.

You might be able to explain it, if you know the internal workings of your compiler. But that would be pretty useless, because a different compiler (or a different version of the same compiler) might very well give a different output.
0
 
Infinity08Commented:
Or to say it in a different way : this code's behavior is undefined, so the output will depend on the specific implementation (ie. the internal workings of the compiler).
0
 
Infinity08Commented:
And some further reading material if you're interested : http://c-faq.com/expr/seqpoints.html
0
 
tpatAuthor Commented:
That is what I observed ... different compilers give different results.
Is  the following also compiler dependent.

#include<stdio.h>

main(){
int a = 10;
int b = 5;

int z = b + ++a + ++a;
printf("Value of z is %d\n",z);

}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.