ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
#define IS_EVEN(n) (((n) % 2) == 0)
int main(void) {
    int x = 7;
    if (IS_EVEN(x))
        printf("even");
    else
        printf("odd");
}
  1. A. even
  2. B. odd
  3. C. Compile-time error: IS_EVEN is not a valid condition
  4. D. Undefined behaviour

Correct answer: B. odd

IS_EVEN(x) textually expands to (((x) % 2) == 0), which for x=7 is (7 % 2) == 0, i.e. 1 == 0, which is false, so the else branch runs, printing odd.

More C Programming questions

Exams that ask this topic