C Programming
C language from basics to advanced placement prep
499 Questions 10 Topics Take Test
Advertisement
Showing 21–30 of 499 questions
Q.21 Medium Preprocessor
What is the purpose of #pragma pack() directive?
A To define a macro
B To control structure member alignment
C To include header files
D To create string macros
Correct Answer:  B. To control structure member alignment
EXPLANATION

#pragma pack() controls how compiler aligns structure members in memory. #pragma pack(1) minimizes padding, while #pragma pack() without arguments resets to default.

Take Test
Q.22 Medium Preprocessor
What is the issue with this macro: #define MAX(a,b) a>b?a:b
int x = MAX(2, 3); int y = MAX(++i, ++j);
A Syntax error
B Arguments are evaluated multiple times
C No issue
D Type mismatch
Correct Answer:  B. Arguments are evaluated multiple times
EXPLANATION

In MAX(++i, ++j), both i and j are incremented twice due to double evaluation. Parentheses help but don't solve side effects completely.

Take Test
Q.23 Medium Preprocessor
What is the output of:
#define STR(x) #x
int main() { printf("%s", STR(Hello)); return 0; }
A Hello
B "Hello"
C x
D Compilation error
Correct Answer:  A. Hello
EXPLANATION

The # operator converts the macro argument into a string literal. STR(Hello) becomes "Hello", and printf outputs Hello without quotes.

Take Test
Q.24 Medium Preprocessor
Which of the following correctly defines a macro with multiple statements?
A #define PRINT(x) printf("%d", x); printf("\n");
B #define PRINT(x) { printf("%d", x); printf("\n"); }
C #define PRINT(x) do { printf("%d", x); printf("\n"); } while(0)
D Both B and C
Correct Answer:  C. #define PRINT(x) do { printf("%d", x); printf("\n"); } while(0)
EXPLANATION

Option C using do-while(0) is the safest approach to avoid issues with semicolons in if-else statements. Option B can cause problems with control flow statements.

Take Test
Q.25 Medium Preprocessor
What is the output of:
#define SQUARE(x) x*x
int main() { int a = SQUARE(2+3); printf("%d", a); return 0; }
A 25
B 11
C 15
D Compilation error
Correct Answer:  B. 11
EXPLANATION

SQUARE(2+3) expands to 2+3*2+3 = 2+6+3 = 11 due to operator precedence. Proper macro should use parentheses: #define SQUARE(x) ((x)*(x))

Take Test
Advertisement
Q.26 Medium Preprocessor
What is the purpose of predefined macros like __LINE__ and __FILE__?
A To store user-defined variables
B To provide compile-time information about the source code location
C To create inline functions
D To define character arrays
Correct Answer:  B. To provide compile-time information about the source code location
EXPLANATION

__LINE__ expands to the current line number and __FILE__ expands to the current filename. These are predefined macros that provide compile-time information useful for debugging and error reporting.

Take Test
Q.27 Medium Preprocessor
What will happen when this code is compiled?
#define SIZE 10
int arr[SIZE];
#undef SIZE
int arr2[SIZE];
A Compilation error - SIZE is undefined
B No error - both arrays created with size 10
C Warning only - arr2 gets default size
D Runtime error
Correct Answer:  A. Compilation error - SIZE is undefined
EXPLANATION

After #undef SIZE, the macro SIZE is no longer defined. Attempting to use SIZE in int arr2[SIZE] will cause a compilation error because SIZE is not recognized by the compiler.

Take Test
Q.28 Medium Preprocessor
What will be the result of this code?
#define MAX(a,b) (a>b?a:b)
int x = MAX(MAX(2,5), MAX(3,4));
A 5
B 4
C 3
D 2
Correct Answer:  A. 5
EXPLANATION

Inner MAX calls: MAX(2,5)=5 and MAX(3,4)=4. Outer MAX call: MAX(5,4)=5. The macro correctly handles nested calls due to proper parenthesization.

Take Test
Q.29 Medium Preprocessor
Which of the following macro definitions will correctly compute the absolute value?
#define ABS(x) ((x)
A Correct - uses ternary operator with proper precedence
B Incorrect - missing parentheses around x
C Incorrect - division cannot be used
D Correct but inefficient
Correct Answer:  A. Correct - uses ternary operator with proper precedence
EXPLANATION

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.

Take Test
Q.30 Medium Preprocessor
What is the token pasting operator (##) used for in C preprocessor?
A Creates comments
B Concatenates two tokens into a single token
C Performs mathematical addition
D Includes header files
Correct Answer:  B. Concatenates two tokens into a single token
EXPLANATION

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.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips