tags:

views:

96

answers:

2
#include<stdio.h>
#include<conio.h>
main()
{
 int i=5;
printf("%d",--i - ++i);//give output=0
printf("%d",++i + ++i);//give output=14
printf("%d",i++ + ++i);// give output=12
printf("%d",--i + ++i);// give output=10
printf("%d",i+++++i); // error :  invalid lvalue in increment /* in this case tell me how    parsing of expression i+++++i takes place by compiler */

getch();
}

in line 6,7,8,9 of code output are written which i get after execution please exlpain the whole process how the things going on here? also give answer for line : 10.

+9  A: 
printf("%d",--i - ++i);//give output=0
printf("%d",++i + ++i);//give output=14
printf("%d",i++ + ++i);// give output=12
printf("%d",--i + ++i);// give output=10

All the above expressions invoke Undefined Behaviour because the variable i is being modified more than once between two sequence points.

printf("%d",i+++++i); 

According to Maximal Munch rule i+++++i is parsed as i++ ++ +i. i++ returns an rvalue and next ++ cannot be applied to the result because ++ requires its operand to be an lvalue.

Prasoon Saurav
Do you mean, it's situation- and compiler-dependent what will happen?
Delan Azabani
@Delan : Undefined behaviour means *anything* can happen. `;)`
Prasoon Saurav
Great idea for a random number generator ;P
Delan Azabani
@Delan : No way, avoid writing such code.
Prasoon Saurav
@Delan: Anything can happen, including *not* being a RNG :).
KennyTM
+5  A: 

i+++++i is tokenized as i ++ ++ + i. The expression i++ returns an rvalue, which cannot be incremented by the 2nd ++, thus the error.

Also, the first 4 statements result in undefined behavior, mainly because the standard doesn't enforce which ++i or --i will be executed first.

KennyTM