ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
#define SIGN(x) ((x) > 0 ? 1 : (x) < 0 ? -1 : 0)
int main(void) {
    int values[3] = {-5, 0, 7};
    for (int i = 0; i < 3; i++) printf("%d ", SIGN(values[i]));
}
  1. A. -1 0 1
  2. B. 1 0 -1
  3. C. 0 0 0
  4. D. -5 0 7

Correct answer: A. -1 0 1

SIGN(x) fully parenthesizes its argument everywhere it's used, avoiding precedence traps, so each use is safe even with a complex expression like values[i]. For -5: negative, gives -1. For 0: neither positive nor negative, gives 0. For 7: positive, gives 1.

More C Programming questions

Exams that ask this topic