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

C Preprocessor Questions and Answers

1.

Which of the following are C preprocessors?

Answer: D

Correct answer: all of the mentioned

#ifdef, #define and #endif are all preprocessor directives - the first and third belonging to conditional compilation and the second to macro definition. Every directive begins with # and is processed before the compiler proper sees the file.

Please log in or register to report this question.

2.

What is the output of this program?

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

 

Answer: A

Correct answer: 27

x expands to 3 wherever it appears, so i = x*x*x; becomes i = 3*3*3;, giving 27. Since all three operands are simple constants there is no precedence trap here - unlike a macro whose body is an expression.

Please log in or register to report this question.

3.

What will be the output of the following C code?

#include <stdio.h>
#define san 10
main()
{
    #ifdef san
    #define san 20
    #endif
    printf("%d",san);
}

 

Answer: B

Correct answer: 20

san is defined, so the #ifdef is true and the redefinition to 20 takes effect. By the time the preprocessor reaches the printf, san means 20, so 20 is printed.

Please log in or register to report this question.

4.

#include <stdio.h>
#define X 3
#if !X
    printf("Geeks");
#else
    printf("Quiz");
  
#endif
int main()
{
        return 0;
}

 

Answer: C

Correct answer: Compiler Error

The #if is evaluated correctly - X is 3, so !X is 0 and the #else branch survives. But what survives is the line printf("Quiz"); sitting at file scope, outside any function, where only declarations are allowed. The compiler rejects it. Conditional compilation chooses which text reaches the compiler; it does not excuse that text from the language rules.

Please log in or register to report this question.

5.

What will be the output of the program?

#include <stdio.h>
#define MAN(x, y) ((x)>(y)) ? (x):(y);

int main()
{
    int i=10, j=5, k=0;
    k = MAN(++i, j++);
    printf("%d, %d, %d\n", i, j, k);
    return 0;
}

 

Answer: A

Correct answer: 12, 6, 12

The macro has no protection against multiple evaluation, so the arguments are substituted textually:

k = ((++i)>(j++)) ? (++i):(j++);

The condition increments i to 11 and yields j’s old value 5, leaving j at 6. Since \(11 > 5\), the true branch runs and increments i again, to 12, which is what k receives. Output: 12, 6, 12 - and i was incremented twice for a single-looking macro call, which is the danger the question illustrates.

Please log in or register to report this question.

6.

 

Output?

#include <stdio.h> 
#define f(g,g2) g##g2 
int main() 
{ 
   int var12 = 100; 
   printf("%d", f(var,12)); 
   return 0; 
}

 

Answer: A

Correct answer: 100

## pastes its operands into one token, so f(var,12) becomes var12 - the name of the local variable. The printf therefore prints its value, 100. Note the pasting happens before any macro argument would otherwise be expanded, which is why the result is a name rather than two separate tokens.

Please log in or register to report this question.

7.

What is the output generated by the following code?

#define square(a) (a*a)
printf("%d", square(4+5));

Answer: C

Correct answer: 29

The macro has no parentheses round its parameter, so the argument is pasted in as raw text:

printf("%d", (4+5*4+5));

Multiplication binds tighter than addition, giving \(4 + 20 + 5 = 29\) rather than the intended \(9^2 = 81\). Parenthesising the parameter - #define square(a) ((a)*(a)) - is what fixes it.

Please log in or register to report this question.

8.

What will be the output of the following C code?

#include <stdio.h>
    int main()
    {
        int one = 1, two = 2;
        #ifdef next
        one = 2;
        two = 1;
        #endif
        printf("%d, %d", one, two);
    }

 

Answer: B

Correct answer: 1, 2

next is never defined, so #ifdef next is false and the two assignments between it and #endif are removed before compilation. The variables keep their initial values, and the program prints 1, 2.

Please log in or register to report this question.

9.

What will be the output of the program?

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

int main()
{
    float s=10, u=30, t=2, a;
    a = 2*(s-u*t)/SQUARE(t);
    printf("Result = %f", a);
    return 0;
}

 

Answer: A

Correct answer: Result = -100.000000

The unparenthesised macro turns the expression into

a = 2*(s-u*t)/t*t;

and with / and * at equal precedence, left to right:

\[ 2 \times (10 - 60) = -100, \quad -100 / 2 = -50, \quad -50 \times 2 = -100 \]

So -100.000000, not the intended \(-100/4 = -25\).

Please log in or register to report this question.

10.

What will be the output of the program?

#include <stdio.h>
#define str(x) #x
#define Xstr(x) str(x)
#define oper multiply

int main()
{
    char *opername = Xstr(oper);
    printf("%s\n", opername);
    return 0;
}

 

Answer: C

Correct answer: print 'multiply'

This is the standard two-level stringizing idiom. A macro argument is not expanded before # is applied, so str(oper) would give "oper". Going through Xstr first forces the argument to be expanded - oper becomes multiply - and only then is it stringized, giving "multiply".

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