6

我正在尝试创建一个返回类型为布尔值的函数......程序的语法似乎是正确的,但编译器给出了错误......

我包含的头文件是:

#include<stdio.h>
#include<stdlib.h>

我创建的功能是:

34.bool checknull(struct node* node){
35.    if ( node != NULL )
36.        return TRUE;
37.       
38.    return false;
39.}

我在编译时得到的是

bininsertion.c:34:1: error: unknown type name ‘bool’
bininsertion.c: In function ‘checknull’:
bininsertion.c:36:10: error: ‘TRUE’ undeclared (first use in this function)
bininsertion.c:36:10: note: each undeclared identifier is reported only once for each  function it appears in
bininsertion.c:38:9: error: ‘false’ undeclared (first use in this function)

我用小写和大写字母都试过“真,假”,但似乎没有用......

4

3 回答 3

21

<stdbool.h>如果需要,您应该包括bool,truefalse。也是true,不是TRUE


如果您不想包含stdbool.h,可以使用稍微丑陋的_Bool.

于 2013-03-03T08:29:43.557 回答
-1

原始答案

尝试包括 cstdio 和 cstdlib。可能没有任何区别,但我的编译器也注意到了这些奇怪的错误。曾经有效的东西,不再有效

编辑

在 C 中,false 由 0 表示,而 true 由任何非零值表示。

bool本质上,您可以像这样滚动自己的数据类型

typedef enum {false, true} bool;

然后你可以像在你的应用程序中一样使用它。

您也可以只包括stdbool.h应该与枚举建议类似的内容

于 2013-03-03T08:32:23.727 回答
-1

bool 不是数据类型..
它在 Visual Studio 中工作正常..因为它是 Microsoft 特定的东西..
只需包含stdbool.h它就可以正常工作:)

于 2013-03-03T10:59:09.107 回答