C中是否允许布尔返回类型?

Viv*_*vek 13 c boolean

当我尝试bool在GCC编译器中编译具有返回类型的函数时,编译器会抛出此错误.

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘comp’
Run Code Online (Sandbox Code Playgroud)

但是当我将返回类型更改为时int,它已成功编译.

功能如下.

bool comp(struct node *n1,struct node *n2)
{
    if(n1 == NULL || n2 == NULL)
    return false;
    while(n1 != NULL && n2 != NULL)
    {
        if(n1->data == n2->data)
        { n1=n1->link; n2=n2->link; }
        else
            return false;

    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

我在这里比较两个链表.C中是否支持bool返回类型?

Oli*_*rth 23

bool 在C99之前不存在关键字.

在C99中,它应该可以工作,但正如@pmg在下面指出的那样,它仍然不是关键字.这是一个声明的宏<stdbool.h>.

  • +1`bool`是C99关键字的typedef(在`<stdbool.h>`中)和预定义类型`_Bool`. (5认同)

use*_*681 8

尝试包括:

#include <stdbool.h>
Run Code Online (Sandbox Code Playgroud)


小智 5

#include<stdio.h>
#include<stdbool.h>
void main(){
    bool x = true;
    if(x)
        printf("Boolean works in 'C'. \n");
    else
        printf("Boolean doesn't work in 'C'. \n");
}
Run Code Online (Sandbox Code Playgroud)