Exercise: C Preprocessor

Questions for: C Preprocessor

Preprocessor directive #undef can be used only on a macro that has been #define earlier
A:
True
B:
False
C:
D:
Answer: A

True, #undef can be used only on a macro that has been #define earlier

Example: #define PI 3.14

We can undefine PI macro by #undef PI

If the file to be included doesn't exist, the preprocessor flashes an error message.
A:
True
B:
False
C:
D:
Answer: A

True, the included file does not exist it will generate the error.

Which of the following are correctly formed #define statements in C?
A:
#define CUBE (X) (X*X*X);
B:
#define CUBE(x) (X*X*X)
C:
#define CUBE(X)(X*X*X)
D:
#define CUBE(X) {X*X*X}
Answer: C
No answer description is available. Let's discuss.
Which of the following are correct preprocessor directives in C?
1: #ifdef
2: #if
3: #elif
4: #undef
A:
1, 2
B:
4
C:
1, 2, 4
D:
1, 2, 3, 4
Answer: D

The macros #ifdef #if #elif are called conditional macros.

The macro #undef undefine the previosly declared macro symbol.

Hence all the given statements are macro preprocessor directives.

Point out the error in the program
#include<stdio.h>

int main()
{
    int i;
    #if A
        printf("Enter any number:");
        scanf("%d", &i);
    #elif B
        printf("The number is odd");
    return 0;
}
A:
Error: unexpected end of file because there is no matching #endif
B:
The number is odd
C:
Garbage values
D:
None of above
Answer: A

The conditional macro #if must have an #endif. In this program there is no #endif statement written.

Ad Slot (Above Pagination)
Quiz