Preprocessor Questions with Answers

29 practice questions on Preprocessor (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 29 free in the AptiNest app.

Sample questions

Question 1Medium

The C-preprocessors are specified with __symbol.

  1. #
  2. $
  3. " "
  4. None of the mentioned

Answer: A. #

Explanation

The C-preprocessors are specified with the # symbol because it is a special character in C that indicates a preprocessor directive, such as including a header file or defining macros.

Step-by-step solution

  1. Preprocessor directives such as #include, #define and #if all begin with the # symbol.
  2. The # must be the first non-blank character on the line for the directive to be recognised.
  3. $ and quotes are not directive markers in standard C.
  4. So the answer is: #.

Why the other options are wrong

  • B. $ is not a preprocessor directive symbol in standard C.
  • C. A pair of quotes with a space is not a directive marker.
  • D. # is the directive marker, so an option is correct.

Tip: Every preprocessor directive begins with #.

Question 2Medium

#include is called

  1. Preprocessor directive
  2. Inclusion directive
  3. Macro definition
  4. None of the mentioned

Answer: A. Preprocessor directive

Explanation

Both user and system header files are included using the preprocessing directive "#include".
It has two variants:

#include <file>

This variant is used for system header files.
It searches for a file named file in a standard list of system directories.
You can prepend directories to this list with the -I #include "file" This variant is used for header files of your own program. It searches for a file named file first in the directory containing the current file, then in the quote directories and then the same directories used for <file>.
You can prepend directories to the list of quote directories with the -iquote. The argument of "#include", whether delimited with quote marks or anglebrackets, behaves like a string constant in that comments are not recognized, and macro names are not expanded. Thus,

#include < x/*y>

specifies inclusion of a system header file named x/*y.

Step-by-step solution

  1. Lines starting with # are handled by the preprocessor before the compiler runs.
  2. #include is one of these lines, and it tells the preprocessor to insert another file.
  3. So #include is a preprocessor directive.
  4. So the answer is Preprocessor directive.

Why the other options are wrong

  • B. Inclusion directive describes what it does, but the general name is preprocessor directive.
  • C. Macros are defined with #define; #include inserts a file.
  • D. A correct option is listed, so none of the mentioned is wrong.

Tip: Every line starting with # is a preprocessor directive.

Question 3Medium

A preprocessor is a program

  1. That processes its input data to produce output that is used as input to another program
  2. That is nothing but a loader
  3. That links various source files
  4. All of the mentioned

Answer: A. That processes its input data to produce output that is used as input to another program

Explanation

A preprocessor is a preprocessor is a program that processes its input data to produce output that is used as input to another program.

Step-by-step solution

  1. A preprocessor is a separate program that runs before the compiler.
  2. It takes source text and produces output, which is then used as input to the next program, the compiler.
  3. That matches the first option.
  4. The answer is That processes its input data to produce output that is used as input to another program.

Why the other options are wrong

  • B. A loader loads a program into memory; it is not the preprocessor.
  • C. Linking combines object files, which is the linker's job.
  • D. All of the mentioned is wrong because options B and C are not correct.

Tip: Preprocessing comes before compiling, linking and loading.

Question 4Medium

What will be the output of the program?

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

Answer: B. 11

Explanation

The macro function

SQR(x)(x*x)

calculate the square of the given number 'x'. (Eg: 102)
1:

int a, b=3;

Here the variable a, b are declared as an integer type and the variable b is initialized to 3.

2:

a = SQR(b+2);

becomes,

=> a = b+2 * b+2; Here SQR(x) is replaced by macro to x*x .
=> a = 3+2 * 3+2;
=> a = 3 + 6 + 2;
=> a = 11;

3:

printf("%d\n", a);

It prints the value of variable 'a'.Hence the output of the program is 11

Step-by-step solution

  1. The macro SQR(x) expands to (x*x) with no outer brackets, and the argument is b+2.
  2. So SQR(b+2) expands to (b+2*b+2), not ((b+2)*(b+2)).
  3. With b = 3: 3 + 2 × 3 + 2 = 3 + 6 + 2 = 11.
  4. So a is 11, which is option B.

Why the other options are wrong

  • A. 25 comes from squaring b + 2 correctly, but the macro does not add the brackets.
  • C. The code compiles and runs, so there is no error.
  • D. The value is fully defined, not garbage.

Tip: Always put brackets around macro parameters and the whole macro body.

Question 5Medium

What will be the output of the program?

#include<stdio.h>
#define MIN(x, y) (x< y)? x : y;
int main()
{
    int x=3, y=4, z;
    z = MIN(x+y/2, y-1);
    if(z > 0)
        printf("%d\n", z);
    return 0;
}
  1. 3
  2. 4
  3. 0
  4. No output

Answer: A. 3

Explanation

The macro

MIN(x, y) (x< y)? x : y;

returns the smallest value from the given two numbers.

1:

int x=3, y=4, z;

The variable x, y, z are declared as an integer type and the variable x, y are initialized to value 3, 4 respectively.

2:

z = MIN(x+y/2, y-1);

becomes,

=> z = (x+y/2 < y-1)? x+y/2 : y - 1;
=> z = (3+4/2 < 4-1)? 3+4/2 : 4 - 1;
=> z = (3+2 < 4-1)? 3+2 : 4 - 1;
=> z = (5 < 3)? 5 : 3;

The macro return the number 3 and it is stored in the variable z.

3:

if(z > 0) becomes if(3 > 0)

here the if condition is satisfied.
It executes the if block statements.

4:

printf("%d\n", z);.

It prints the value of variable z.
Hence the output of the program is 3

Step-by-step solution

  1. The macro is replaced by its text, so MIN(x+y/2, y-1) becomes (x+y/2 < y-1)? x+y/2: y-1; with the semicolon.
  2. x+y/2 is 3 + 2 = 5, and y-1 is 4 - 1 = 3.
  3. The condition 5 < 3 is false, so the value is y-1, which is 3.
  4. Z is 3, and the test z > 0 is true, so the program prints 3.
  5. So the output is 3.

Why the other options are wrong

  • B. 4 is y, but the false branch gives y-1, which is 3.
  • C. 0 would be the output only if z were 0 or less, but z is 3.
  • D. The program prints because z > 0 is true.

Tip: Expand a macro as plain text first, and watch the operator precedence of the expanded text.

Question 6Medium

#include statement must be written

  1. Before main()
  2. Before any scanf/printf
  3. After main()
  4. Inside main() only

Answer: B. Before any scanf/printf

Explanation

A header such as stdio.h declares printf and scanf, so its #include must come before the first call to them.
Writing it at the top of the file, before main(), is the usual style, but the rule is only that it comes before the functions are used.

Step-by-step solution

  1. #include copies the header file into the program before compilation.
  2. The header must come before the first use of any function it declares, such as scanf or printf.
  3. Placing #include before main is common but not required, as long as it comes before the first use.
  4. So the answer is Before any scanf/printf.

Why the other options are wrong

  • A. Before main is common practice, but the real rule is before the first use.
  • C. After main is too late, because main already calls the functions before the header is read.
  • D. It does not have to be inside main(); it only has to come before the first use.

Tip: A header must be read before the compiler meets the first call to its functions.

Question 7Medium

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;
}
  1. Result = -100.000000
  2. Result = -25.000000
  3. Result = 0.000000
  4. Result = 100.000000

Answer: A. Result = -100.000000

Explanation

The macro function SQUARE(x) x*x calculate the square of the given number 'x'. (Eg: 102)

1:

float s=10, u=30, t=2, a;

Here the variable s, u, t, a are declared as an floating point type and the variable s, u, t are initialized to 10, 30, 2.

2:

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

becomes,

=> a = 2 * (10 - 30 * 2) / t * t;

Here SQUARE(t) is replaced by macro to t*t.

=> a = 2 * (10 - 30 * 2) / 2 * 2;

=> a = 2 * (10 - 60) / 2 * 2;

=> a = 2 * (-50) / 2 * 2;

=> a = 2 * (-25) * 2;

=> a = (-50) * 2;

=> a = -100;

3:

printf("Result=%f", a);

It prints the value of variable 'a'. Hence the output of the program is -100

Step-by-step solution

  1. SQUARE(t) is a macro, so it expands to t*t with no brackets, and the line becomes a = 2*(s-u*t)/t*t.
  2. Compute s-u*t = 10 - 30×2 = 10 - 60 = -50.
  3. Evaluate left to right: 2×(-50) = -100, then -100/2 = -50, then -50×2 = -100.
  4. So a = -100 and the output is: Result = -100.000000.

Why the other options are wrong

  • B. This is the value if SQUARE(t) were treated as one unit: 2×(-50)/4 = -25. The macro has no brackets, so it gives -100.
  • C. The result is 0 only if the bracket term were zero, but s-u*t is -50.
  • D. The sign is wrong: s-u*t is negative, so the result is negative.

Tip: Expand macros as plain text before doing the arithmetic; a macro without brackets can change the result.

Question 8Medium

What will be the output of the program?

#include<stdio.h>
#define MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c)
int main()
{
    int x;
    x = MAX(3+2, 2+7, 3+7);
    printf("%d\n", x);
    return 0;
}
  1. 5
  2. 9
  3. 10
  4. 3+7

Answer: C. 10

Explanation

The macro

MAX(a, b, c) (a>b ? a>c ? a : c: b>c ? b : c)

returns the biggest of given three numbers.

1:

int x;

The variable x is declared as an integer type.

2:

x = MAX(3+2, 2+7, 3+7);

becomes,

=> x = (3+2 >2+7? 3+2 > 3+7? 3+2: 3+7: 2+7 > 3+7? 2+7: 3+7)

=> x = (5 >9? (5 > 10? 5: 10): (9 > 10? 9: 10) )

=> x = (5 >9? (10): (10) )

=> x = 10

3:

printf("%d\n", x);

It prints the value of 'x'.

Hence the output of the program is "10".

Step-by-step solution

  1. The macro is plain text substitution, so MAX(3+2, 2+7, 3+7) becomes (3+2>2+7? 3+2>3+7? 3+2: 3+7: 2+7>3+7? 2+7: 3+7).
  2. The values are 3+2 = 5, 2+7 = 9 and 3+7 = 10.
  3. The first test is 5 > 9, which is false, so the second branch is taken.
  4. In the second branch 9 > 10 is false, so the value is 3+7, which is 10.
  5. The output is 10.

Why the other options are wrong

  • A. 5 is the value of 3+2, but the first branch is never taken because 5 > 9 is false.
  • B. 9 is the value of 2+7, but 9 > 10 is false, so that branch is not chosen.
  • D. The macro gives the computed value 10, not the text 3+7.

Tip: Macro arguments are not evaluated first; the expression is pasted in and then evaluated.

Question 9Medium

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;
}
  1. 12, 6, 12
  2. 11, 5, 11
  3. 11, 5, Garbage
  4. 12, 6, Garbage

Answer: A. 12, 6, 12

Explanation

The macro

MAN(x, y) ((x)>(y)) ? (x):(y);

returns the biggest number of given two numbers.

2:

k = MAN(++i, j++); becomes,
=> k = ((++i)>(j++)) ? (++i):(j++);
=> k = ((11)>(5)) ? (12):(6);
=> k = 12

3:

printf("%d, %d, %d\n", i, j, k);

It prints the variable i, j, k.
In the above macro 2 the variable i value is incremented by 2 and variable j value is incremented by

1. Hence the output of the program is 12, 6, 12

Step-by-step solution

  1. - MAN(++i, j++) expands to ((++i)>(j++))? (++i):(j++); with an extra semicolon.
  2. - The comparison evaluates ++i to 11 and j++ to 5 (j becomes 6); 11 > 5 is true.
  3. - The true branch runs ++i, so i becomes 12 and the value 12 is assigned to k.
  4. - The printf prints i = 12, j = 6, k = 12.
  5. - The output is 12, 6, 12.

Why the other options are wrong

  • B. I is changed twice, once in the comparison and once in the branch, so it is 12, not 11.
  • C. K is assigned 12, so it is not garbage.
  • D. K is assigned 12, so it is not garbage, and i is 12.

Tip: Macro arguments with side effects run every time the macro uses them.

Question 10Medium

#pragma exit is primarily used for?

  1. Checking memory leaks after exitting the program
  2. Informing Operating System that program has terminated
  3. Running a function at exitting the program
  4. No such preprocessor exist

Answer: C. Running a function at exitting the program

Explanation

It is primarily used for running a function upon exiting the program.

Step-by-step solution

  1. The #pragma exit directive, as in Turbo C and similar compilers, names a function to run when the program exits.
  2. It is the counterpart of #pragma startup, which runs a function before main.
  3. It does not report to the operating system, which is done by return or exit().
  4. It does not check memory leaks.
  5. The answer is Running a function at exitting the program.

Why the other options are wrong

  • A. The pragma does not check for memory leaks.
  • B. Telling the operating system about termination is done by return or exit(), not by this pragma.
  • D. The pragma exists in several compilers, so the statement is false.

Tip: #pragma startup runs before main and #pragma exit runs after main.

Practise all 29 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics