Why is X % 0 an invalid expression?
I always thought X % 0 should equal X. Since you can’t divide by zero, shouldn’t the answer naturally be the remainder, X (everything left over)?
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.
The C++ Standard(2003) says in §5.6/4,
That is, following expressions invoke undefined-behavior(UB):
Note also that
-5 % 2is NOT equal to-(5 % 2)(as Petar seems to suggest in his comment to his answer). It’s implementation-defined. The spec says (§5.6/4),