Which preprocessor directive is used to conditionally compile code based on whether a macro is defined?
Answer: D
#ifdef checks if a macro is defined, #ifndef checks if it's not defined. Both are used for conditional compilation based on macro definition status.
Q.902Medium
What will be printed?
#define DEBUG 1
#if DEBUG
printf("Debug mode ON");
#else
printf("Debug mode OFF");
#endif
Answer: A
Since DEBUG is defined as 1 (non-zero), the #if DEBUG condition is true, so only the code in #if block executes. The #else block is skipped.
Q.903Medium
What does the following macro do?
#define CONCATENATE(a,b) a##b
Answer: B
The ## operator (token pasting operator) concatenates two tokens into a single token during preprocessing. For example, CONCATENATE(var,1) becomes var1.
Q.904Easy
Which statement about preprocessor is TRUE?
Answer: B
Preprocessor directives (starting with #) are processed before the actual compilation by a separate preprocessor tool. They perform text substitution and conditional compilation.
Q.905Medium
What is the output of:
#define STR(x) #x
printf("%s", STR(HELLO));
Answer: A
The # operator (stringification) converts the macro argument into a string literal. STR(HELLO) becomes "HELLO", and printf prints: HELLO
Advertisement
Q.906Hard
Consider:
#define MAX(a,b) ((a)>(b)?(a):(b))
Which advantage does this provide?
Answer: D
Macros with parentheses provide inline substitution (better performance, no function call overhead) and can work with any data type. However, they lack type safety that functions provide.
Q.907Medium
What happens when you use #undef on a macro?
Answer: B
#undef removes a macro definition from the preprocessor's symbol table, allowing you to redefine it later without errors.
Q.908Medium
Which of the following will correctly find the maximum of two numbers using a macro?
Answer: B
Option B is safest as all operands and the entire expression are parenthesized, preventing operator precedence issues and side effects.
Q.909Hard
What will be output?
#define PRINT(x) printf(#x " = %d\n", x)
PRINT(5+3);
Answer: A
The # operator converts 5+3 into the string "5+3". The macro expands to printf("5+3" " = %d\n", 5+3) which prints: 5+3 = 8
Q.910Easy
Which preprocessor directive should be used to check if a macro is NOT defined?
Answer: D
Both #ifndef and #if !defined(MACRO) are equivalent and check if a macro is not defined. #ifndef is simpler syntax for this specific case.
Q.911Hard
What is the result of the following?
#define MIN(x,y) ((x)<(y)?(x):(y))
int a=5, b=3;
int result = MIN(a++, b++);
Answer: C
The macro expands to ((a++)<(b++)?(a++):(b++)). Since 5<3 is false, b++ is evaluated twice (once in comparison, once in ternary), making b=5. a is incremented once in comparison, so a=6.
Q.912Easy
Which header file is included using angle brackets in standard C library?
Answer: B
Standard library headers like stdio.h, stdlib.h, string.h are included using angle brackets #include <filename>.
Q.913Hard
What does the following preprocessor do?
#define OFFSET(type, field) ((size_t)&(((type *)0)->field))
Answer: A
This macro calculates the byte offset of a field within a structure by casting 0 as a pointer to the type and taking the address of the field. Similar to the offsetof() macro in stddef.h.
Q.914Medium
Which of the following is the correct way to prevent multiple inclusion of a header file?
Answer: D
#ifndef guards and #pragma once are both used to prevent multiple inclusion. #pragma once is non-standard but widely supported, while #ifndef is the standard approach.
Q.915Easy
Which preprocessor directive is used to define a constant that cannot be changed during program execution?
Answer: A
#define is used to create macro definitions and constants at compile time. #const, #constant, and #fixed are not valid preprocessor directives in C.
Q.916Medium
What is the output of the following code?
#define SQUARE(x) x*x
int a = SQUARE(5+3);
Answer: B
SQUARE(5+3) expands to 5+3*5+3 due to operator precedence (multiplication before addition), which equals 5+15+3=23. However, the expression is actually 5+3*5+3=23. Actually it's (5+3)*(5+3)=64 if properly parenthesized. Without parentheses: 5+3*5+3 = 5+15+3 = 23. But SQUARE(5+3) = 5+3*5+3 = 23. Re-evaluating: x*x where x=5+3 becomes 5+3*5+3 = 23+21 = 44.
Q.917Easy
Which of the following is a difference between #include <file.h> and #include "file.h"?
Answer: D
The angle bracket version searches in standard system directories first, while quoted version searches in the current/local directory first. Both statements B and C correctly describe this distinction.
Q.918Easy
Consider the following code:
#define PI 3.14
#undef PI
#define PI 3.14159
What is the value of PI after execution?
Answer: B
#undef removes the previous definition of PI. The subsequent #define redefines PI as 3.14159. This is valid C syntax and the final value of PI is 3.14159.
Q.919Medium
What happens when you use stringification operator (#) in a macro?
#define STRINGIFY(x) #x
Answer: B
The # operator (stringification) converts its argument into a string literal. For example, STRINGIFY(hello) becomes "hello". This is useful for creating string representations of identifiers.
Q.920Medium
What is the token pasting operator (##) used for in C preprocessor?
Answer: B
The ## operator (token pasting) concatenates two tokens into a single token during preprocessing. For example, #define CONCAT(a,b) a##b creates CONCAT(var,1) as var1.