Preprocessor Directives in C
- Get link
- X
- Other Apps
Preprocessing is an initial phase to process text before compilation. Preprocessor directives are lines of the source file where the first non-whitespace character is #, which distinguishes them from other lines of text. It operates under the control of preprocessor directive which is placed in the source program before the main().
Before the source code is passed through the compiler, it is examined by the preprocessor for any preprocessor directives. The preprocessor directives are always preceded by a hash sign (#). The preprocessor is executed before the actual compilation of program code begins.
Therefore, the preprocessor expands all the directives and take the corresponding actions before any code is generated by the program statements. No semicolon (;) can be placed at the end of a preprocessor directive.
Advantages of preprocessor directives
The advantages of using preprocessor directives in a C program include:
- Program becomes readable and easy to understand
- Program can be easily modified or updated
- Program becomes portable as preprocessor directives makes it easy to compile the program in
- different execution environments
- Due to the aforesaid reason the program also becomes more efficient to use
- Macros
- File Inclusion
- Conditional Compilation
- Other directives
- An object-like macro is a simple identifier which will be replaced by a code fragment. They are usually used to give symbolic names to numeric constants. Object like macros do not take an argument. It is the same what we have been using to declare constants using #define directive.
- The general syntax of defining a macro can be given as:
- #define identifier string
- The preprocessor replaces every occurrence of the identifier in the source code by a string.
- #define PI 3.14
- They are used to stimulate functions. When a function is stimulated using a macro, the macro definition replaces the function definition.
- The name of the macro serves as the header and the macro body serves as the function body. The name of the macro will then be used to replace the function call.
- The function-like macro includes a list of parameters.
- References to such macros look like function calls. However, when a macro is referenced,
- source code is inserted into the program at compile time. The parameters are replaced by the corresponding arguments, and the text is inserted into the program stream. Therefore, macros are considered to be much more efficient than functions as they avoid the overhead involved in calling a function.
- The syntax of defining a function like macro can be given as
- # define identifier(arg1,arg2,...argn) string
- The following line defines the macro MUL as having two parameters a and b and the
- replacement string (a * b):
- #define MUL(a,b) (a*b)
- Look how the preprocessor changes the following statement provided it appears after the macro definition.
- int a=2, b=3,c;
- c = MUL(a,b);
- An external file containing function, variables or macro definitions can be included as a part of our program. This avoids the effort to re-write the code that is already written.
- The #include directive is used to inform the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive
- appears.
- The #include directive can be used in two forms. Both forms makes the preprocessor insert the entire contents of the specified file into the source code of our program. However, the difference between the two is the way in which they search for the specified. #include <filename>
- This variant is used for system header files. When we include a file using angular brackets, a search is made for the file named filename in a standard list of system directories. #include "filename"
- This variant is used for header files of your own program. When we include a file using double quotes, the preprocessor searches the file named filename first in the directory containing the current file, then in the quote directories and then the same directories used for <filename>.
- The #line directive enables the users to control the line numbers within the code files as well as the file name that we want that appears when an error takes place. The syntax of #line directive is:
- #line line_number filename
- Here, line_number is the new line number that will be assigned to the next code line. The line numbers of successive lines will be increased one by one from this point on.
- Filename is an optional parameter that redefines the file name that will appear in case an error occurs. The filename must be enclosed within double quotes. If no filename is specified then the compiler will show the original file name. For example:
- #include<stdio.h>
- main()
- {
- #line 10 "Error.C"
- int a=10:
- #line 20
- printf("%d, a);
- }
- This directive is the simplest conditional directive.The controlled text will get included in the preprocessor output if the macroname is defined.
- The controlled text inside a conditional will embrace preprocessing directives. They are executed only if the conditional succeeds. You can nest these in multiple layers, but they must be completely nested.
- In other words, ‘#endif’ always matches the nearest ‘#ifdef’ (or ‘#ifndef’, or ‘#if’). Also, you can’t begin a conditional group in one file and finish it in another.
- Syntax:
- #ifdef MACRO
- controlled text
- #endif
- All these directives works together and control compilation of portions of the program using some conditions.
- If the condition with the #if directive results in a non zero value, then the group of line immediately after the #if directive will be executed otherwise if the condition with the #elif directive evaluates to a non zero value, then the group of line immediately after the #elif directive will be executed else the lines after #else directive will be executed.
- Syntax:
- #if macro_condition
- statements
- #elif macro_condition
- statements
- #else
- statements
- #endif
- Get link
- X
- Other Apps
Comments
Post a Comment
For any correction, doubts or suggestion, please let me know