How a Preprocessor works in C?

0

A preprocessor is a system software that performs preprocessing on the source code written in a high-level language such as C. In C, a preprocessor is a program that processes source code before it is passed to the compiler. It is responsible for handling preprocessor directives, which begin with the "#" symbol. These directives include files, macros, and conditional compilation. The preprocessor replaces these directives with their corresponding code and then passes the modified source code to the compiler for further processing. This allows for efficient and convenient code reuse, as well as the ability to conditionally include or exclude code based on preprocessor definitions.


It is important to note that the preprocessor does not understand the scope rules of C, which means it does not recognize the program's block structure. Preprocessor directives like #define come into effect as soon as they are seen and remain in effect until the end of the file that contains them, regardless of the program's block structure. This is one of the reasons why it is important to use preprocessor directives carefully and judiciously in order to avoid unexpected behavior in the program.


The preprocessor performs several tasks on the HLL code before it is passed to the compiler. The main tasks are:

  1. Removing comments: The preprocessor removes all the comments from the source code. Comments are written for humans to understand the code and are not executed by the machine, so they are removed as they are not needed for execution.
  2. File inclusion: The preprocessor includes all the files from the library that the program needs. The #include directive is used to tell the preprocessor to include the contents of a library file. For example, #include <stdio.h> will tell the preprocessor to include all the contents in the library file stdio.h.
  3. Macro expansion: The preprocessor expands macros, which are small functions that are not as overhead to process. Macros can be defined using the #define directive. For example, #define SI 1000 defines a macro named SI with the value 1000. Macros can be deleted using the #undef directive.

It's also worth noting that the preprocessor can be invoked with the gcc -E option to see the file with removed comments and all preprocessor directives expanded. This file is saved with a '.i' extension (prog.i) which will be input to the compiler. And also, Macros can be written with multiple lines and each statement ends with “\”.

There are two types of macros in C:


Object-like macros: These are macros that do not take any parameters. They are defined using the #define directive and their value is replaced wherever they are used in the code. For example,


Function-like macros: These are macros that take one or more parameters. They are defined using the #define directive, and their value is replaced with the passed parameter. They can be defined using the parenthesis () around the parameters. For example,

It's also possible to delete a macro definition using the #undef directive. For example,

This would remove the macro definition for PI.


In C, macros can be written with multiple lines just like a function, but each statement must end with a backslash () to indicate that the macro definition continues on the next line. For example,

This way, the preprocessor knows that the macro definition continues on the next line and that the definition is complete only when the closing brace (} )is encountered.


It's worth noting that this kind of macro definition is less readable than a function definition, and it can cause some confusion or errors if not used carefully.

Tags

Post a Comment

0Comments
Post a Comment (0)