Before we move on to the main topic we have to learn some basics regarding our concepts like preprocessor, and directives.
Directive: They mainly instruct our compiler on how it should process the input data. It can be done with the help of the preprocessors.
Preprocessor: It is a computer code that is made to run all our computers programs, application programs, and also the hardware.
These are some words that we should add at the start of our program as they make the main code text in our program executable.
They always begin with the character symbol #
, and it is followed by some specific directive name. They are called by the compiler before the step of the compilation to make the process of our program.
Note: The preprocessor directives are executed before compilation.
The C preprocessors are not part of our compiler, but they are some of the separate steps in the overall compilation process.
The commands that we use in the C preprocessors are called the C preprocessor directives.
In C language, we can define it as a macro processor to transform our code before its compilation. Some of the examples are:
#define
#if
#include
#error
It is also known as the macro preprocessor as they use macros. The block diagram shows the preprocessor in the C language:
#include
: It includes a particular header from another file.#include<stdio.h>int main(){printf("Hello Educative.io");return 0;}
#define
: It allows the definition of macros with source code.#include <stdio.h>#define MAX(s,v) ((s)>(v)?(s):(v))int main(){printf("Maximum of 4 and 6 is: %d\n", MAX(4,6));return 0;}
#undef
: It removes all definitions that are pre-specified.#include<stdio.h>#define num 6int div = num / num;#undef numint main(){printf("%d",div);return 0;}
#ifdef
: It allows for a conditional compilation process.#else
: It is quite opposite to #if
directive.#endif
: It closes the #if
, #ifdef
, or #ifndef
directives directly.#include <stdio.h>#define NOINPUTvoid main(){int s = 0;#ifdef NOINPUTs = 6;;#elseprintf("Enter value of s:");scanf("%d", &s);#endifprintf("Value of s: %d\n", s);}
#if
: It checks if the given condition is true
.#else
: It is totally opposite to if
.#include <stdio.h>#define NUMBER 0int main(){#if (NUMBER==0)printf("Value of Number is: %d",NUMBER);#elseprintf("value is not defined");#endif}
#error
: It just throws some error.#indef
: If macros are not defined, it returns true
.#include<stdio.h>#ifndef __MATH_H#error First include then compile#elsevoid main(){float s;s = sqrt(6);printf("%f",s);}#endif
#pragma
: It provides more information (commands) to the compiler.#include<stdio.h>#pragma startup funcvoid main(){printf("\nI am in main");}
It is part of code that is given a name and is replaced by the value of a macro.
It is defined by #define
.
There are two types of macros:
#define PI 3.14
#define MAX(s,v) ((s)(v)?(s):(v))
Note: Here
PI
andMAX
are the names of the macro.