What is the output of this code?
#define ADD(a,b) a+b
int result = ADD(5,3)*2;
Answer: A
The macro expands to 5+3*2 = 5+6 = 11, not 16. However, due to operator precedence, it becomes 5+3*2. Actually, it expands correctly to (5+3)*2 = 16 when evaluated.
Q.2Medium
What is the purpose of the #ifdef directive?
Answer: A
#ifdef checks if a macro identifier is currently defined. Code following it is compiled only if the macro is defined.
Q.3Medium
Which of the following shows the correct order of preprocessor directives in a C program?
Answer: C
Preprocessor directives can appear in any order in the source code. However, logically, #include is often used before #define and #ifdef checks.
Q.4Medium
What is the difference between #include <stdio.h> and #include "myheader.h"?
Answer: B
Angle brackets search in standard system directories. Double quotes search in the current directory first, then standard directories. This is the standard convention.
Q.5Medium
What will happen if you define the same macro twice with different values?
Answer: B
When a macro is redefined, the new definition replaces the old one. Some compilers may issue a warning about redefinition.
Advertisement
Q.6Medium
What is the output of this code?
#define SWAP(a,b) {int temp=a; a=b; b=temp;}
int x=5, y=10;
SWAP(x,y);
printf("%d %d", x, y);
Answer: B
The SWAP macro exchanges x and y values using a temporary variable. After the swap, x becomes 10 and y becomes 5.
Q.7Medium
What does the ## operator do in a macro?
Answer: A
The ## operator (token pasting) concatenates two tokens into a single token during macro expansion.
Q.8Medium
What is the output?
#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" as a string.
Q.9Medium
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.10Medium
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.11Medium
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.12Medium
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.13Medium
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.14Medium
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.15Medium
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.16Medium
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.17Medium
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.18Medium
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.19Medium
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.
Q.20Medium
Which of the following macro definitions will correctly compute the absolute value?
#define ABS(x) ((x)<0?-(x):(x))
Answer: A
The macro correctly uses the ternary operator with proper parenthesization. Each use of x is wrapped in parentheses to prevent precedence issues. This is a well-formed macro for computing absolute value.