😸
[C] assert 関数
機能
実行時の条件判定に失敗した場合にプログラムを異常終了させるためのマクロ[1]
使用例
assert(1 == 1); と記述し、条件を満たす場合
\n は改行を表す エスケープシーケンス[2]
コード
#include <assert.h>
assert(1 == 1);
全文
#include <stdio.h>
#include <assert.h>
void main() {
assert(1 == 1);
printf("assert通過\n");
}
実行結果
assert通過
assert(x != 0); で変数の条件を確認する例
コード
#include <assert.h>
int x = 0;
assert(x != 0);
全文
#include <stdio.h>
#include <assert.h>
void main() {
int x = 0;
assert(x != 0);
printf("xはゼロではない\n");
}
実行結果
Assertion failed: x != 0, file (ファイル名), line (行番号)
Discussion