Preprocessor Directives in C: Overview and Usage

Rumman Ansari   Software Engineer   2024-07-05 05:57:00   9733  Share
Subject Syllabus DetailsSubject Details 8 Questions
☰ TContent
☰Fullscreen

Table of Content:

The C language uses the preprocessor to extend its power and notation. C has a special feature of preprocessor which makes it different from other high level languages that don't have this type of facility. In this chapter, we present a detailed discussion of the preprocessor, including new features added by the ANSI C committee. We begin by explaining the use of #include. Then we thoroughly discuss the use of the #define macro facility. Macros can be used to generate inline code that takes the place of a function call. Their use can reduce program execution time.

Lines that begin with a # are called preprocessing directives. These lines communicate with the preprocessor. In ANSI C, the # can be preceded on the line by white space, whereas in traditional C, it must occur in column 1. The syntax for preprocessing directives is independent of the rest of the C language. The effect of a preprocessing directive starts at its place in a file and continues until the end of that file, or until its effect is negated by another directive. It is always helpful to keep in mind that the preprocessor does not "know c."

Some advantages of using preprocessor are-

  • Readability of the program is increased.
  • Makes the program portable and efficient.
  • Program modification becomes easy.

We know that the code that we write is translated into object code by the compiler. But before being compiled, the code is passed through the C preprocessor. The preprocessor scans the whole source code and modifies it, which is then given to the compiler.

preprocessor in c

The lines starting with the symbol # are known as preprocessor directives. When the preprocessor finds a line starting with the symbol #, it considers it as a command for itself and works accordingly. All the directives are executed by the preprocessor, and the compiler does not receive any line starting with # symbol.

Some features of preprocessor directives are-

  • Each preprocessor directive starts with a # symbol.
  • There can be only one directive on a line.
  • There is no semicolon at the end of a directive.
  • To continue a directive on next line, we should place a backslash at the end of the line.
  • The preprocessor directives can be placed anywhere in a program (inside or outside functions) but they are usually written at the beginning of a program.
  • A directive is active from the point of its appearance till the end of the program.

Functionalities performed by the preprocessor directives

  • Simple Macro Substitution
  • Macros with arguments
  • The Use of #define
  • Error generation, pragmas and predefined macro names.
  • Including 'files
  • The Use of #define

The preprocessor directives that perform these functions are as given below

  • #define
  • #else
  • #error
  • #if
  • #elif
  • #line
  • #ifdef
  • #endif
  • #pragma
  • #ifndef
  • #und6f

There are three operators that are used with these directives

Defined operator defined()
Stringizing operator #
Token pastingoperator ##

Tasks Performed By Pre-processor :

  • Replaces Trigraph Sequences
  • Joins any Line with the Backslash Character into Single Line
  • Divide Program into Set of Tokens
  • Expand Macroes
  • Remove Comments and Replace it by Single Space
  • Represent the Escape Sequence by its Internal Representation
  • Concatenate adjacent Constant character String

Types of Pre-processor  directives

preprocessor in c
  1. Macro Substitution Directive
  2. File Inclusive
  3. Conditional Compilation