Showing 41–50 of 499 questions
Consider the following code:
#define MAX 100
#define MAX 200
What will be the result?
A
Compilation error: macro redefinition
B
MAX will have value 200, last definition wins
C
Compilation warning but MAX remains 100
D
Runtime error
Correct Answer:
A. Compilation error: macro redefinition
EXPLANATION
In C, redefining a macro without #undef first will cause a compilation error. Most compilers treat macro redefinition as an error.
What is the output?
#define STR(x) #x
printf("%s", STR(hello));
A
hello
B
"hello"
C
Error: undefined variable
D
STR(hello)
EXPLANATION
The # operator (stringification) converts the macro argument into a string literal. STR(hello) becomes "hello" as a string.
What does the ## operator do in a macro?
A
Concatenates tokens into a single token
B
Creates comments
C
Defines nested macros
D
Marks preprocessor directives
Correct Answer:
A. Concatenates tokens into a single token
EXPLANATION
The ## operator (token pasting) concatenates two tokens into a single token during macro expansion.
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);
A
5 10
B
10 5
C
Compilation error
D
Runtime error
EXPLANATION
The SWAP macro exchanges x and y values using a temporary variable. After the swap, x becomes 10 and y becomes 5.
What will happen if you define the same macro twice with different values?
A
Compilation error
B
First definition is ignored, second is used
C
Both definitions are active
D
Runtime error occurs
Correct Answer:
B. First definition is ignored, second is used
EXPLANATION
When a macro is redefined, the new definition replaces the old one. Some compilers may issue a warning about redefinition.
What is the difference between #include and #include "myheader.h"?
A
No difference, both are equivalent
B
First searches in standard paths, second in current directory
C
First is for functions, second is for variables
D
#include "" is deprecated
Correct Answer:
B. First searches in standard paths, second in current directory
EXPLANATION
Angle brackets search in standard system directories. Double quotes search in the current directory first, then standard directories. This is the standard convention.
Which of the following shows the correct order of preprocessor directives in a C program?
A
#include, #define, #ifndef, #endif
B
#define, #include, #ifdef, #endif
C
Any order is valid
D
#include must be first
Correct Answer:
C. Any order is valid
EXPLANATION
Preprocessor directives can appear in any order in the source code. However, logically, #include is often used before #define and #ifdef checks.
What is the purpose of the #ifdef directive?
A
To check if a macro is defined
B
To include a file conditionally
C
To define a new macro
D
To undefine a macro
Correct Answer:
A. To check if a macro is defined
EXPLANATION
#ifdef checks if a macro identifier is currently defined. Code following it is compiled only if the macro is defined.
What is the output of this code?
#define ADD(a,b) a+b
int result = ADD(5,3)*2;
EXPLANATION
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.
What is the correct way to store variable-length strings dynamically?
A
char str[MAX]; gets(str);
B
char *str = (char*)malloc(length+1); fgets(str, length+1, stdin);
C
char *str; scanf("%s", str);
D
char str[] = malloc(100);
Correct Answer:
B. char *str = (char*)malloc(length+1); fgets(str, length+1, stdin);
EXPLANATION
Proper way requires dynamic allocation with malloc, adding 1 for null terminator, and using fgets for safe input.