Programming in C: Unit I (d): Preprocessor Directives

#Error Directive

with Example C Programs | Preprocessor Directives

The #error directive is used to produce compiler-time error messages. The syntax of this directive is #error string.The error messages include the argument string.

#ERROR DIRECTIVE

The #error directive is used to produce compiler-time error messages. The syntax of this directive is

#error string

The error messages include the argument string. The #error directive is usually used to detect programming inconsistencies and violation of constraints during preprocessing. When #error directive is encountered, the compilation process terminates and the message specified in string is printed to stderr. For example, consider the piece of codes given below which illustrates error processing during preprocessing:

#ifndef SQUARE

of #error MACRO not defined.

#endif

#ifndef VERSION

#error Version number is not specified.

#endif

#ifdef WINDOWS

/* Windows specific code */

#else

#error "This code works only on WINDOWS operating system"

#endif

The #error directive causes the preprocessor to report a fatal error.

Here the string need not be enclosed within double quotes. It is a good programming practice to enclose the string in double quotes. The #error directive is a very important directive mainly because of two reasons:

• First, it helps you to determine whether a given line is being compiled or not.

• Second, when used within a heavily parameterized body of code, it helps to ensure that a particular MACRO has been defined.

Besides the #error directive, there is another directive #warning, which causes the preprocessor to issue a warning and continue preprocessing. The #warning directive is same as that of #error.

#warning string

Here, string is the warning message that has to be displayed. One important place where #warning directive can be used is in obsolete header files. You can display a message directing the user to the header file which should be used instead.

Programming in C: Unit I (d): Preprocessor Directives : Tag: : with Example C Programs | Preprocessor Directives - #Error Directive