_Static_assert C · 关键字
C11 引入的编译期断言,如果条件不成立,编译器会报错并显示信息。
怎么用
_Static_assert(条件, "信息");
小例子
#include <stdio.h>
int main(void) {
_Static_assert(sizeof(char) == 1, "char 必须是 1 字节");
return 0;
}
编译结果
编译通过
提醒:用来在编译期检查类型大小、常量等条件;失败时直接编译错误
官方文档:https://en.cppreference.com/w/c/language/_Static_assert
在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← C全部词条