Updated March 28, 2023
Introduction to #Error in C
The #error in C is one of the most useful features of the C preprocessor, which is a directive used to indicate the error. And it causes the compilation to fail while allowing us to issue a statement that will be displayed as the compilation error. #error directive can be combined with if condition or elif condition or else condition to cause the failure of compilation and display the appropriate message. Whenever the #error directive is encountered in the program, and the condition is true, the corresponding message is displayed as the compilation error output, and the rest of the program compilation is skipped.
The syntax of #Error in C is as follows:
#error <Compilation failed error message to be displayed as the output>
Working of #Error in C is as follows
- Whenever there is a need to indicate an error while displaying the appropriate compilation error message and skip the execution of the rest of the program, we make use of the #error directive in C.
- When we make use of the #error in C, it causes the compilation to fail while allowing us to issue a statement that will be displayed as the compilation error.
- #error directive can be combined with if condition or elif condition or else condition to cause the failure of compilation and display the appropriate message.
- Whenever the #error directive is encountered in the program, and the condition is true, the corresponding message is displayed as the compilation error output, and the rest of the program compilation is skipped.
Examples of #Error in C
Given below are the examples of #Error in C:
Example #1
C program to demonstrate the use of #error directive in a program to display the custom compilation error message on encountering the #error directive while the condition being true and skips the compilation of the rest of the program
Code:
#include<stdio.h>
//making use of if-else condition while including #error directive with the appropriate compilation failed message to be displayed on the output screen
#ifndef __MATH_H
#error Please include math.h header before compiling the program
#else
//if the #error directive condition is false, then the else part containing the main method is executed which consists of three doubles variables to store double values and then make use of pow function to find the power and display the value.
int main()
{
double a = 2.0;
double b = 2.0;
double c;
c = pow(a, b);
printf("The power of a raised to b is : %lf", c);
return 0;
}
//if else condition ends here
#endif
Output:
In the above program, we make use of the if-else condition while including the #error directive with the appropriate compilation failed message to be displayed on the output screen. Here we are checking if the math.h header is included in the program or not in the if-else condition because the program makes use of mathematical functions from the math.h header. If the header is not included, the custom #error directive message is displayed, and the rest of the program is skipped. If the math.h header is included in the program, the else part is executed, which computes the power of a number, given two numbers, and displays the output. The output is as shown in the snapshot above.
Example #2
C program to demonstrate the use of #error directive in a program to display the custom compilation error message on encountering the #error directive while the condition being true and skips the compilation of the rest of the program
Code:
#include<stdio.h>
//making use of if else condition while including #error directive with the appropriate compilation failed message to be displayed on the output screen
#ifndef __MATH_H
#error Please include math.h header before compiling the program
#else
//if the #error directive condition is false, then the else part containing the main method is executed which consists of three doubles variables to store double values and then make use of pow function to find the power and display the value.
int main()
{
int a, b;
a = abs(-50);;
printf("The absolute value of the given number is: %d\n", a);
b = abs(-100);;
printf("The absolute value of the given number is: %d\n", b);
return 0;
}
//if else condition ends here
#endif
Output:
In the above program, we make use of the if-else condition while including the #error directive with the appropriate compilation failed message to be displayed on the output screen. Here we are checking if the math.h header is included in the program or not in the if-else condition because the program makes use of mathematical functions from the math.h header. If the header is not included, the custom #error directive message is displayed, and the rest of the program is skipped. If the math.h header is included in the program; the else part is executed, which finds the absolute value of the given number and displays the output. The output is as shown in the snapshot above.
Advantages of #Error in C
There are several advantages of using the #error directive in C. They are:
- By using the #error directive in the program, the compilation time required to compile the entire program is saved because as soon as the #error directive is encountered in the program, the compilation of the program stops there and skips the compilation of the rest of the program.
- Custom compilation failed messages can be displayed as the output by making use of the #error directive.
Conclusion
In this tutorial, we understand the concept of #error directive in C through definition, syntax, and working of #error directive through programming examples and their outputs and the advantages of using #error directive in our program to display the custom compilation failed message and skip the compilation of the rest of the program.
Recommended Articles
This is a guide to #Error in C. Here we discuss the Working of #Error in C with Syntax and Examples along with codes and outputs. You may also have a look at the following articles to learn more –