Machine-translated by Google. Questions are authored in English.
Top

C Preprocessor Questions and Answers

71.

Every C program will contain at least one preprocessor directive.

Answer: B

Correct answer: False

Nothing compels a program to use a directive. A program that neither includes a header nor defines a macro - int main(void) { return 0; } - is perfectly valid C. In practice almost every program includes something, but that is habit rather than a rule.

Please log in or register to report this question.

72.

Will the program compile successfully?

#include <stdio.h>
#define X (4+Y)
#define Y (X+3)

int main()
{
    printf("%d\n", 4*X+2);
    return 0;
}

 

Answer: B

Correct answer: No

The two definitions refer to each other, and the preprocessor refuses to expand a macro recursively inside its own expansion. So the expansion of X stops with a bare Y (or the other way about) left in the text, and the compiler then meets an undeclared identifier. Mutually recursive macros are always a mistake.

Please log in or register to report this question.

73.

 

The following program won’t compile because there’re space between macro name and open parenthesis.

#include "stdio.h"
 
#define MYINC   (  a  )  (  ( a )  +  1 )
 
int main()
{
 
 printf("GeeksQuiz!");
 
 return 0;
}

 

Answer: B

Correct answer: FALSE

The space does change the meaning: with it, MYINC is an object-like macro whose body is the text ( a ) ( ( a ) + 1 ), rather than a function-like macro taking a parameter. But the program never uses MYINC, so nothing is expanded and it compiles and runs perfectly well. The statement is therefore false.

Please log in or register to report this question.

Answer: C

Correct answer: It causes the preprocessor to report a fatal error

#error message makes the preprocessor stop with a diagnostic. It is used to fail loudly when a configuration makes no sense - for example #if !defined(__STDC__) followed by #error This code requires a standard C compiler - rather than letting the build produce something broken.

Please log in or register to report this question.

75.

Identify the stringizing operator.

Answer: C

Correct answer: #

A single # before a macro parameter is the stringizing operator: it turns the argument’s text into a string literal, so #define str(x) #x makes str(hello) become "hello". The double ## is a different operator - it pastes two tokens into one.

Please log in or register to report this question.

76.

In a program the statement: #include "filename" is replaced by the contents of the file "filename"?

Answer: A

Correct answer: Before compilation

#include is a preprocessor directive: the named file’s contents are pasted in before compilation, producing a single translation unit. Nothing about it happens at run time, and the quoted form differs from the angle-bracket form only in where the file is searched for first.

Please log in or register to report this question.

77.

Identify the trigraph sequence for \.

Answer: A

Correct answer: ??/

The trigraph for the backslash is ??/. Trigraphs were introduced so that C could be written on terminals whose character sets lacked some of the symbols the language needs; the compiler replaces each three-character sequence in the very first phase of translation, before anything else.

Please log in or register to report this question.

78.

Which of the following are correct preprocessor directives in C?

1. #ifdef

2. #if

3. #elif

4. #undef

Answer: D

Correct answer: 1, 2, 3, 4

All four are real directives: #ifdef tests whether a macro is defined, #if evaluates a constant expression, #elif is the “else if” of the conditional group, and #undef removes a definition.

Please log in or register to report this question.

79.

What will be the output of the following C code if the value of ‘p’ is 10 and that of ‘q’ is 15?

#include <stdio.h>
int main()
{
    int p,q;
    printf("Enter two numbers\n");
    scanf("%d",&p);
    scanf("%d",&q);
    #if(4<2>-1)
    printf("%d",q);
    #else
    printf("bye");
    #endif
}

 

Answer: B

Correct answer: 15

The condition 4<2>-1 looks like a chained comparison but is not one. Relational operators are left-associative, so it groups as (4<2) > -1:

\[ (4 < 2) = 0, \quad 0 > -1 \text{ is true} \]

So the #if branch survives and the program prints q, which is 15. Note the values are read at run time but the branch is chosen at preprocessing time, so the input never affects which printf is compiled.

Please log in or register to report this question.

80.

The preprocessor directive which is used to remove the definition of an identifier which was previously defined with #define?

Answer: B

Correct answer: #undef

#undef NAME removes an existing macro definition. After it, the identifier is once again an ordinary token - and, usefully, #ifdef NAME becomes false. It is also the way to redefine a macro safely, since redefining with a different body and no #undef in between is not strictly conforming.

Please log in or register to report this question.

Loading…
Tags: C Preprocessor Questions and Answers || C Preprocessor MCQ Questions and Answers || C Preprocessor GK Questions and Answers || C Preprocessor GK MCQ Questions || C Preprocessor Multiple Choice Questions and Answers || C Preprocessor GK || GK on C Preprocessor || C Programming Questions and Answers || C Programming MCQ Questions and Answers || C Programming GK Questions and Answers || GK on C Programming