bool

提供: C言語入門
移動: 案内検索
スポンサーリンク

bool とは、stdbool.hで提供される「データ型」です。

読み方

bool
ぶーる

概要

boolは、ブーリアン型(Boolean datatype)で、真理値の2つの値をとります。

true
false

boolを使用する場合は、stdbool.hをインクルードします。boolに必要な以下のシンボルが定義されています。

  • bool
  • true
  • false

C99_Bool 型が定義されました。

  • _Boolを持つ環境では、bool_Boolとして扱われます。
  • _Boolを持たない環境では、boolは、intとして扱われます。

ヘッダファイル

FreeBSDでは、以下のヘッダファイルで boolが定義されています。

/usr/include/stdbool.h

以下のコードは、FreeBSDのstdbool.hの抜粋です。

#ifndef __bool_true_false_are_defined
#define __bool_true_false_are_defined   1
 
#ifndef __cplusplus
 
#define false   0
#define true    1
 
#define bool    _Bool
#if __STDC_VERSION__ < 199901L && __GNUC__ < 3 && !defined(__INTEL_COMPILER)
typedef int     _Bool;
#endif
 
#endif /* !__cplusplus */
#endif /* __bool_true_false_are_defined */

ソースコード

/*
 * bool.c
 * Copyright (C) 2014 kaoru <kaoru@bsd>
 */
#include <stdbool.h>
int
main(int argc, char const* argv[])
{
        bool b = false;
        (void) printf("%ld\n", sizeof(b));
        (void) printf("%ld\n", sizeof(int));
        return 0;
}

コンパイル

cc bool.c

実行例

64bitのFreeBSDでの実行例です。

% ./a.out
1
4

関連項目

  1. _Bool
  2. bool



スポンサーリンク