Which of the following is NOT a preprocessor directive?
Answer: D
#allocate is not a valid preprocessor directive. Valid directives include #include, #define, #pragma, #ifdef, #ifndef, #if, #else, #elif, #endif, #error, #undef.
Q.22Medium
Consider the following code:
#define MAX 100
#define MAX 200
What will be the result?
Answer: A
In C, redefining a macro without #undef first will cause a compilation error. Most compilers treat macro redefinition as an error.
Q.23Medium
What will be the output of the following code?
#define SQUARE(x) x*x
int result = SQUARE(2+3);
printf("%d", result);
Answer: B
SQUARE(2+3) expands to 2+3*2+3 = 2+6+3 = 11 due to operator precedence. Macros don't evaluate arguments; they substitute text directly. Safe macro should be ((x)*(x)).
Q.24Easy
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.25Medium
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.
Advertisement
Q.26Medium
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.27Easy
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.28Medium
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
Q.29Hard
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.30Medium
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.31Medium
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.32Hard
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.33Easy
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.34Hard
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.35Easy
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.36Hard
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.37Medium
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.38Easy
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.39Medium
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.40Easy
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.