PDA

View Full Version : How the controls for the post increment



navaneethan
July 20th, 2010, 06:09 AM
#include<stdio.h>
int main()
{
int a=5;
a=a++ * a++;
printf("%d",a);
return 0;
}


#include<stdio.h>
int main()
{
int a=5;
printf("%d",a=(a++ * a++));
return 0;
}


Friends,
here i get the output 27 and 30 respectively Please may i know how the control goes I can able to understand the first one but How the 2nd one? what is the difference between both of them?

dwhitney67
July 20th, 2010, 09:53 AM
#include <stdio.h>

int main(void)
{
int a = 5;
int y = a++ * a++;

printf("a = %d, y = %d\n", a, y);
return 0;
}




gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:6: warning: operation on ‘a’ may be undefined


Thus the final answer: operation on ‘a’ may be undefined

And because of such, you should not develop code like this.

MadCow108
July 20th, 2010, 10:08 AM
heres a little explanation why its undefined:
http://en.wikipedia.org/wiki/Sequence_point