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

C Preprocessor Questions and Answers

91.

Would the following typedef work?
typedef #include l;

Answer: B

Correct answer: No

typedef is part of the C language and #include is a preprocessor directive; the two belong to different phases and cannot be combined in one construct. The preprocessor would try to include a file named by whatever followed, and the compiler would meet nonsense whatever happened. So it does not work.

Please log in or register to report this question.

92.

What will be the output of the program?

#include <stdio.h>
#define MIN(x, y) (x<y) ? x : y

int main()
{
    int x=3, y=4, z;
    z = MIN(x, y);
    if(z > 0)
        printf("%d\n", z);
    return 0;
}

 

Answer: A

Correct answer: 3

MIN(x, y) expands to (x<y) ? x : y, so with \(x = 3\) and \(y = 4\) the condition holds and z gets 3. Since 3 is greater than 0, the if succeeds and the value is printed. (The macro has no outer parentheses, so it would misbehave inside a larger expression - but the assignment here is safe.)

Please log in or register to report this question.

93.

Output of following C program?

#include <stdio.h>
#define max abc
#define abc 100
 
int main()
{
    printf("maximum is %d", max);
    return 0;
}

 

Answer: A

Correct answer: maximum is 100

Two rules combine. First, a macro is rescanned after substitution, so max expands to abc and abc then expands to 100. Second, macro replacement applies to tokens, not to text inside a string literal - so the max inside "maximum is %d" is untouched. The output is maximum is 100.

Please log in or register to report this question.

94.

The EOF character can be included in a file as part of its data

Answer: B

Correct answer: FALSE

EOF is not a character at all. It is a macro in <stdio.h> expanding to a negative integer, usually -1, which getchar and its relatives return to signal that no more input is available. Since it is outside the range of an unsigned char, it cannot be one of the bytes stored in a file. (This is why getchar returns int rather than char.)

Please log in or register to report this question.

95.

What will be the output of the program?

#include <stdio.h>
#define PRINT(int) printf("int=%d, ", int);

int main()
{
    int x=2, y=3, z=4;   
    PRINT(x);
    PRINT(y);
    PRINT(z);
    return 0;
}

 

Answer: A

Correct answer: int=2, int=3, int=4

A macro parameter may be named int - the preprocessor has no reserved words, and within the macro body the name simply stands for whatever was passed. Each call substitutes its own argument, so the three calls print 2, 3 and 4. The int= inside the string literal is untouched, since macro replacement never reaches inside a string.

Please log in or register to report this question.

96.

Will it result in to an error if a header file is included twice?

Answer: C

Correct answer: It is compiler dependent.

It depends on what the header contains and how strictly the compiler treats a repeat. A header with an include guard is harmless however often it is named, and repeating a macro definition or a function prototype identically is allowed. But a header that defines a type or a variable will, on a second inclusion, produce a duplicate definition - and older compilers reject some repeats that newer ones tolerate. Guarding the header removes the question entirely.

Please log in or register to report this question.

97.

Macros with arguments are not allowed.

Answer: B

Correct answer: FALSE

Function-like macros with arguments are perfectly ordinary: #define MAX(a,b) ((a) > (b) ? (a) : (b)). They are only textual substitution, which is why the parameters need parenthesising, but they are certainly allowed.

Please log in or register to report this question.

98.

A macro should always be accommodated in a single line.

Answer: A

Correct answer: FALSE

A macro definition ends at the end of the line, but the line can be continued with a backslash, so a macro may span as many lines as you like. Multi-line macros are common, usually wrapped in do { … } while(0) so that they behave as a single statement.

Please log in or register to report this question.

99.

What will be the output of the program?

#include <stdio.h>
#define SQR(x)(x*x)

int main()
{
    int a, b=3;
    a = SQR(b+2);
    printf("%d\n", a);
    return 0;
}

 

Answer: B

Correct answer: 11

The macro body parenthesises the whole expression but not the parameter, so SQR(b+2) becomes (b+2*b+2):

\[ 3 + 6 + 2 = 11 \]

Only ((x)*(x)) would give the intended \(5^2 = 25\).

Please log in or register to report this question.

Answer: B

Correct answer: To include the following code only if the named macro is not already defined

#ifndef tests whether a macro is undefined and, if so, compiles the block up to the matching #endif. Its commonest use is the include guard, which stops a header being processed twice in the same translation unit.

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