现在的位置: 首页 > 综合 > 正文

C语言不支持布尔型数据类型 C语言的布尔类型(_Bool)       

2013年10月30日 ⁄ 综合 ⁄ 共 10276字 ⁄ 字号 评论关闭

C语言里有没有布尔型这个数据类型

但我们由于使用习惯要这样使用的话,可以

#define ture 0
#define false 1

没有布尔类型的原因  在c里任何类型都可以当作bool来用,只要该值不为0就表示true,为0则表示false

ANSI C99标准规定C语言中有布尔类型(_Bool)。
为了方便,使用布尔类型可以加入头文件stdbool.h,该文件定义了bool=_Bool,true=1,false=0。
使用布尔类型必须保证所使用的编译器支持C99标准
没有的!在C语言中用0代替假值,非0代表真值.上面这种定义法只是定义常量,为了方便那些习惯了使用true,false的程序员

C语言的布尔类型
 
  在C语言标准(C89)没有定义布尔类型,所以C语言判断真假时以0为假,非0为真。所以我们通常使用逻辑变量的做法:
 
  //定义一个int类型变量,当变量值为0时表示false,值为1时表示true
 
  int flag;
 
  flag = 0;
 
  //……
 
  flag = 1;
 
  if( flag )
 
  {
 
  //……
 
  }
 
  但这种做法不直观,而且没有明确flag一定是布尔值。所以我们又借助C语言的宏定义:
 
  //宏定义布尔类型
 
  #define BOOL int
 
  #define TRUE 1
 
  #define FALSE 0
 
  //定义一个布尔变量
 
  BOOL flag = FALSE;
 
  这种方法虽然直观,但依然是换汤不换药,变量flag在编译器看来依然是int类型。
 
  新版本总会改进一些不好的地方,所以在最新的C语言标准(C99)解决了布尔类型的问题。C99提供了_Bool 型,所以布尔类型可以声明为 _Bool flag.
 
  _Bool依然仍是整数类型,但与一般整型不同的是,_Bool变量只能赋值为0或1,非0的值都会被存储为1.
 
  C99还提供了一个头文件 定义了bool代表_Bool,true代表1,false代表0.只要导入 stdbool.h ,就能非常方便的操作布尔类型了。
 
  //导入 stdbool.h 来使用布尔类型
 
  #include

  #include

  //计算n!,n的值在main中定义
 
  int main(void)
 
  {
 
  int n = 10; //计算叠乘数
 
  int sum = 1; //用来存放叠乘的结果
 
  bool flag = false; //叠乘标记
 
  int num = n; //循环次数
 
  while( !flag )
 
  {
 
  sum = sum * (num--);
 
  //当num=1时结束循环
 
  if( num == 1)
 
  {
 
  flag = true;
 
  }
 
  }
 
  printf ("%d的叠乘值为 %d \n", n, sum);
 
  return 0;
 
  }

原文出自【比特网】,转载请保留原文链接:http://soft.chinabyte.com/database/234/12514234.shtml

 

C语言的布尔类型(_Bool)       

分类:           
C
7155人阅读评论(1)收藏举报
Technorati 标签: ,,,,

        也许很多人都和我一样,不知道现在的C语言已经有了布尔型:从C99标准开始,类型名字为“_Bool”。

        在此之前的C语言中,使用整型int来表示真假。在输入时:使用非零值表示真;零值表示假。在输出时:真的结果是1,假的结果是0;(这里我所说的“输入”,意思是:当在一个需要布尔值的地方,也就是其它类型转化为布尔类型时,比如 if 条件判断中的的条件;“输出”的意思是:程序的逻辑表达式返回的结果,也就是布尔类型转化为其他类型时,比如 a==b的返回结果,只有0和1两种可能)。

        所以,现在只要你的编译器支持C99(我使用的是Dev C++4.9.9.2),你就可以直接使用布尔型了。另外,C99为了让CC++兼容,增加了一个头文件stdbool.h。里面定义了booltruefalse,让我们可以像C++一样的定义布尔类型。

1. 我们自己定义的“仿布尔型”

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

  1. /* 第一种方法 */ 
  2. #define TRUE 1 
  3. #define FALSE 0 
  4.  
  5.  
  6. /* 第二种方法 */  
  7. enum bool{false,
    true};  
/* 第一种方法 */
#define TRUE 1
#define FALSE 0


/* 第二种方法 */ 
enum bool{false, true}; 

