ProgrammingHardWipro · Cognizant pattern
What is printed?
What is printed?
#define IS_EVEN(n) ((n) % 2 == 0)
int main() {
int x = 7;
if (!IS_EVEN(x))
printf("odd");
else
printf("even");
}- A. Compilation error: macro cannot be negated with !
- B. oddeven
- C. odd
- D. even
Correct answer: C. odd
IS_EVEN(x) expands to ((x) % 2 == 0), fully parenthesized so it's safe to negate: !((7) % 2 == 0) = !(1 == 0) = !0 = 1 (true), so 'odd' prints.
More C Programming questions
- Assume 32-bit int. What is printed? c int flags = 0; flags |= (1 << 2); flags |= (1 << 4); flags &= ~(1 << 2); printf("%d", flags);
- What is printed? c #if defined(DEBUG) && !defined(RELEASE) #define LOG(x) printf("[debug] %sn", x) #else #define LOG(x) #endif int main()…
- What is printed? c int a = 10; int b = (a > 5) ? (a < 20 ? 1 : 2) : 3; printf("%d", b);
- What is printed? c #define N 5 #undef N #define N 10 int main() { printf("%d", N); }
- Assume a 64-bit LP64 ABI. What is printed? c int a = 10; int p = &a; int pp = &p; pp = 20; printf("%d %d %d", a, p, pp);
- Assume a 64-bit LP64 ABI. What is printed? c char names[] = {"Alice", "Bob", "Carol"}; char p = names; p++; printf("%s %s", p, (names + 2));