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));
}- A. 1, correctly testing whether 3 is odd
- B. 0, incorrectly reporting 3 as not odd because of missing parentheses around x
- C. 3
- 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
- What is printed? c #define STR(x) #x #define XSTR(x) STR(x) #define VERSION 7 int main(void) { printf("%s %s", STR(VERSION)…
- What is printed? c #define SWAP(a, b) do { int t = a; a = b; b = t; } while (0) int main(void) { int x = 1, y = 2; if (x < y) SWAP(x, y)…
- What is printed? c #define A 1 #define B (A + 1) #undef A #define A 10 int main(void) { printf("%d", B); }
- What is printed? c #define FOO 10 #define BAR FOO + 5 int main(void) { int x = BAR 2; printf("%d", x); }
- What is printed? c #define LOG(msg) printf("[LOG] %s line %dn", msg, LINE) int main(void) { LOG("start"); LOG("end"); }
- What is printed? c #define TRUE 1 #define FALSE 0 #define BOOL int BOOL flag = TRUE; int main(void) { if (flag == TRUE) printf("yes")…