C言語 bool型 定義と使い方【初心者向け】
C言語では、通常、bool型はstdbool.hヘッダーファイルをインクルードして定義されます。bool型は真(true)または偽(false)の値を表すことができます。
bool型の定義例:
#include <stdbool.h>
bool flag = true;
bool型を使用した例:
#include <stdio.h>
#include <stdbool.h>
int main() {
bool flag = true;
if (flag) {
printf("The flag is true.\n");
} else {
printf("The flag is false.\n");
}
return 0;
}
C言語では、trueは1、falseは0と定義されています。そのため、bool型の変数にtrueやfalseを直接代入したり、条件文でbool型の変数を使って判断することができます。