C Programming
C language from basics to advanced placement prep
499 Questions 10 Topics Take Test
Advertisement
Showing 41–50 of 499 questions
Q.41 Medium Preprocessor
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.

Take Test
Q.42 Medium Preprocessor
What is the output?
#define STR(x) #x
printf("%s", STR(hello));
A hello
B "hello"
C Error: undefined variable
D STR(hello)
Correct Answer:  A. hello
EXPLANATION

The # operator (stringification) converts the macro argument into a string literal. STR(hello) becomes "hello" as a string.

Take Test
Q.43 Medium Preprocessor
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.

Take Test
Q.44 Medium Preprocessor
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
Correct Answer:  B. 10 5
EXPLANATION

The SWAP macro exchanges x and y values using a temporary variable. After the swap, x becomes 10 and y becomes 5.

Take Test
Q.45 Medium Preprocessor
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.

Take Test
Q.46 Medium Preprocessor
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.

Take Test
Q.47 Medium Preprocessor
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.

Take Test
Q.48 Medium Preprocessor
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.

Take Test
Q.49 Medium Preprocessor
What is the output of this code?
#define ADD(a,b) a+b
int result = ADD(5,3)*2;
A 16
B 11
C Error
D 8
Correct Answer:  A. 16
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.

Take Test
Q.50 Medium Dynamic Memory
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.

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