# #error directive in C

#### Software Engineering C

Reading time: 20 minutes | Coding time: 5 minutes

#error is a preprocessor directive in C which is used to raise an error during compilation and terminate the process.

Syntax:

#error <error message>


Usually, the error message is associated with preprocessor conditional statements like #if, #ifdef and others.

It is used for various situations like terminating compilation process if there are conflicting requests and settings. It is widely used in the build process of various software.

## Simple example

In this C code example, we will raise an error is the value of a macro named power is less than 2. We need to check for two cases:

• POWER is not defined
• POWER is less than 2

Go through this code where we have defined POWER as 1. It will give a compilation error.

#include<stdio.h>

#define POWER 1

#if !defined POWER || POWER < 2
#error POWER is not defined or is less than 2
#endif

int main()
{
return 0;
}


Compilation error:

opengenus.c:6:2: error: #error POWER is not defined or is less than 2
#error POWER is not defined or is less than 2
^


If we do not define POWER macro, we will get the same compilation error as we are checking for it in the same condition. Consider this code:

#include<stdio.h>

#if !defined POWER || POWER < 2
#error POWER is not defined or is less than 2
#endif

int main()
{
return 0;
}


Compilation error:

opengenus.c:6:2: error: #error POWER is not defined or is less than 2
#error POWER is not defined or is less than 2
^


If we define the POWER macro and set it to 3, it will compile and execute successfully. Consider the following code:

#include<stdio.h>

#define POWER 3

#if !defined POWER || POWER < 2
#error POWER is not defined or is less than 2
#endif

int main()
{
return 0;
}


The above code compiles successfully.

## Example with math.h

In C, the header file math.h defines a macro named _MATH_H and defines several useful mathematical utilities as well. To use its utilities, we need to include it and hence, we can raise an error by checking if the macro is defined or not.

Consider this code:

#include<stdio.h>

#ifndef _MATH_H
#error You need to include math.h
#else

int main()
{
float a = sqrt(5);
printf("%f", a);
return 0;
}

#endif


It will raise a compilation error:

opengenus.c:4:2: error: #error You need to include math.h
#error You need to include math.h
^


If we include math.h, the error will be removed and the code will be compiled successfully. Check with this code:

#include<stdio.h>
#include<math.h>

#ifndef _MATH_H
#error You need to include math.h
#else

int main()
{
float a = sqrt(5);
printf("%f", a);
return 0;
}

#endif


Output:

2.236068


## Example with a real situation

Consider software system which will use a database system like Sqlite or MySQL. If the settings are such that it uses both or none, we should raise an error in the compilation process. To do this, the code will be like:

#if !(defined USING_SQLITE && defined USING_MYSQL)
#error You must use either sqlite or mysql
#endif

#if defined USING_SQLITE && defined USING_MYSQL
#error You cannot use both sqlite and mysql at the same time
#endif

#ifdef USING_SQLITE
// code using sqlite
#endif

#ifdef USING_MYSQL
// code using mysql
#endif


#### OpenGenus Foundation

The official account of OpenGenus IQ backed by GitHub, DigitalOcean and Discourse