int main(void)
{
int i;
scanf( "%d", &i );
i = i++ * i++ ;
printf( "%d", i );
getchar();
getchar();
return 0;
}
Why does this program print 25
instead of 27
if I input 5
?
int main(void)
{
int i;
scanf( "%d", &i );
i = i++ * i++ ;
printf( "%d", i );
getchar();
getchar();
return 0;
}
Why does this program print 25
instead of 27
if I input 5
?
i = i++ * i++ ;
is undefined behaviour and so can do whatever it pleases, such as return 25, return 27, return 30 (which probably makes more sense than 27), format your hard disk, or even laugh derisively at you.
:-)
The C standard (both C99 and C11, and possibly earlier though I haven't checked) has things known as sequence points, and you are not permitted to change the same variable twice without an intervening sequence point (of which the multiplication symbol *
was not one).
You can see what are considered sequence points in Appendix C of both those iterations of the standard.