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

C Preprocessor Questions and Answers

11.

What will be the output of the following C code?

#define sqr(x) x*x
main()
{
    int a1;
    a1=25/sqr(5);
    printf("%d",a1);
}

 

Answer: A

The output of the code shown above is 25. This is because the arithmetic statement takes the form of: 25/5*5 (which is equal to 1). If the macro had been defined as #define sqr(x) (x*x), the output would have been 1.

Please log in or register to report this question.

12.

After preprocessing when the program is sent for compilation the macros are removed from the expanded source code.

Answer: A

Correct answer: TRUE

The preprocessor produces a translation unit in which every macro has been expanded and removed - no #define and no macro name survives into what the compiler parses. That is why a macro has no type, occupies no storage, and cannot be inspected in a debugger.

Please log in or register to report this question.

Answer: A

Correct answer: Tells the preprocessor to grab the text of a file and place it directly into the current file

#include performs a textual insertion: the named file’s contents replace the directive, and the result is then processed as if it had been typed there. This is why a header can define macros and types that the including file goes on to use, and why include guards are needed to stop the same text arriving twice.

Please log in or register to report this question.

14.

What will be the output of the following C code?

#include <stdio.h>
#define a 2
main()
{
    int r;
    #define a 5
    r=a*2;
    printf("%d",r);
}

 

Answer: A

Correct answer: 10

The preprocessor works on the text in order and ignores the braces entirely, so by the time it reaches r = a*2; the macro a has been redefined to 5. The statement becomes r = 5*2;, giving 10. Expect a redefinition warning, since there is no #undef in between.

Please log in or register to report this question.

15.

What will be the output of the following C code?

#include <stdio.h>
#define max 100
main()
{
    #ifdef max
    printf("hello");
}

 

Answer: D

The code shown above results in an error. This is because the preprocessor #endif is missing in the above code. If the identifier is defined, then the statements following #ifdef are executed till #endif is encountered.

Please log in or register to report this question.

16.

A macro must always be defined in capital letters.

Answer: B

Correct answer: False

Capitals are a convention, not a requirement. They serve as a warning to the reader that the name will be substituted textually, with all the traps that carries. The preprocessor accepts any valid identifier, and the standard library itself defines lower-case macros such as getchar.

Please log in or register to report this question.

17.

What will be the output of the following C code?

#include <stdio.h>
#define max 100
void main()
{
    #if(max)
    printf("san");
    #endif
    printf("foundry");
}

 

Answer: D

The code shown above is an illustration of the preprocessor directive #if. The value of the defined identifier max is 100. On checking the condition (100510==), which is true, the statements after #if are executed till #endif is encountered. Hence the output is sanfoundry.

Please log in or register to report this question.

Answer: C

Correct answer: a message from programmer to the preprocessor

A directive such as #include, #define or #ifdef is an instruction to the preprocessor, the phase that transforms the source text before the compiler sees it. It is not part of the C language proper, which is why directives have their own syntax and end at the end of a line rather than at a semicolon.

Please log in or register to report this question.

19.

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

 

Answer: B

Correct answer: 36

The macro has no parentheses, so square(6) expands to the bare text 6*6 and the statement becomes

x = 36/6*6;

/ and * have equal precedence and associate left to right, so this is \((36/6) \times 6 = 6 \times 6 = 36\), not 1. Writing the macro as #define square(x) ((x)*(x)) is what prevents this.

Please log in or register to report this question.

20.

What will be the output of the program?

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

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

 

Answer: A

Correct answer: 2, 3, 4,

Each call substitutes its own argument into the macro body, so the three calls become printf("%d,", x), printf("%d,", y) and printf("%d,", z). The output is 2, 3, 4, - with a trailing comma, since the format string ends with one.

Please log in or register to report this question.

Loading…

Other Subjects

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