定义bool型变量出现 ”identifier “bool” is undefined”错误

bool表示布尔型变量,也就是逻辑型变量。

bool类似于其他的基本数据类型,他和char字符型变量一样,有1个字节。

但是bool变量和char变量不一样的地方是,同样是一个字节的变量,bool只有ture和false这两个值。其中true为1,false为0。

而由于bool变量在C语言中,并不属于基本数据类型,所以在定义bool变量的时候,往往还需要添加一个头文件,也就是#include “stdbool.h”,该头文件主要内容如下所示。

/* stdbool.h: ISO/IEC 9899:1999 (C99), section 7.16 */

/* Copyright (C) ARM Ltd., 2002

 * All rights reserved
 * RCS $Revision: 177844 $
 * Checkin $Date: 2012-11-21 11:51:12 +0000 (Wed, 21 Nov 2012) $
 * Revising $Author: drodgman $
   */

#ifndef __bool_true_false_are_defined
#define __bool_true_false_are_defined 1
#define __ARMCLIB_VERSION 5060002

  #ifndef __cplusplus /* In C++, 'bool', 'true' and 'false' and keywords */
    #define bool _Bool
    #define true 1
    #define false 0
  #else
    #ifdef __GNUC__
      /* GNU C++ supports direct inclusion of stdbool.h to provide C99
         compatibility by defining _Bool */
      #define _Bool bool
    #endif
  #endif

#endif /* __bool_true_false_are_defined */

当然如果没有该头文件,则可以定义bool结构体来实现bool变量的定义:

typedef enum
{
  FALSE = 0,
  TRUE = !FALSE
}
bool;

正如程序所写,该结构体中只有两个变量,分别用来表示TRUE和FALSE。

当然如果这还觉得麻烦,可以直接将bool定义成char,也就是如下所示:

#define bool char

因为如上文所说,bool和char都是1字节的变量,而bool只需要用到0或者1,将其定义为char完全可以胜任,并且也不会有内存的浪费。

如果不重新定义bool变量的话,往往会在编译过程中出现“ identifier “bool” is undefined”的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值