C言語にもboolがあった。
最近cflowの便利さに気づいたtanakaです。
% cflow cat.c | grep ':$' | sed 's/<.*//' main() usage() simple_cat() cat() write_pending() next_line_num()
コールグラフが一目瞭然でっす。超便利。
便利過ぎなので関数のみ抽出するシェルスクリプト作った。
fflow() { cflow $@ | grep ':$' | sed 's/ <.*//' }
やばい。死ねる。
boolって何よ?
で、cflowのテストにGNU catのソースを使ってみたら、思わぬ拾い物。
bool ok = true;
boolにtrue。なんだコレ?
マクロの正体は、
#include <stdbool.h>
調べてみたら、標準Cライブラリの実装 stdbool.hあった。どうやらC99からの新機能らしい。
#include <stdio.h> #include <stdbool.h> int main(void) { bool b = true; printf("%d\n", b); // 1 }
うっは、C言語にboolなんて!!
GCCのソースでも覗いてみる
Fedora17, GCC4.7.2のstdbool.hは、/usr/include/ではなく、/usr/lib/gcc/i686-redhat-linux/4.7.2/include/stdbool.hにあった。
#ifndef _STDBOOL_H #define _STDBOOL_H #ifndef __cplusplus #define bool _Bool #define true 1 #define false 0 #else /* __cplusplus */ /* Supporting <stdbool.h> in C++ is a GCC extension. */ #define _Bool bool #define bool bool #define false false #define true true #endif /* __cplusplus */ /* Signal that all the definitions are present. */ #define __bool_true_false_are_defined 1 #endif /* stdbool.h */
- _Bool型をbool型として再定義している。
- C++の場合は、_Bool型を定義。
- 型_Boolとして宣言されたオブジェクトは, 値0及び1を格納するのに十分な大きさをもつ。
- An object declared as type _Bool is large enough to store the values 0 and 1.
- スゲーきっちり和訳されてるな。流石です。
- ISO/IEC 9899:TC2より。
- 2012/10/30時点の最新版はISO/IEC 9899:2011/Cor. 1:2012。
- 他の型でも処理系によって解釈が異なるので、メモリの処理をするときは、sizeofすべき。
- 便利なので使ってこう。
まとめ
規格を読め。
更新履歴
- 2012/10/30
- GCCのソース調べてみた。
- _Boolの型について調べてみた。
- Todo:まともなサンプルを書くかな。