Skip to study material
← Question index

QUESTION 32 · Qualifiers and Preprocessor

Explain the C preprocessor and its main directives.

5 marks

Answer in brief

The preprocessor handles directives and macro replacement as part of translating C source. A directive normally begins with # after any leading whitespace.

Main directives

  1. #include makes header contents available for preprocessing.
  2. #define creates an object-like or function-like macro.
  3. #undef removes a macro definition.
  4. #if, #ifdef, #ifndef, #elif, #else and #endif control which source text is included.
  5. #error requests a diagnostic when a required configuration is missing or invalid.

Header inclusion

Use #include <stdio.h> for the standard input/output declarations. Quoted inclusion, such as #include "settings.h", is commonly used for project headers. The implementation defines the precise search rules. Including a header is not the same as copying the compiled implementation of every library function into the program.

Example explanation

The sample defines LIMIT as 3. During preprocessing, uses of the macro name are replaced by its replacement tokens. The resulting loop prints 1, 2 and 3. The macro is not an ordinary int variable and does not have a separate object storing 3.

Why it is useful

Headers support shared declarations. Macros can name configuration values, and conditional compilation can choose platform or debug code. These operations affect the translated source; an ordinary runtime if instead makes a decision while the program is running.

Important points

Do not put a semicolon after a simple numeric macro unless a semicolon is intentionally part of the replacement. Macros are not checked like typed function parameters. Inspect expanded expressions carefully, especially when arithmetic operators or repeated arguments are involved.

Flowchart

Control flow for translation with labelled true and false paths.
Explain the C preprocessor and its main directives. · Download diagram

C program and output

/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
#define LIMIT 3
int main(void)
{
    for (int i = 1; i <= LIMIT; ++i) { printf("%d\n", i); }
    return 0;
}

Sample input and output

This example uses values already written in the program.

Output

1
2
3

Related exam questions

Related questions in the preprocessor category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.

  • September/October 2021 · Q3(b) · 8 marks

    What is the use of preprocessor directives? Explain.

    Paper P01 · page 1
  • March/April 2023 · Q1(e) · 2 marks

    Differentiate #undef and #ifndef.

    Paper P02 · page 1

View this category in the exam question bank →