C Preprocessor Directives

Advertisements
Advertisements

There are a lot of components or processors involved inside or outside the C compiler that affects the compilation process. One of them is the C preprocessor.

The preprocessor executes its own commands to the program before it is sent to the compiler for compilation. The preprocessor commands are called preprocessor directives.

Though the directives have nothing to do with C language, it has somehow become part of the C programming language.

Before you start learning about the C compiler, learn C programming basics.

Steps to Compile a C Program

To understand the role of the C preprocessor, we must first understand the process of compiling a C program successfully. The steps are listed below and each of the steps produces some output file.

Advertisements

Step 1: Write the Source code in the Text editor.

Output File: Prog.C

Step 3: The Prog.C to preprocessor and preprocessor expands its codes. The expanded source code is sent to C compiler.

Output File: Prog.I

Step 4: The Prog. I file is compiled and an intermediate object file is created if there is no error.

Output File: Prog.obj

Step 5: A linker links the object code of program and object code of library functions to a system and creates an executable code.

Output File: Prog.exe

To run the program, you need to execute the Prog.exe file.

Features of Preprocessor Directives

You can place a preprocessor directive anywhere within your program but the common practice is to place the code before the main function.

Every preprocessor directive starts with a pound sign (#) and then a directive name.

The next part in a preprocessor statement is called macro template and macro expansion.

For example,

#define SIDE 25

where

Advertisements

\#define is called directive.

SIDE is called macro \hspace{3px} template.

25 is the macro \hspace{2px} expansion.

Types of Directives

The type of directives is basically its purpose. It is about what the macro does in the program. We can divide the preprocessor directive into the following categories.

  • Macro Expansion
  • File Inclusion
  • Conditional Compilation
  • Other misc directives

In future articles, we will learn about each one of them in more details.

Bestseller

Effective C: An Introduction to Professional C Programming

A detailed introduction to the C programming language for experienced programmers.

The world runs on code written in the C programming language, yet most schools begin the curriculum with Python or Java. Effective C bridges this gap and brings C into the modern era–covering the modern C17

You’ll learn:

  • How to identify and handle undefined behavior in a C program
  • The range and representations of integers and floating-point values
  • How dynamic memory allocation works and how to use nonstandard functions
  • How to use character encodings and types
  • How to perform I/O with terminals and filesystems using C Standard streams and POSIX file descriptors
  • How to understand the C compiler’s translation phases and the role of the preprocessor
  • How to test, debug, and analyze C programs

Effective C will teach you how to write professional, secure, and portable C code that will stand the test of time and help strengthen the foundation of the computing world.

Advertisements
Advertisements
Advertisements