LearnFrenzy
Sign in Take a free test

Operators and Expressions · Predict the Output · Question 10

Unary minus with % and / on negative numbers Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

What is the output of the following C program?

main.c
#include <stdio.h>

int main(void) {
    int a = -7;
    printf("%d %d %d\n", -a % 3, a / -2, -(a % 4));
    return 0;
}
  • A 1 3 3 Correct
  • B -1 3 3
  • C 1 -3 3
  • D 1 3 -3
Correct Answer: Option A

Unary minus binds tighter than %: (-a) % 3 = 7 % 3 = 1. −7 / −2 = 3.5 → 3 (toward zero). −7 % 4 = −3, negated: 3.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links