What is the output of the following code?
#include <stdio.h> int main(void) { printf("%d \n",?45 % 7); printf("%d \n", 45 %-7); printf("%d \n",-45 %-7); printf("%d \n", 45 % 7); return 0; }
-3 3 -3 3
With the modulus operator, the sign of the result is the same as the sign of the left operand whether or not the operands have different signs.
Thus, 45 % ?7 results in the value 3, whereas ?45 % 7 results in the value ?3; the expression -45 % -7 also evaluates to -3.