C Program to demonstrate usage of #error directive

The #error directive is one of the C preprocessor directives whose purpose is to stop the compilation process when a violation of constraint is found.

This program demonstrate the use of #error directive in Turbo C++ that is running on a DOS Box 0.74 system running on a Windows 7 64-bit PC.

Problem Definition

In this program, we are trying to write a program for finding q square-root of a number. To find square root we need a function called sqrt() which is part of Math.h header file.

The header file is not included in the programming which is an error. The #error directive will find this error and throw a user-defined message before terminating the compilation process.

Program Code – #error

#include <stdio.h>
#include <conio.h>

#ifndef _MATH_H
#error First include header file then compile
#else

main()
{
float a,b = 25;
a = sqrt(b);
printf("%f\n",a);
return 0;
}
#endif

Output – #error directive

The output from the program is a compilation error – #error directive in turbo C++ compiler.

First include header file then compile
Advertisements