79088623

Date: 2024-10-15 06:17:48
Score: 3
Natty:
Report link

Can someone tell me why these two functions return different results?

Why returnN(text2, -2) returns 2 is that s.size() returns a value of type size_t, which is an unsigned integer type. So, When n is used in the modulus operation with s.size(), it promotes n to an unsigned integer type, causing a conversion from a negative number to a large positive number. That’s why get 2 as the result.

However, in returnN2, s.size() is explicitly assigned to the variable a, which is of type int. So, the % operation here performs signed integer modulus. That’s why get -2 as the result.

Reasons:
  • RegEx Blacklisted phrase (2.5): Can someone tell me
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): Can someone
  • Low reputation (0.5):
Posted by: Shelton Liu