In: Computer Science
Question 2.
Explain preprocessor directives in C++.
int x = VALUE;
The preprocessor director in c++ gives the instructions to compiler for preprocessing the data or information before the actual compilation begins. It is the lines that which is included in the program and is invoked by the compiler for doing the process before the compilation. It begins with the # symbol. Only whitespace character will be there before the preprocessor and we cannot write anything before that. Also they do not end with a semicolon .
Example is #define ,#include ,etc
#define PI 3.14 - when we define this PI with 3.14 and in the program at compiling time instead of PI the value 3.14 is replaced.
Error directives is used to stop or aborting the compilation when an error is been found in the program.
Syntax is #error message
Example: we are using the sqrt () function and we do not use the math.h header file, then we get the error.
Now include the math function,
Here we get the right output when the #define cmath is added.
pragma directives helps us to specify functions which is required for the run before the execution of main().
#include <stdio.h>
void func();
#pragma startup func
void func()
{
Printf("Inside func()\n");
}
int main()
{
printf("inside main()\n");
Return 0;
}
Define directives are used for defining some constant . The constant will be replaced by the value at compile time
Example
undef is used to tell for removing the macro definition. Here I have used under PI after defining PI. We get an error.
The code is shown below,
#include<stdio.h>
#define VALUE 5
int main()
{
int x=VALUE;
printf("%d",x);
return 0;
}
Hope this helps you. Please give a thumbs up if it helped you. Comment for more doubts regards to the same. Thanks for posting