Why does the following compile in C++?
int phew = 53;
++++++++++phew ;
The same code fails in C, why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That is because in
C++pre-increment operator returns anlvalueand it requires its operand to be anlvalue.++++++++++phew ;in interpreted as++(++(++(++(++phew))))However your code invokes
Undefined Behaviourbecause you are trying to modify the value ofphewmore than once between two sequence points.In
C, pre-increment operator returns anrvalueand requires its operand to be anlvalue. So your code doesn’t compile in C mode.