C - What is the output: modulus operator?

Question

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;
}


Click to view the answer

-3
3
-3
3

Note

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.

Related Quiz