2. 使用_Bool

        现在,我们可以简单的使用 _Bool 来定义布尔型变量。_Bool类型长度为1,只能取值范围为0或1。将任意非零值赋值给_Bool类型,都会先转换为1,表示。将零值赋值给_Bool类型,结果为0,表示。 下面是一个例子程序。

  1. #include <stdio.h>  
  2. #include <stdlib.h>   
  3.  
  4. int main(){ 
  5.     _Bool a = 1; 
  6.     _Bool b = 2;    /* 使用非零值,b的值为1 */  
  7.     _Bool c = 0; 
  8.     _Bool d = -1;   /* 使用非零值,d的值为1 */  
  9.      
  10.     printf("a==%d,  /n", a); 
  11.     printf("b==%d,  /n", b); 
  12.     printf("c==%d,  /n", c); 
  13.     printf("d==%d,  /n", d); 
  14.  
  15.     printf("sizeof(_Bool) == %d  /n",
    sizeof(_Bool)); 
  16.      
  17.     system("pause"); 
  18.     return EXIT_SUCCESS; 
#include <stdio.h> 
#include <stdlib.h>  

int main(){
    _Bool a = 1;
    _Bool b = 2;    /* 使用非零值,b的值为1 */ 
    _Bool c = 0;
    _Bool d = -1;   /* 使用非零值,d的值为1 */ 
    
    printf("a==%d,  /n", a);
    printf("b==%d,  /n", b);
    printf("c==%d,  /n", c);
    printf("d==%d,  /n", d);

    printf("sizeof(_Bool) == %d  /n", sizeof(_Bool));
    
    system("pause");
    return EXIT_SUCCESS;
}

运行结果如下:(只有0和1两种取值)

  1. a==1, 
  2. b==1, 
  3. c==0, 
  4. d==1, 
  5. sizeof(_Bool) == 1 
a==1,
b==1,
c==0,
d==1,
sizeof(_Bool) == 1

3. 使用stdbool.h

        在C++中,通过bool来定义布尔变量,通过truefalse对布尔变量进行赋值。C99为了让我们能够写出与C++兼容的代码,添加了一个头文件<stdbool.h>。在gcc中,这个头文件的源码如下:(注,为了清楚,不重要的注释部分已经省略)

  1. /* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
  2.         This file is part of GCC.
  3. */ 
  4.  
  5. #ifndef _STDBOOL_H 
  6. #define _STDBOOL_H 
  7.  
  8. #ifndef __cplusplus 
  9.  
  10. #define bool    _Bool 
  11. #define true    1 
  12. #define false   0 
  13.  
  14. #else /* __cplusplus ,应用于C++里,这里不用处理它*/ 
  15.  
  16. /* Supporting <stdbool.h> in C++ is a GCC extension.  */ 
  17. #define _Bool   bool 
  18. #define bool    bool 
  19. #define false   false 
  20. #define true    true 
  21.  
  22. #endif /* __cplusplus */ 
  23.  
  24. /* Signal that all the definitions are present.  */ 
  25. #define __bool_true_false_are_defined   1 
  26.  
  27. #endif  /* stdbool.h */ 
/* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
        This file is part of GCC.
 */

#ifndef _STDBOOL_H
#define _STDBOOL_H

#ifndef __cplusplus

#define bool	_Bool
#define true	1
#define false	0

#else /* __cplusplus ,应用于C++里,这里不用处理它*/

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

         可见,stdbool.h中定义了4个宏,bool、true、false、__bool_true_false_are_defined。 其中bool就是 _Bool类型,true和false的值为1和0,__bool_true_false_are_defined的值为1。

下面是一个例子程序

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <stdbool.h> 
  4.  
  5. /* 测试C99新添加的头文件 stdbool.h */ 
  6.  
  7. int main(){ 
  8.     bool m = true
  9.     bool n = false
  10.     printf("m==%d,  n==%d  /n", m, n); 
  11.      
  12.     printf("sizeof(_Bool) == %d  /n",
    sizeof(_Bool)); 
  13.  
  14.     system("pause"); 
  15.     return EXIT_SUCCESS; 
#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h>

/* 测试C99新添加的头文件 stdbool.h */

int main(){
    bool m = true;
    bool n = false;
    printf("m==%d,  n==%d  /n", m, n);
    
    printf("sizeof(_Bool) == %d  /n", sizeof(_Bool));

    system("pause");
    return EXIT_SUCCESS;
}

执行结果为:

  1. m==1,  n==0 
  2. sizeof(_Bool) == 1 
m==1,  n==0
sizeof(_Bool) == 1

   

本文链接:http://blog.csdn.net/daheiantian/archive/2011/02/05/6241893.aspx 

 

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

注:本文仅针对符合C99标准的编译器,比如:gcc3.4.5。VC6等编译器不在本文讨论范围内。

  我们知道在C++里有专门的bool类型,用来表示真或假。但是在C语言里没有这样的类型(至少我是一直这么认为的),表达式的值0为假,非0为真。所以条件判断语句( if(...)、while(...) )非常灵活,甚至一个指针类型都可以是条件表达式。

  为了使程序更清晰,我们常常会给出如下的宏定义:

typedef int BOOL;
#define TRUE 1
#define FALSE 0

这是最常见的写法,能被任何C语言编译器认可。

  今天我在一段程序里看见这么一行 #include <stdbool.h>,这个陌生的头文件还以std打头而且还和bool有关,引起了我的警觉,立刻百度之。得知这是C99标准新增的头文件,意在引入bool类型,使之和C++兼容。随后我看了一下这个头文件的源代码,内容如下:

/*
* ISO C Standard: 7.16 Boolean type and values <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这个类型产生的兴趣。又一翻百度,发现这是C99标准引入的新的关键字。对,是关键字,而不是宏定义,也不是typedef。通过sizeof(_Bool);得知这个类型占1个字节,而且无论给这个类型的变量赋任何非0整数值,其值都是1,这也说明了他不是其他整数类型的别名。

  好了,其实说白了,C语言不是没有布尔类型,只能说在C99标准之前没有。现在只要在源文件中包含stdbool.h这个头文件,就可以在C语言里像C++那样使用bool类型了。

-------------------------------------------------------------
  原创文章,转载请注明出处。
  百度空间:http://hi.baidu.com/roovent
  电子邮箱:yangq.nj#gmail.com
-------------------------------------------------------------

C语言的布尔类型

  在C语言标准(C89)没有定义布尔类型,所以C语言判断真假时以0为假,非0为真。所以我们通常使用逻辑变量的做法:

复制代码
//定义一个int类型变量,当变量值为0时表示false,值为1时表示true
int flag;
flag = 0;
//......
flag = 1;

if( flag )
{
//......
}
复制代码

  

  但这种做法不直观,而且没有明确flag一定是布尔值。所以我们又借助C语言的宏定义:

复制代码
//宏定义布尔类型
#define BOOL int
#define TRUE 1
#define FALSE 0

//定义一个布尔变量
BOOL flag = FALSE;
复制代码

  这种方法虽然直观,但依然是换汤不换药,变量flag在编译器看来依然是int类型。

  新版本总会改进一些不好的地方,所以在最新的C语言标准(C99)解决了布尔类型的问题。C99提供了_Bool 型,所以布尔类型可以声明为 _Bool flag。

  _Bool依然仍是整数类型,但与一般整型不同的是,_Bool变量只能赋值为0或1,非0的值都会被存储为1。

  C99还提供了一个头文件 <stdbool.h> 定义了bool代表_Bool,true代表1,false代表0。只要导入 stdbool.h ,就能非常方便的操作布尔类型了。

复制代码
//导入 stdbool.h 来使用布尔类型
#include <stdbool.h>
#include <stdio.h>

//计算n!,n的值在main中定义
int main(void)
{
    int n = 10;    //计算叠乘数
    int sum = 1; //用来存放叠乘的结果
    bool flag = false;    //叠乘标记
    
    int num = n;    //循环次数
    while( !flag )
    {
        sum = sum * (num--);
        //当num=1时结束循环
        if( num == 1)
        {
            flag = true;
        }
    }
    printf ("%d的叠乘值为 %d \n", n, sum);
    return 0;
}
复制代码

 

Technorati 标签: ,,,,

        也许很多人都和我一样,不知道现在的C语言已经有了布尔型:从C99标准开始,类型名字为“_Bool”。

        在此之前的C语言中,使用整型int来表示真假。在输入时:使用非零值表示真;零值表示假。在输出时:真的结果是1,假的结果是0;(这里我所说的“输入”,意思是:当在一个需要布尔值的地方,也就是其它类型转化为布尔类型时,比如 if 条件判断中的的条件;“输出”的意思是:程序的逻辑表达式返回的结果,也就是布尔类型转化为其他类型时,比如 a==b的返回结果,只有0和1两种可能)。

        所以,现在只要你的编译器支持C99(我使用的是Dev C++4.9.9.2),你就可以直接使用布尔型了。另外,C99为了让CC++兼容,增加了一个头文件stdbool.h。里面定义了booltruefalse,让我们可以像C++一样的定义布尔类型。

1. 我们自己定义的“仿布尔型”

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

  1. /* 第一种方法 */ 
  2. #define TRUE 1 
  3. #define FALSE 0 
  4.  
  5.  
  6. /* 第二种方法 */  
  7. enum bool{false,
    true};  
/* 第一种方法 */
#define TRUE 1
#define FALSE 0


/* 第二种方法 */ 
enum bool{false, true}; 

2. 使用_Bool

        现在,我们可以简单的使用 _Bool 来定义布尔型变量。_Bool类型长度为1,只能取值范围为0或1。将任意非零值赋值给_Bool类型,都会先转换为1,表示。将零值赋值给_Bool类型,结果为0,表示。 下面是一个例子程序。

  1. #include <stdio.h>  
  2. #include <stdlib.h>   
  3.  
  4. int main(){ 
  5.     _Bool a = 1; 
  6.     _Bool b = 2;    /* 使用非零值,b的值为1 */  
  7.     _Bool c = 0; 
  8.     _Bool d = -1;   /* 使用非零值,d的值为1 */  
  9.      
  10.     printf("a==%d,  /n", a); 
  11.     printf("b==%d,  /n", b); 
  12.     printf("c==%d,  /n", c); 
  13.     printf("d==%d,  /n", d); 
  14.  
  15.     printf("sizeof(_Bool) == %d  /n",
    sizeof(_Bool)); 
  16.      
  17.     system("pause"); 
  18.     return EXIT_SUCCESS; 
#include <stdio.h> 
#include <stdlib.h>  

int main(){
    _Bool a = 1;
    _Bool b = 2;    /* 使用非零值,b的值为1 */ 
    _Bool c = 0;
    _Bool d = -1;   /* 使用非零值,d的值为1 */ 
    
    printf("a==%d,  /n", a);
    printf("b==%d,  /n", b);
    printf("c==%d,  /n", c);
    printf("d==%d,  /n", d);

    printf("sizeof(_Bool) == %d  /n", sizeof(_Bool));
    
    system("pause");
    return EXIT_SUCCESS;
}

运行结果如下:(只有0和1两种取值)

  1. a==1, 
  2. b==1, 
  3. c==0, 
  4. d==1, 
  5. sizeof(_Bool) == 1 
a==1,
b==1,
c==0,
d==1,
sizeof(_Bool) == 1

3. 使用stdbool.h

        在C++中,通过bool来定义布尔变量,通过truefalse对布尔变量进行赋值。C99为了让我们能够写出与C++兼容的代码,添加了一个头文件<stdbool.h>。在gcc中,这个头文件的源码如下:(注,为了清楚,不重要的注释部分已经省略)

  1. /* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
  2.         This file is part of GCC.
  3. */ 
  4.  
  5. #ifndef _STDBOOL_H 
  6. #define _STDBOOL_H 
  7.  
  8. #ifndef __cplusplus 
  9.  
  10. #define bool    _Bool 
  11. #define true    1 
  12. #define false   0 
  13.  
  14. #else /* __cplusplus ,应用于C++里,这里不用处理它*/ 
  15.  
  16. /* Supporting <stdbool.h> in C++ is a GCC extension.  */ 
  17. #define _Bool   bool 
  18. #define bool    bool 
  19. #define false   false 
  20. #define true    true 
  21.  
  22. #endif /* __cplusplus */ 
  23.  
  24. /* Signal that all the definitions are present.  */ 
  25. #define __bool_true_false_are_defined   1 
  26.  
  27. #endif  /* stdbool.h */ 
/* Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
        This file is part of GCC.
 */

#ifndef _STDBOOL_H
#define _STDBOOL_H

#ifndef __cplusplus

#define bool	_Bool
#define true	1
#define false	0

#else /* __cplusplus ,应用于C++里,这里不用处理它*/

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

         可见,stdbool.h中定义了4个宏,bool、true、false、__bool_true_false_are_defined。 其中bool就是 _Bool类型,true和false的值为1和0,__bool_true_false_are_defined的值为1。

下面是一个例子程序

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <stdbool.h> 
  4.  
  5. /* 测试C99新添加的头文件 stdbool.h */ 
  6.  
  7. int main(){ 
  8.     bool m = true
  9.     bool n = false
  10.     printf("m==%d,  n==%d  /n", m, n); 
  11.      
  12.     printf("sizeof(_Bool) == %d  /n",
    sizeof(_Bool)); 
  13.  
  14.     system("pause"); 
  15.     return EXIT_SUCCESS; 
#include <stdio.h> 
#include <stdlib.h> 
#include <stdbool.h>

/* 测试C99新添加的头文件 stdbool.h */

int main(){
    bool m = true;
    bool n = false;
    printf("m==%d,  n==%d  /n", m, n);
    
    printf("sizeof(_Bool) == %d  /n", sizeof(_Bool));

    system("pause");
    return EXIT_SUCCESS;
}

执行结果为:

  1. m==1,  n==0 
  2. sizeof(_Bool) == 1 
m==1,  n==0
sizeof(_Bool) == 1

   

本文链接:http://blog.csdn.net/daheiantian/archive/2011/02/05/6241893.aspx 

抱歉!评论已关闭.