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

C Preprocessor Questions and Answers

112.

Predict the output of following program?

#include <stdio.h>
#define MAX 1000
int main()
{
   int MAX = 100;
   printf("%d ", MAX);
   return 0;
}

 

Answer: C

Correct answer: Compiler Error

The macro replaces the identifier MAX everywhere it appears, including in the declaration. The compiler therefore sees

int 1000 = 100;

which is a syntax error. The preprocessor knows nothing about scope or declarations - it is a text substitution, and this is why macro names are conventionally written in capitals, so they are not accidentally reused as variable names.

Please log in or register to report this question.

113.

Which of the following are correctly formed #define statements?

Answer: D

Correct answer: #define SQR(X) (X*X)

Taking the others in turn:

  • #define INCH PER FEET 12 defines the macro INCH as the text PER FEET 12 - a macro name cannot contain spaces.
  • #define SQR(X) (X *X); ends in a semicolon, which travels with every expansion and breaks any use inside a larger expression.
  • #define SQR(X) X*X has no outer parentheses, so 1/SQR(2) becomes 1/2*2.

Only #define SQR(X) (X*X) is well formed - though even it should parenthesise the parameter, as ((X)*(X)), to survive an argument such as a+b.

Please log in or register to report this question.

114.

#include <stdio.h>
#define get(s) #s
 
int main()
{
    char str[] = get(GeeksQuiz);
    printf("%s", str);
    return 0;
}

 

Answer: C

Correct answer: GeeksQuiz

# in a macro body is the stringizing operator: it turns the argument into a string literal. So get(GeeksQuiz) becomes "GeeksQuiz", the array is initialised from it, and the program prints GeeksQuiz - without the #, which is an operator rather than part of the text.

Please log in or register to report this question.

115.

Nested macros are allowed.

Answer: B

Correct answer: TRUE

Macros may be nested, because the result of an expansion is rescanned for further macro names. #define max abc followed by #define abc 100 makes max expand to 100. The one limit is that a macro is not expanded recursively inside its own expansion, which prevents an infinite loop.

Please log in or register to report this question.

116.

Which statment is true about the given code ?

#include <stdio.h>
enum colors {lets,find,course};
int main()
{
  printf("%d %d %d",course,lets,find);
  return 0;
}

 

Answer: C

Correct answer: 2 0 1

Enumerators are numbered from 0 in declaration order unless given explicit values, so lets is 0, find is 1 and course is 2. The printf asks for them in the order course, lets, find, so the output is 2 0 1.

Please log in or register to report this question.

117.

Will the program compile successfully?

#include <stdio.h>

int main()
{
    printf("India" "BIX\n");
    return 0;
}

 

Answer: A

Correct answer: Yes

Adjacent string literals are concatenated by the compiler, so "India" "BIX\n" becomes the single literal "IndiaBIX\n". This is a language rule, and it is what lets a long message be split across several lines of source.

Please log in or register to report this question.

118.

The preprocessor directive which checks whether a constant expression results in a zero or non-zero value __________

Answer: A

#if checks whether a constant expression results in zero or not. If the expression is a non-zero value, then the statements between #if and #endif will be executed. If the constant expression results in a zero value, the statements between #if and #endif will not be executed.

Please log in or register to report this question.

119.

What is the Error of this program?

#include <stdio.h>
#define CONDITION(x) 
printf("letsfindcourse");                         
int main()
{
  CONDITION(0);
  return 0;
}

 

Answer: B

Correct answer: Compilation error

CONDITION(x) is defined with an empty body - the macro definition ends at the end of that line. The printf on the next line is therefore not part of the macro at all: it is an ordinary statement sitting at file scope, outside any function, which the compiler rejects. Continuing the definition onto a second line requires a backslash at the end of the first.

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