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

C Preprocessor Questions and Answers

31.

#include <stdio.h>
#define a 10
int main()
{
  printf("%d ",a);
 
  #define a 50
 
  printf("%d ",a);
  return 0;
}

 

Answer: B

Correct answer: 10 50

The preprocessor works line by line, top to bottom, so the first printf is expanded while a still means 10 and the second after it has been redefined to 50. The output is 10 50. Redefining a macro to a different value without an intervening #undef is not strictly conforming, so a compiler will warn.

Please log in or register to report this question.

32.

What is the output of this program?

#include <stdio.h>
int main()
{
  printf("%d",30);
  return 0;
}

 

Answer: C

Correct answer: 30

There is nothing wrong with the program: <stdio.h> is included, main returns int, and printf("%d", 30) matches its format specifier to an int argument. It compiles cleanly and prints 30.

Please log in or register to report this question.

33.

In a macro call the control is passed to the macro.

Answer: B

Correct answer: False

There is no transfer of control. A macro is expanded in place, before compilation, so its body ends up written out at each call site - no jump, no stack frame, no return. That is the essential difference from a function: the speed of inline code, at the cost of size and of type checking.

Please log in or register to report this question.

34.

Will the following program print the message infinite number of times?

#include <stdio.h>
#define INFINITELOOP while(1)

int main()
{
    INFINITELOOP
    printf("IndiaBIX");
    return 0;
}

 

Answer: A

Correct answer: Yes

The macro expands to while(1), and the printf on the next line becomes the loop’s body - a while without braces takes the single statement that follows it, and a newline means nothing to the compiler. So the message is printed forever, and return 0; is never reached.

Please log in or register to report this question.

35.

What will be the output of the program?

#include <stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%d\n", FUN(va1, 2));
    return 0;
}

 

Answer: B

Correct answer: 20

## pastes its operands into a single token, so FUN(va1, 2) becomes va12 - the name of the second variable. The printf therefore prints 20.

Please log in or register to report this question.

36.

Preprocessor directive #ifdef .. #else ... #endif is used for conditional compilation.

Answer: A

Correct answer: True

#ifdef / #else / #endif is exactly the mechanism for conditional compilation: only one branch survives preprocessing, and the other never reaches the compiler at all. It is the usual way of adapting a source file to different platforms or of compiling out debugging code.

Please log in or register to report this question.

37.

A preprocessor directive is a message from compiler to a linker.

Answer: B

Correct answer: False

A directive is a message from the programmer to the preprocessor. The compiler communicates with the linker through the object file it produces, not through anything written with a #.

Please log in or register to report this question.

38.

What will be the output of the following C code?

#include <stdio.h>
#define hello 
main()
{
    #ifdef hello
    #define hi 4
    #else
    #define hi 5
    #endif
    printf("%d",hi);
 
}

 

Answer: A

The code shown above illustrates #define, #ifdef, #else, #endif. Since the identifier hello is defined (condition of #if is true), another identifier names hi is defined and it’s value is 4. If hello was not defined, then the value of hi would be 5.

Please log in or register to report this question.

39.

In which stage the following code

#include <stdio.h>

gets replaced by the contents of the file stdio.h

Answer: D

Correct answer: During preprocessing

#include is handled in the preprocessing phase, before compilation: the header’s text is pasted in, producing a single translation unit for the compiler. Nothing about it happens at link time or run time.

Please log in or register to report this question.

40.

What will be the output of the following C code?

#include <stdio.h>
#define hello 10
void main()
{
    printf("%d",hello);
    #undef hello
    printf("%d",hello);
}

 

Answer: C

Correct answer: error

The first printf is fine, but #undef hello removes the definition, so in the second printf the identifier hello is no longer replaced by 10. It is then an undeclared variable, and the compiler reports an error.

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