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

C Preprocessor Questions and Answers

Answer: B

Correct answer: a predefined data type in stdarg.h

va_list is a type - a typedef declared in <stdarg.h> - used to declare the object that holds the state of the walk through the variable arguments. va_start, va_arg and va_end are the macros that operate on it.

Please log in or register to report this question.

42.

What is the output of this program?

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

 

Answer: D

Correct answer: Compilation error

The macro body is 17; - semicolon included. Substituting it gives

printf("%d",17;);

which is a syntax error. This is why a #define for a constant should never end with a semicolon: the macro is text, and the semicolon travels with it wherever the name is used.

Please log in or register to report this question.

43.

# include <stdio.h>
# define scanf  "%s Geeks Quiz "
int main()
{
   printf(scanf, scanf);
   return 0;
}

 

Answer: D

Correct answer: %s Geeks Quiz Geeks Quiz

scanf is redefined as a string literal, so the call expands to

printf("%s Geeks Quiz ", "%s Geeks Quiz ");

The first copy is the format string and the second is the argument that its %s consumes. The %s is replaced by the whole second string, so the output is %s Geeks Quiz Geeks Quiz. Nothing stops a macro from shadowing a standard library name, which is exactly why it is a bad idea.

Please log in or register to report this question.

44.

The NULL character indicates an end of a string and a file.

Answer: B

Correct answer: FALSE

The null character marks the end of a string in memory, and nothing more. A file’s end is not marked by any byte within it: it is known from the file’s recorded length, and reported to the program as EOF. A file may perfectly well contain null bytes as data - every binary file does.

Please log in or register to report this question.

45.

Once preprocessing is over and the program is sent for the compilation the macros are removed from the expanded source code.

Answer: A

Correct answer: True

Preprocessing 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.

46.

What will be the output of the following C code?

#include <stdio.h>
void main()
{
    #ifndef max
    printf("hello");
    #endif
    printf("hi");
}

 

Answer: B

The code shown above illustrates the preprocessor directive #ifndef. If the identifier checked is not defined, then the statements following #ifndef are executed. Here, since the identifier max is not defined, the statement after #ifndef is executed. Hence the output is: hellohi

Please log in or register to report this question.

47.

Macros with arguments are allowed

Answer: A

Correct answer: True

Function-like macros with parameters are ordinary: #define MAX(a,b) ((a) > (b) ? (a) : (b)). The arguments are substituted as text, which is why the parameters and the whole body need parenthesising and why an argument with a side effect can be evaluated more than once.

Please log in or register to report this question.

48.

Which of the following are correctly formed #define statements in C?

Answer: C

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

The others each have a fault:

  • #define CUBE (X) (X*X*X); - the space after the name makes it an object-like macro whose body is the whole rest of the line, and the trailing semicolon travels with every use.
  • #define CUBE(x) (X*X*X) - the parameter is x but the body uses X, so the argument is never substituted.
  • #define CUBE(X) {X*X*X} - braces make it a block, which cannot appear where a value is wanted.

Only the third is well formed - though ((X)*(X)*(X)) would be safer still.

Please log in or register to report this question.

49.

What will be the output of the following C code?

#include <stdio.h>
#define san 557
main()
{
    #ifndef san
    printf("yes");
    #endif
    printf("no");
}

 

Answer: C

Correct answer: no

san is defined, so #ifndef san is false and the printf("yes") between it and #endif is deleted before compilation. Only the unconditional printf("no") remains, so the output is no.

Please log in or register to report this question.

50.

The translator which performs macro calls expansion is called:

Answer: C

Correct answer: Macro pre - processor

Macro expansion is done by the macro preprocessor, the phase that runs before compilation proper: it handles #include, #define and the conditional directives, and hands the compiler a file with no directives left in it. The linker, by contrast, works after compilation on object files.

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