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).
#ifndef BUFFER_SIZE #error "BUFFER_SIZE must be defined" #endif
Try it
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.
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
#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#elsefollowed by#error "unsupported platform". - Message text is not macro-expanded.
#error VERSION is badprints the wordVERSION, not its value. - Prefer
static_assertfor conditions on types andsizeof(which the preprocessor cannot evaluate). Use#erroronly for preprocessor-level facts such as macros and<limits.h>values. - Warnings in builds with
-Werrorbecome errors, so a#warningcan 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
#erroror continue to report further problems; do not depend on the exact output format. - Portability: use
#warningin C23 mode freely. In older modes,-pedanticflags it as an extension.