Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
C #error
2.1.
Syntax
3.
C #error Example
4.
FAQs 
4.1.
What is the use of #error in C?
4.2.
Why is the #define directive used?
4.3.
What are the types of errors in the C programming?
4.4.
When linker error occurs?
4.5.
What is the difference between run-time and compile-time error?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

C #error

Author Shivam Verma
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

The #error is a preprocessor directive. Let's understand what a preprocessor directive is? Preprocessor directives are lines in a program that begins with the symbol #, distinguishing them from standard source code text. The compiler utilizes them to process some programs before compiling them. Preprocessor directives alter the source code's content, resulting in a new source code without these directives.

Also see: C Static Function, and Tribonacci Series

C #error

The preprocessor directive #error denotes an error. If the #error directive is found, the compiler gives a fatal error and terminates the compilation process.

Syntax

The #error directive in C has the following syntax:

#error message

Preprocessor conditional statements like #if, #ifdef, and others are usually associated with the error message. It's used in various scenarios, such as terminating the compilation process if several requests and settings conflict. It's commonly used in software development.

You can also read about dynamic array in c and Short int in C Programming

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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  

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  

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 problemstop 100 SQL problemsresources, 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!

Previous article
C #else
Next article
C #Pragma
Live masterclass