首页 > 编程知识 正文

布尔型用法,C语言bool实例

时间:2023-05-06 16:31:41 阅读:10895 作者:479

C语言是当今世界上最流行、最广泛使用的高级编程语言之一,具有高效、可移植性好等优点,尤其是在嵌入式领域应用广泛。 以下编辑详细介绍了bool在c语言中的使用方法。 希望对您有帮助。

在c语言中bool的使用方法如下。

C99以前没有定义bool类型,C99在stdbool.h中定义了_Bool为bool,当然也没有定义true和false。 但是,c有bool型,也定义了true和false。

所以如果是C99以前的标准,就需要自己定义bool。 例如

typedef enum {false=0,true=1} bool;

/*版权(c ) 1998、1999、2000自由软件基础,Inc。

This file is part of GCC。

gcc is自由软件; youcanredistributeitand/or modify

itunderthetermsofthegnugeneralpublicenseaspublishedby

自由软件基础; 以太网版本2,or(atyouroption ) )。

Any Later版本。

gcisdistributedinthehopethatitwillbeuseful,

but WITHOUT ANY WARRANTY; withouteventheimpliedwarrantyof

merchantabilityorfitnessforaparticularpurpose.see the

gnugeneralpubliclicenseformoredetails。

youshouldhavereceivedacopyofthegnugeneralpubliclicense

阿龙威国际机场; 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,ifyouincludethisheaderfileintosource

files compiled by GCC,thisheaderfiledoesnotbyitselfcause

theresultingexecutabletobecoveredbythegnugeneralpublic

license.thisexceptiondoesnothoweverinvalidateanyother

reasonswhytheexecutablefilemightbecoveredbythegnugeneral

公共许可证. * /

/*

* ISOC标准:7.16 booleantypeandvalues

*/

#ifndef _STDBOOL_H

#define _STDBOOL_H

#ifndef __cplusplus

#define bool _Bool

#定义真1

#define false 0

#else /* __cplusplus */

/* supportingincisagccextension.* /

#define _Bool bool

#定义布尔布尔

#定义假假

#定义真

#endif /* __cplusplus */

/* signalthatallthedefinitionsarepresent.* /

# define _ _ bool _ true _ false _ are _ defined 1

#endif /* stdbool.h */

1.FALSE/TRUE和false/true的区别:

false/true是用标准c语言添加的新关键字,false/true通过#define。 这需要用途

解决了程序在c和c中的环境差异,以下是windef.h中对FALSE/TRUE的定义:

#ifndef FALSE

#define FALSE 0

#endif

#ifndef TRUE

#定义真1

#endif

也就是说,FALSE/TRUE是int类型,FALSE/TRUE是bool类型。 所以两者不同,只是

我们在使用中没有这种感觉。 因为c会给我们做隐式转换。

2.BOOL的大小和bool的区别:

因为bool在c中占用1个字节,但bool是int类型,int类型的大小因环境而异:

false/true仅占用一个字节,但TRUE/FALSE在windef .h上的BOOL定义取决于环境: 类型在球;

3 .空值和0之间的差异:

或者,看看windef.h中的空值定义:

#ifndef NULL

#ifdef __cplusplus//这指示用c编译程序

#定义空值0

#else

#定义空值((void * )0) ) )。

#endif

#endif

所以,它们没有区别。 但是,在c中进行强制的类型转换。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。