诊断指令

来自cppreference.com

显示给定的错误消息并使得程序非良构,或给定的警告消息而不影响程序的合法性 (C23 起)

语法

#error 诊断消息 (1)
#warning 诊断消息 (2) (C23 起)

解释

1) 实现在遇到 #error 指令后,显示消息 诊断消息 ,并令程序非良构(停止编译)。
2)(1) ,除了不影响程序的合法性并且编译继续。

诊断消息 可由多个词组成,不必在引号中。

注解

在其于 C23 的标准化前, #warning 已经被许多编译器作为遵从的扩展提供。

示例

#if __STDC__ != 1
#  error "Not a standard compliant compiler"
#endif
 
#if __STDC_VERSION__ >= 202302L
#  warning "Using #warning as a standard feature"
#endif
 
#include <stdio.h>
int main (void)
{
    printf("The compiler used conforms to the ISO C Standard !!");
}

可能的输出:

The compiler used conforms to the ISO C Standard !!

引用

  • C17 标准(ISO/IEC 9899:2018):
  • 6.10.5 Error directive (第 126-127 页)
  • C11 标准(ISO/IEC 9899:2011):
  • 6.10.5 Error directive (第 174 页)
  • C99 标准(ISO/IEC 9899:1999):
  • 6.10.5 Error directive (第 159 页)
  • C89/C90 标准(ISO/IEC 9899:1990):
  • 3.8.5 Error directive

参阅