#navi(../)
* bool型の宣言 [#e6bb9d02]
以下にC言語でbool型を使用するための記事を以下に記します。

#contents
#htmlinsertpcsp(c-top.html,c-sp.html)

* 関連記事 [#o0f6aa58]
-[[bool型を使用する>宣言/bool型を使用する]]
-[[charの配列とcharのポインタの違い>宣言/charの配列とcharのポインタの違い]]
-[[enumのサンプルコード>宣言/enumのサンプルコード]]


* bool型を使用できるようにする [#d2369769]

bool型を利用するためには、&color(red){stdbool.h};を利用する。~
以下、UbuntuおよびCentOSにて動作することを確認。

以下のようなCソースでbool型を宣言するとエラーとなる。~

 #include <stdio.h>
 
 int main(void)
 {
   bool t = true;
   bool f = false;
   return 0;
 }

gccにてコンパイルすると以下のメッセージが表示される。
 $ gcc bool.c 
 bool.c: In function ‘main’:
 bool.c:5: error: ‘bool’ undeclared (first use in this function)
 bool.c:5: error: (Each undeclared identifier is reported only once
 bool.c:5: error: for each function it appears in.)
 bool.c:5: error: expected ‘;’ before ‘t’
 bool.c:6: error: expected ‘;’ before ‘f’

* 解決方法 [#ie17b0a0]
以下のように&color(red){#include <stdbool.h>};を記述すればコンパイルできる。
 #include <stdio.h>
 #include <stdbool.h>
 
 int main(void)
 {
   bool t = true;
   bool f = false;
   return 0;
 }

#htmlinsertpcsp(c-btm.html,c-sp.html)

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS