#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.

CategoryPreprocessor directives
KindKeyword / Statement
C standardC89 / 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 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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