Error Directive

The error directive is used to specify an error message for a specific situation. In the following program, the error message is displayed if USD and UKP are not defined.

Example

#include <stdio.h>

#if !defined (USD) || !defined (UKP)     // B
#error "ERROR: NO_CURRENCY rate is specified." //C
#endif

int main()
{
    printf("erro directives") // program never reaches here
    int rs; // error message will be flasehd from statement C 
    getchar();
    return 0;
}

Explanation

  1. Statement B checks whether UKP or USD is defined.
  2. If both are not defined then the preprocessor displays an error.

Points to Remember

  1. The #error directive allows us to specify an error message.
  2. The error message is generated by the preprocessor.