C - What is the output: Division with Integers?

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

?6
?6
6
6

Note

With division, the result will always be negative if the operands have different signs.

Thus, the expression ?45 / 7 produces the same result as the expression 45 / ?7, which is ?6.

If the operands in a division are of the same sign, either positive or negative, the result is always positive.

Thus, 45 / 7 produces the same result as ?45 / ?7, which is 6.

Related Quiz