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

C Preprocessor Questions and Answers

21.

What is the output of this program?

#include <stdio.h> 
#include <stdlib.h>
#define square(x) x*x
int main()
{
  int i;
  i = 27/square(3);
  printf("%d",i);
  return 0;
}

 

Answer: D

Correct answer: 27

The macro has no parentheses, so 27/square(3) expands to

27/3*3

and with / and * at equal precedence and left associativity that is \((27/3) \times 3 = 9 \times 3 = 27\). The intended \(27/9 = 3\) needs #define square(x) ((x)*(x)).

Please log in or register to report this question.

22.

What will be the output of the following C code?

#include <stdio.h>
#define INDIA 1
#define US 2
#define CC US
main()
{
    #if CC==INDIA
    printf("Rupee");
    #elif CC==US
    printf("Dollar");
    #else
    printf("Euro");
    #endif 
}

 

Answer: C

Correct answer: Dollar

CC expands to US, which expands to 2 - expansions are rescanned. The first test 2 == 1 fails, the #elif test 2 == 2 succeeds, so printf("Dollar") is the only line that reaches the compiler.

Please log in or register to report this question.

23.

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

Answer: B

Correct answer: FALSE

No control is transferred. A macro call is expanded in place, before compilation, so the code ends up written out at each call site - there is no jump, no stack frame and no return. That is exactly what distinguishes a macro from a function: speed at the call, at the cost of code size and of type checking.

Please log in or register to report this question.

Answer: A

Correct answer: Used in a header file to avoid its inclusion more than once

#pragma once tells the preprocessor to include the file at most once per translation unit, however many times it is named. It does the same job as the traditional include guard (#ifndef HDR_H / #define HDR_H / #endif) with less typing, at the cost of not being in the standard - though every mainstream compiler supports it.

Please log in or register to report this question.

25.

What will be the output of the program?

#include <stdio.h>
#define MAX(a, b) (a > b ? a : b)

int main()
{
    int x;
    x = MAX(3+2, 2+7);
    printf("%d\n", x);
    return 0;
}

 

Answer: B

Correct answer: 9

The macro body parenthesises nothing, so the expansion is (3+2 > 2+7 ? 3+2 : 2+7). Addition binds tighter than >, so the comparison is \(5 > 9\), which is false, and the result is \(2+7 = 9\). Here the missing parentheses happen not to matter, but with an argument such as 1&2 they would.

Please log in or register to report this question.

Answer: C

Correct answer: Running a function at exiting the program

#pragma exit nominates a function to be run as the program terminates, after main returns - the counterpart of #pragma startup. Both are Borland extensions rather than standard C; the portable equivalent is atexit().

Please log in or register to report this question.

27.

#include <stdio.h>
#if X == 3
    #define Y 3
#else
    #define Y 5
#endif
 
int main()
{
    printf("%d", Y);
    return 0;
}

What is the output of the above program?

Answer: B

Correct answer: 5

X is never defined, and in a #if any identifier that is not a defined macro is replaced by 0. So the test reads 0 == 3, which is false, the #else branch is taken, Y becomes 5, and the program prints 5. Note this is decided at preprocessing time, so it cannot depend on any run-time value.

Please log in or register to report this question.

28.

What will be the output of the following C code?

#include <stdio.h>
#define hello 10
main()
{
    #ifdef hello
    #undef hello
    #define hello 100
    #else
    #define hello 200
    #endif
    printf("%d",hello);
}

 

Answer: C

Correct answer: 100

hello is defined, so the #ifdef branch runs: the old definition is removed with #undef and a new one, 100, put in its place. Using #undef before redefining is exactly the right way to do this, since it avoids the redefinition warning. The output is 100.

Please log in or register to report this question.

29.

A header file contains macros, structure declaration and function prototypes.

Answer: A

Correct answer: True

A header typically carries exactly those three things - macros, type and structure declarations, and function prototypes - because they are what several source files need to share. What it should not carry is a definition that allocates storage or a function body, since including it in two files would then produce two definitions.

Please log in or register to report this question.

30.

What will be the output of the program?

#include <stdio.h>
#define MESS junk

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

 

Answer: B

Correct answer: MESS

Macro replacement operates on tokens, and the contents of a string literal are not tokens. The MESS inside "MESS\n" is therefore left alone, and the program prints MESS. To get the macro’s text into a string you need the # operator.

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