• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

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
0
tpat
Asked:
tpat
  • 4
1 Solution
 
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
 
Infinity08Commented:
>> Is  the following also compiler dependent.

Yes, because you're again modifying the same value (a) more than once between sequence points.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now