int main() {
int a = 10;
int b = a * a++;
printf("%i %i", a, b);
return 0;
}
Is the output of the above code undefined behavior?
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.
No in
the behavior is undefined, so the result can be anything – that’s not what “implementation dependent” means.
You might wonder why it’s UB here since
ais modified only once. The reason is there’s also a requirement in 5/4 paragraph of the Standard that the prior value shall be accessed only to determine the value to be stored.ashall only be read to determine the new value ofa, but hereais read twice – once to compute the first multiplier and once again to compute the result ofa++that has a side-effect of writing a new value intoa. So even thoughais modified once here it is undefined behavior.