ProgrammingVery hardProduct company OA

What is printed?

What is printed?
#define IS_ODD(x) (x % 2 == 1)
int main(void) {
    printf("%d", IS_ODD(4 - 1));
}
  1. A. 1, correctly testing whether 3 is odd
  2. B. 0, incorrectly reporting 3 as not odd because of missing parentheses around x
  3. C. 3
  4. D. Compilation error

Correct answer: B. 0, incorrectly reporting 3 as not odd because of missing parentheses around x

Without parentheses around the parameter, IS_ODD(4 - 1) expands to (4 - 1 % 2 == 1). Precedence from highest to lowest here is %, then -, then ==, so it groups as ((4 - (1 % 2)) == 1) = ((4 - 1) == 1) = (3 == 1) = 0. The macro silently fails to test whether 3 is odd and instead prints 0, purely because x was not wrapped in parentheses in the macro body.

More C Programming questions

Exams that ask this topic