Skip to content
Tutorials Center
My learning

Topics

C Reference ยท Preprocessor

The #error and #warning directives

Reference for C #error and #warning: stop the build or print a message from the preprocessor, guard unsupported configurations and see C23 status.

#error makes the preprocessor print a message and fail the compilation. It is used inside conditional blocks to reject configurations that cannot work: an unsupported compiler, a missing required macro, two incompatible options. #warning prints a message but lets the build continue. It is used for deprecation notices and reminders.

#warning became standard in C23. Before that it was a widely supported extension (GCC and Clang accepted it for years).

C
#ifndef BUFFER_SIZE
#error "BUFFER_SIZE must be defined"
#endif

Try it

C Guards that never fire

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C A warning that does not stop the build

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
#error pp-tokens
#warning pp-tokens        /* C23; earlier a common extension */
Name Description
pp-tokens The rest of the line, printed as the diagnostic text. Quote it if it contains an apostrophe, which is otherwise read as an unterminated character constant by some compilers.

Errors: #error always fails the translation unit if it is not in a skipped group. Text inside a skipped #if branch is not processed, so the directive does nothing there.

Usage notes

  • Pair it with #if/#else: end a platform chain with #else followed by #error "unsupported platform".
  • Message text is not macro-expanded. #error VERSION is bad prints the word VERSION, not its value.
  • Prefer static_assert for conditions on types and sizeof (which the preprocessor cannot evaluate). Use #error only for preprocessor-level facts such as macros and <limits.h> values.
  • Warnings in builds with -Werror become errors, so a #warning can still break a strict CI.
  • A good message names the macro and the fix: #error "define HAVE_THREADS or set SINGLE_THREADED".
  • The compiler may stop at the first #error or continue to report further problems; do not depend on the exact output format.
  • Portability: use #warning in C23 mode freely. In older modes, -pedantic flags it as an extension.