<assert.h>

Diagnostics

定义宏:

void assert (scalar-expression);

若expression为0,则打印出错信息(类似Assertion failed: expression, function abc, file xyz, line nnn.),调用abort函数中断程序执行。

若定义了NDEBUG,则assert宏无效: #define assert(ignore) ((void)0)

static_assert ( constant-expression , string-literal ) ;

编译期断言。可扩展成_Static_assert

// gcc -std=c11 test_assert.c
// gcc -std=c11 test_assert.c -DNDEBUG

#include <assert.h>

void check(int* p) {
  assert(p != 0);
}

int main() {
  int a = 0;
  int* p1 = &a, * p2 = 0;
  check(p1);
  check(p2);
  _Static_assert(0 != 0, "_Static_assert");
  static_assert(0 != 0, "static_assert expands to _Static_assert");

  return 0;
}
原文地址:https://www.cnblogs.com/chenkkkabc/p/3359333.html