ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
#define LOG(msg) printf("[LOG] %s\n", msg)
#ifdef DEBUG
#define TRACE(msg) LOG(msg)
#else
#define TRACE(msg)
#endif
int main(void) {
TRACE("starting");
printf("done");
}- A. [LOG] starting\ndone
- B. done
- C. Compile-time error: DEBUG is not defined
- D. [LOG] starting\n
Correct answer: B. done
Since DEBUG is not defined anywhere in this file, the #else branch applies: TRACE(msg) expands to nothing at all. TRACE("starting"); therefore vanishes entirely (becoming just an empty statement), and only printf("done") actually executes, printing done.
More C Programming questions
- What is printed? c #define CONCAT(a, b) a##b int xy = 42; printf("%d", CONCAT(x, y));
- What is printed? c #if defined(STDC) && STDCVERSION >= 199901L int x = 10; #else int x = 20; #endif printf("%d", x);
- What is printed? c #define MIN(a, b) ((a) < (b) ? (a) : (b)) #define CLAMP(x, lo, hi) MIN(MAX(x, lo), hi) #define MAX(a, b) ((a) > (b) ?…
- What is printed? c #define INCR(x) (++x) int arr[3] = {10, 20, 30}; int i = 0; printf("%d", arr[INCR(i)]);
- What is printed? c #define ISEVEN(n) (((n) % 2) == 0) int main(void) { int x = 7; if (ISEVEN(x)) printf("even"); else printf("odd"); }
- What happens when this runs? c int x; printf("%d", x);