What is Preprocessor Directive in C
Preprocessor directive
Q) What is a C preprocessor? What are the different types of preprocessor directives?
Preprocessor
- Some preprocessor remains a program that processes our source program before this is passed into the compiler.
- All preprocessor directives begin with a # symbol. For e.g., #include< stdio.h>
Image Source ~ Crafted With ©Ishwaranand – 2020 ~ Image by ©Ishwaranand |
Type of preprocessor directive
- Macro expansion
- File inclusion
- Conditional compilation
Macro expansion
- The directive written using # define is called as a macro. The macro substitution is the process where an identifier in a program is replaced by a predefined string.
- The processor replaces every occurrence of an identifier in the source code by a sting.
Simple Macro
Syntax;
# define identifier string
E.gÂ
# include PI 3.14
Macro with argumentsÂ
- The macro that accepts arguments is known as a macro with arguments.
Syntax:
  # define identifier (a1, a2, …, aN) string
E.g
# include cube (X) (x*x*x)
File inclusion
 The directive # include< filename> is used to include the contents of the file to the source program.
Syntax:
 # include <FileName.h>  OR #include “FileName.h”
Â
E.gÂ
#include<math.h>
- This directive causes the contents of file math.h to be included in our program so that all functions of math.h can be used in our program.
Conditional compilation
- A conditional compilation directive causes the preprocessor to conditionally suppress the compilation of portions of source code. These are:
- #if
- #ifdef
- #endif
- #ifndef
- #elif
- #else
E.g
void main()
{
…
#if(conditions)
statement 1;
#else
statement2;
# endif
…
}
Q) What is the difference between #include <filename>and #include “filename”?
- There exist two ways to write a #include statement. These are:
#include “filename”
#include<filename>
#include “filename.c”
- This command would loof for the file filename.c in the current directory as well as the specified list of directories as mentioned in the include search path that might have been set up.
#include<filename.c>
- This command would look for the file filename.c in the specified list of directories only.
Last updated on Sunday - May 21st, 2023