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

C Preprocessor Questions and Answers

61.

Which file is generated after pre-processing of a C program?

Answer: B

Correct answer: .i

The preprocessor produces a translation unit with all includes pulled in and all macros expanded, and by convention that file has the extension .i (.ii for C++). You can see it with gcc -E prog.c -o prog.i. .o is the object file produced later, by the assembler.

Please log in or register to report this question.

62.

What will be the output of the program?

#include <stdio.h>
#define CUBE(x) (x*x*x)

int main()
{
    int a, b=3;
    a = CUBE(b++);
    printf("%d, %d\n", a, b);
    return 0;
}

 

Answer: C

Correct answer: 27, 6

CUBE(b++) expands to (b++*b++*b++), which modifies b three times in one expression with no sequence point between - formally undefined behaviour. The listed answer, 27 and 6, is what the common compilers give: the three operands all read 3 before any increment takes effect, and b ends three higher. Nothing should rely on it; the real lesson is never to pass an argument with a side effect to a macro that uses it more than once.

Please log in or register to report this question.

63.

What is the output of the following C program?

#include <stdio.h>
#define SQR(x) (x*x)
int main()
{
    int a;
    int b=4;
    a=SQR(b+2);
    printf("%d\n",a);
    return 0;
}

 

Answer: A

Correct answer: 14

With b equal to 4, the unparenthesised macro expands to

a = b+2*b+2;

and multiplication binds tighter than addition: \(4 + 8 + 2 = 14\). The intended \(6^2 = 36\) requires the parameters to be parenthesised: #define SQR(x) ((x)*(x)).

Please log in or register to report this question.

64.

A macro must always be written in capital letters.

Answer: B

Correct answer: FALSE

Capitals are a strong convention, not a rule - they warn the reader that the name is a macro and will be substituted textually. The preprocessor accepts any valid identifier, and the standard library itself defines lower-case macros such as getchar and putc.

Please log in or register to report this question.

65.

It is necessary that a header files should have a .h extension?

Answer: B

Correct answer: No

The .h suffix is a convention. #include takes whatever name it is given and pastes in the contents of that file, so a header may be called anything - C++ standard headers have no extension at all. Following the convention is still worth doing, since it tells the reader what the file is for.

Please log in or register to report this question.

66.

What will be the output of the following C code?

#include <stdio.h>
#define max 20
main()
{
    #ifndef max
    #define min 10
    #else
    #define min 30
    #endif
    printf("%d",min);
}

 

Answer: C

Correct answer: 30

max is defined, so #ifndef max is false and the #else branch is taken, defining min as 30. The program prints 30.

Please log in or register to report this question.

67.

What is the output of this program?

#include <stdio.h>
#define int char
main()
{
  int i=50;
  printf ("sizeof (i) =%d", sizeof (i));
}

 

Answer: D

Correct answer: 1

#define int char replaces the token int everywhere it appears afterwards, so int i = 50; becomes char i = 50; and sizeof(i) is 1. The preprocessor has no notion of keywords being reserved - it substitutes tokens, and int is just a token to it.

Please log in or register to report this question.

68.

What is the output of this program?

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

 

Answer: B

Correct answer: Compilation error

The macro CONDITION(x) has an empty body, since a #define ends at the end of its line. The printf below it is therefore an ordinary statement at file scope, where only declarations may appear, and the compiler rejects it. To make it part of the macro the first line would have to end in a backslash.

Please log in or register to report this question.

Answer: A

Correct answer: The file is searched for in the standard compiler include paths

Angle brackets - #include <stdio.h> - tell the preprocessor to look in the standard system include directories. Quotes - #include "myheader.h" - add the directory of the current source file to the front of that search, which is why they are used for a project’s own headers.

Please log in or register to report this question.

70.

What will be the output of the program?

#include <stdio.h>
#define SWAP(a, b) int t; t=a, a=b, b=t;
int main()
{
    int a=10, b=12;
    SWAP(a, b);
    printf("a = %d, b = %d\n", a, b);
    return 0;
}

 

Answer: B

Correct answer: a = 12, b = 10

The macro expands to int t; t=a, a=b, b=t; inside main, where a declaration followed by an expression statement is perfectly legal. The comma operator sequences the three assignments left to right, so the values are exchanged and the output is a = 12, b = 10. The macro would break if used twice in the same block, since t would be declared twice.

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