C #error Example
Here is an example of using the #error preprocessor directive.
#include<stdio.h>
#ifndef _MATH_H
#error First include math.h then compile
#else
int main(){
float n;
n=sqrt(8);
printf("%f\n",n);
return 0;
}
#endif

You can also try this code with Online C Compiler
Run Code
Output
prog.c:3:2: error: #error First include math.h then compile
#error First include math.h then compile
^~~~~
Here the above program gives a compilation error. If we include math.h, this error will be removed, and the code will be compiled successfully. Here is the updated code.
#include<stdio.h>
#include<math.h>
#ifndef _MATH_H
#error First include math.h then compile
#else
int main(){
float n;
n=sqrt(8);
printf("%f\n",n);
return 0;
}
#endif

You can also try this code with Online C Compiler
Run Code
Output
2.828427
You can check code with the help of an online compiler.
FAQs
What is the use of #error in C?
The #error directive is commonly used to prevent compilation if a known condition exists that would cause the program to fail if the compilation is completed.
Why is the #define directive used?
The #define directive specifies values or macros that the preprocessor will use to change the program source code before it is generated. Because preprocessor definitions are substituted before the compiler acts on the source code, any errors that are introduced by #define are challenging to trace.
What are the types of errors in the C programming?
There are five different types of errors in the C programming language:
- Syntax error
- Run-time error
- Logical error
- Semantic error
- Linker error
When linker error occurs?
Linker error occurs when the linker tries to put all the pieces of a program together to make an executable; one or more of the pieces are missing. This usually occurs when the linker cannot find an object file or library.
What is the difference between run-time and compile-time error?
The errors that belong to the semantics or syntax are known as compile-time errors. The errors that occur when the code is being executed during run-time are known as run-time errors.
Conclusion
In this article, we have extensively discussed the #error preprocessor directive.
We hope that this article has helped you enhance your knowledge regarding the #error preprocessor directive and if you would like to learn more, check out our article on Preprocessors in C.
Refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Refer to the links problems, top 100 SQL problems, resources, and mock tests to enhance your knowledge.
For placement preparations, visit interview experiences and interview bundle.
Do upvote our blog to help other ninjas grow. Happy Coding!