Pre-Processing in C

Pre-Processing in C Language with Examples

In this article, I will discuss the Pre-Processing in C Language with Examples. Please read our previous article, where we discussed Functions in C. As part of this article, you will learn what is Pre-Processing in C, its type, and when and how to use Pre-Processing in C programs with examples.

What is a Pre-Processor in C Language?

Preprocessing in C is a phase in the compilation process where certain initial transformations and operations are performed on the source code before it is compiled. The C preprocessor, which handles this phase, operates under the directives specified in the source code. These directives are instructions for the preprocessor and begin with the # symbol. Let us understand this in more detail.

Pre-processing is a program that will be executed automatically before passing the source program to the compiler. Pre-processing is under the control of pre-processor directives. All preprocessor directives start with a pound (#) symbol & should not be ended with a semicolon (;). When we are working with pre-processor directives, they can be placed anywhere within the program but are recommended to be placed on top of the program before defining the first function.

Types of Pre-Processor Directives in C Language:

In C programming language, the pre-processor directives are classifieds into 4 types, such as

1. File Inclusion (#include):

Used to include the contents of a file in another file.
Commonly used for including standard library headers (like stdio.h, stdlib.h) or other user-defined header files.
Example: #include <stdio.h>

2. Macro Definitions (#define):

Used to define macros, which are essentially code substitutions made by the preprocessor.
Can define object-like macros (constants) or function-like macros.
Example: #define PI 3.14159

3. Conditional Compilation:

Allows certain parts of code to be compiled or excluded based on specific conditions.
Involves directives like #if, #ifdef, #ifndef, #else, #elif, and #endif.
Useful for compiling code conditionally for different platforms, debugging purposes, etc.
Example:
#ifdef DEBUG
printf(“Debug Information\n”);
#endif

4. Miscellaneous Directives:

Pragma Directives (#pragma):
Used to offer machine-specific or compiler-specific instructions to the compiler.
Often used to control aspects like optimization, warnings, etc.
Example: #pragma once (common in header files to prevent double inclusion)

Predefined Macro Names:
The preprocessor defines several macros automatically, such as __DATE__ (the current date), __TIME__ (the current time), and __FILE__ (the current filename).

Error Directive (#error):
Used to generate an error message during compilation.
Useful for flagging incorrect configurations or conditions.
Example: #error “Unsupported configuration”

How is the Executable File Created in the C Language?

When we are working with any C application, we are required to perform 4 steps:

  1. Editing
  2. Compiling
  3. Linking
  4. Loading
Editing:

It is a process of constructing the source program and saving it with the “.c” extension. The “.c” file contains the application’s source code, i.e., user-readable format data. We required text editors like a notepad, word pad, or any other C language-related IDE to perform the editing process.

Compiling:

It is a process of converting high-level programming language code into machine-readable data, i.e., object or compiled code. We will get the “.OBJ” file when the compilation process is complete. The “.OBJ” file contains the compiled code. To compile any C program, we required a C-programming language compiler like GCC, Turbo-C, etc.

Linking:

It combines all the current project’s obj files and standard lib or obj files to construct an executable file. When the linking process succeeds, an automatically executable file is generated with the “.exe” extension. The “.exe” file contains the native code of the operating system.

Loading:

It carries the application file (.exe file) from the secondary storage area to primary memory, i.e., RAM. Editing, Compiling, and Linking are under the control of IDE, and loading is under the control of the Operating System. When we are working with any C application, it creates 5 types of files, i.e., .c, .BaK, .exe, .I, .obj

  1. .c, .I, .Bak contains user-readable format data, i.e., source format. Generally, the “.I” file contains extended source code constructed after pre-processing.
  2. .obj file contains compiled code that can be understandable to the system only.
  3. .exe file contains the native code of the Operating System.

For a better understanding of the above-discussed points, please have a look at the following diagram.

Pre-Processing in C

In the next article, I will discuss Macro Substitution Directives in C language. In this article, I try to explain Pre-Processing Directives in C Language with Examples. I hope you enjoy this Pre-Processing Directive in the C article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *