我想找到_Bool
我的系统上定义,因此对于系统中它的失踪,我可以实现它。 我已经看到了在这里和其他网站不同的定义,但要检查系统上的明确定义。
轻微的问题,我找不到在哪里_Bool定义,甚至stdbool.h
mussys@debmus:~$ find /usr/include/* -name stdbool.h
/usr/include/c++/4.3/tr1/stdbool.h
和grep
用于_Bool
在/usr/include/*
和/usr/include/*/*
不要么找到它。
那么,是什么呢?
_Bool
是一个内置的类型,所以不要指望找到它的定义在头文件,甚至是系统头文件。
说了这么多,猜测从您正在搜索的路径你的系统,你在看着/usr/lib/gcc/*/*/include
?
我的“真实” stdbool.h
住在这里。 正如预期的那样它#define
小号bool
是_Bool
。 作为_Bool
是一种原产于编译器有一个在头文件中没有定义它。
作为一个说明:
该_Bool在C99定义。 如果你建立你的程序有:
gcc -std=c99
你可以期望它在那里。
其他人都回答了关于这个问题的_Bool
位置,并发现如果C99声明...但是,我并不满足于自制声明人人都献出。
你为什么不完全定义的类型?
typedef enum { false, true } bool;
_Bool
是C99预定义类型,就像int
或double
。 你不会找到定义int
无论是在任何的头文件。
你可以做的是
- 检查编译器是C99
- 如果使用
_Bool
- 否则使用一些其他类型的(
int
或unsigned char
)
例如:
#if defined __STDC__ && defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
/* have a C99 compiler */
typedef _Bool boolean;
#else
/* do not have a C99 compiler */
typedef unsigned char boolean;
#endif
一些编译器不提供_Bool关键字,所以我写了我自己的stdbool.h:
#ifndef STDBOOL_H_
#define STDBOOL_H_
/**
* stdbool.h
* Author - Yaping Xin
* E-mail - xinyp at live dot com
* Date - February 10, 2014
* Copyright - You are free to use for any purpose except illegal acts
* Warrenty - None: don't blame me if it breaks something
*
* In ISO C99, stdbool.h is a standard header and _Bool is a keyword, but
* some compilers don't offer these yet. This header file is an
* implementation of the stdbool.h header file.
*
*/
#ifndef _Bool
typedef unsigned char _Bool;
#endif /* _Bool */
/**
* Define the Boolean macros only if they are not already defined.
*/
#ifndef __bool_true_false_are_defined
#define bool _Bool
#define false 0
#define true 1
#define __bool_true_false_are_defined 1
#endif /* __bool_true_false_are_defined */
#endif /* STDBOOL_H_ */
$ echo '_Bool a;' | gcc -c -x c -
$ echo $?
0
$ echo 'bool a;' | gcc -x c -c -
<stdin>:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘a’
这表明_Bool
是一个内置的类型, bool
是不是通过编译单个变量声明没有包含。