bool java_【原创】bool、BOOL 和 _Bool 辨析

最近在搞跨平台编译的时候又遇到了 C99 标准支持的问题,主要体现在布尔类型问题上面。于是乎决定把这个问题彻底搞搞清楚,遂成此文。

bool、BOOL 和 _Bool 的区别

bool 类型在 C++ 中以关键字的形式被支持,表示布尔类型,其对应变量的值只有真(true)和假(false)两种值。

BOOL 类型在头文件 中定义为 typedef int BOOL;在头文件 中定义为 typedef long BOOL;

BOOL 类型的长度视实际环境来定,一般可认为是 4 个字节。

BOOL 是微软定义的表达布尔逻辑的类型。与 C++ 中的 bool 类型不同是,它是一个三值逻辑:TRUE、FALSE 和 ERROR。当返回值为大于 0 的整数时为 TRUE,返回值为 0 时为 FALSE,返回值为 -1 时为 ERROR。

_Bool 是 C99 标准中定义的一个新关键字,以提供布尔类型。C2008 草案中只规定了 _Bool 类型的大小至少应能够存放 0 和 1 这两个值。而并没有规定具体的大小。这交给编译器自由发挥了。

【跨平台如何使用布尔类型】

C++ 里有专门的 bool 关键字。但是在 C99 之前,C 语言里没有这样的类型。从 C99 标准开始,增加了关键字 _Bool 用来表示布尔类型。所以只要你的编译器支持 C99,你就可以直接使用布尔型了(当然,VC,VS系列编译器均不支持 C99)。除此之外,C99 为了在 C 中兼容 C++ 里对布尔类型的定义,又增加了一个头文件 stdbool.h。并在其中定义了 bool、true 和 false,让我们可以像 C++ 一样的定义和使用布尔类型。

使用布尔类型的几种方式:

a. 自己定义的“仿布尔类型”

在 C99 标准被支持之前,我们常常自己模仿定义布尔类型,方式有很多种,常见的有下面两种:

/* 第一种方法 */

typedef int BOOL;

#define TRUE 1

#define FALSE 0

/* 第二种方法 */

enum bool{false, true};

b. 使用 C99 新增的关键字 _Bool

C99 新增关键字 _Bool 类型的长度为 1,只能取值为 0 或 1 。将任意非零值赋值给 _Bool 类型变量,都会先转换为 1,表示为真。将零值赋值给 _Bool 类型,结果为 0,表示为假。

c. 使用 C99 新增头文件 stdbool.h

在 C++ 中,通过 bool 来定义布尔变量,通过 true 和 false 对布尔变量进行赋值。C99 为了让我们能够写出与 C++ 兼容的代码,添加了头文件 。所以我们只要包含了该头文件,就可以像 C++ 中使用布尔变量的方式进行操作。

在我自己的 linux 系统中查找 stdbool.h 头文件,找到两处:

1. 系统定义

[root@Betty ~]# vi /usr/lib/syslinux/com32/include/stdbool.h

/*

* $Id: stdbool.h,v 1.1 2003/04/16 06:32:31 hpa Exp $

*

* stdbool.h

*/

#ifndef _STDBOOL_H

#define _STDBOOL_H

#ifndef __cplusplus

#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L)

# if !defined(__GNUC__) ||(__GNUC__ < 3)

typedef char _Bool; /* For C compilers without _Bool */

# endif

#endif

#define bool _Bool

#define true 1

#define false 0

#else

/* C++ */

#define bool bool

#define true true

#define false false

#endif

#define __bool_true_false_are_defined 1

#endif /* _STDBOOL_H */

2. GCC 定义

[root@Betty ~]# vi /usr/lib/gcc/x86_64-redhat-linux/4.1.1/include/stdbool.h

/* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify

it under the terms of the GNU General Public License as published by

the Free Software Foundation; either version 2, or (at your option)

any later version.

GCC is distributed in the hope that it will be useful,

but WITHOUT ANY WARRANTY; without even the implied warranty of

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

GNU General Public License for more details.

You should have received a copy of the GNU General Public License

along with GCC; see the file COPYING. If not, write to

the Free Software Foundation, 51 Franklin Street, Fifth Floor,

Boston, MA 02110-1301, USA. */

/* As a special exception, if you include this header file into source

files compiled by GCC, this header file does not by itself cause

the resulting executable to be covered by the GNU General Public

License. This exception does not however invalidate any other

reasons why the executable file might be covered by the GNU General

Public License. */

/*

* ISO C Standard: 7.16 Boolean type and values

*/

#ifndef _STDBOOL_H

#define _STDBOOL_H

#ifndef __cplusplus

#define bool _Bool

#define true 1

#define false 0

#else /* __cplusplus */

/* Supporting 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 */

其实这两个头文件的定义本身还隐形的说明了一些信息,阅者自斟。

参考文章:

1.《

浅谈C语言中的布尔(bool)类型

2.《

C语言的布尔类型(_Bool)

3.《

C/C++ 中的bool类型

》(这个文章有料)

转载至链接:https://my.oschina.net/moooofly/blog/180380

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值