Preprocessor Directives In C Programming | LearntHub

Preprocessor Directive:- Preprocessor directives are lines which use in a program that begins with the character #.
Preprocessor Directives
Preprocessor directives follow special syntax rules that are different from the normal C syntax.
Read Also:
  1. Array
  2. Strings
  3. Structures
They are apply by the compiler to process some programs before compilation. Preprocessor directives can be divided into three type
  • Macro substitution directives 
  • File inclusion directives
  • Compiler control directives 
The set of common used preprocessor directives and their use is written below:
  • #define   - Defines a macro substitution 
  • #undef    - Undefines a macro 
  • #include - Specifies the files to be included 
  • #ifdef       - Test for a macro definition 
  • #endif      - Specifies the end of #if 
  • #ifndef    - Test whethera macro is not defined 
  • #if            - Test a compile time condition 
  • #else       - Specifies alternatives when #if test fails 
  • #elif         - Provides alternative test facility 
  • #pragma -Specifies certain instructions 
  • #error     -Stops compilation when an error occurs 
#ifdef: The syntax of #ifdef directive is:
   #ifdef macro_definition
Use: This directive is use to check whether the identifier is currently defined. Identifiers can be defined by a #define directive.

#undef: The syntax of #undef directive is:                  #undef token
Use: The #undef directive used for undefines a preprocessor macro defined previously using #define. Usually, #undef is used to scope a preprocessor constant into a very limited region.

#pragma: The syntax of #pragma is :        #pragma compiler specific extension
Use: The pragma directive is used to access compiler-specific preprocessor extensions.
Read Also:
A common use of #pragma is the #pragma once directive, which asks the compiler to include a header file only a single time, no matter how many times it has been taken.

#include: The syntax of #include directive is:
    #include <header name>
Use: The include directive instructs the preprocessor to paste the text of the given file into the current file. Generally, it is necessary to tell the preprocessor where to look for header files if they are not placed in the current directory or a standard system directory.

Macro:- Macro is a preprocessor directive which is indicated by the symbol "#define". C allows defining an identifier having constant value using #define directive. This directive is placed at the beginning of a C program. The symbol # occurs in the first column and no semicolon is use at the end.
For example:

Macro substitution: Macro substitution is a process where an identifier in a program is replaced by a predefined string composed of one or more tokens. The preprocessor accomplish this task under the direction of #define statement. This statement, usually known as a macro definition (or simply a macro) takes the following general form:
   #define identifier string 
If this statement is included in the program at the beginning, then the preprocessor replaces every occurrence of the identifier in the source code by the string.
There are different forms of macro substitution.
The most common forms are:
  • Simple macro substitution.
  • Augmented macro substitution. 
  • Nested macro substitution. 

Ma cro as a variable: A macro can be declared to accept a variable number of arguments.
The syntax for defining the macro is,

This kind of macro is called variadic.
When the macro is invoked, all the tokens in its argument list after the last named argument (this macro has none), including any commas, become the variable argument. This sequence of tokens replaces the identifier_VA_ARGS_in the macro body wherever it appears.
Thus, we have this expansion:
The variable argument is completely macro-expanded before it is inserted into the macro expansion, just like an ordinary argument.
Macro as a function: To define a funetion-like macro, we use the same #define' directive, but we put a pair of parenthesis immediațely after the macro name.
For example:

A function-like macro is only expanded if its name appears with a pair of parenthesis after it. If we write just the name, it is left alone. This can be useful when we have a function and a macro of the same name, and we wish to use the function sometimes.

Here the call to foo()will use the macro, but the function pointer will get the address of the real function. If the macro were to be expanded, it would cause a syntax error. 4. Application of macros: The use of a macro in place of a function eliminates the time delays associated with funetion calls.
If a program contains many reported function calls, the time savings resulting from the use of macros can become significant.
Types of macros: There are following type of macro, which written below:
1. Object-like macros: The object-like macros is an identifier which is replaced by the value. It is widely used to represent the constant value.
For example:
2. Function-like macros: The function macros looks like function call.
For example:

Post a comment