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.