#error Force a diagnostic during preprocessing
#error Force a diagnostic during preprocessing
#error forces a diagnostic and stops translation, often with #if for invalid configurations. Compiler wording varies; it cannot validate runtime input.
| Category | Preprocessor directives |
|---|---|
| Kind | Keyword / Statement |
| C standard | C89 / C90 |
📝 Syntax
#error diagnostic-text
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
Expected diagnostic (exact wording is compiler-dependent):
error: Configuration required
❌ error Intentional error: this example demonstrates #error. Compilation fails when the directive is processed, no runnable program is produced, and the puts statement never produces runtime output.
ℹ️ Info The output block illustrates an expected diagnostic, not text that every compiler must reproduce verbatim. Real diagnostics may include a file name, line number, and different prefix.
🏷️ Related preprocessor directives
📚 Related Tutorials
❓ FAQ
QWhy does the #error example fail to compile?
AThe failure is intentional. CONFIG_READY is 0, so the condition is true and the preprocessor executes #error. Correct the configuration condition before expecting compilation to continue.
QHow should #error be used in a real project?
AUse it to enforce mandatory configuration that can be checked during preprocessing, with diagnostic text that helps the user fix the problem. Do not use it as a substitute for runtime input validation or ordinary error handling.
QDoes diagnostic text after #error need quotation marks?
ANo. #error is followed by a sequence of preprocessing tokens, usually a plain explanation that helps the user fix the configuration; the compiler determines the diagnostic format. If you write a string literal, its quotes may appear in the diagnostic. Use conditional directives so it triggers only when the configuration is invalid.