C 语言的 _Bool 类型

C99 新增了 _Bool 类型,用于表示布尔值,即逻辑值 true 和 false。

_Bool 类型也是一种整数类型。

原则上 _Bool 类型只占用一位存储空间。

C语言将非 0 的数当为 true,0 当为 false。

表示真或假的变量称为布尔变量 (Boolean variable)

_Bool 是 C 语言中布尔变量的类型名.

_Bool 类型的变量只能储存 1 或 0.

如果把其他非零数值赋值给布尔变量, 则该变量会被设置为 1. 这体现了 C 把所有非零值都视为真.

程序示例:

#include<stdio.h>
int main(void)
{
	_Bool a = 10;
	printf("%d\n", a);
	return 0;
}

结果:

1

代码示例:

#include<stdio.h>
int main(void)
{
	int beep = 11;
	if (beep)
	{
		printf("1\n");
	}
	else
	{
		printf("2\n");
	}


	return 0;
}

结果:

1

代码示例:

#include<stdio.h>
int main(void)
{
	int beep = 0;
	if (beep)
	{
		printf("1\n");
	}
	else
	{
		printf("2\n");
	}


	return 0;
}

结果:

2

C99 also provides for a stdbool.h header file. This header file makes bool an alias for Bool and defines true and false as symbolic constants for the values 1 and 0. Including this header file allows you to write code that is compatible with C++, which defines bool , true , and false as keywords.

The relational operators are themselves organized into two different precedences.
Higher precedence group: < , <= , > , >=
Lower precedence group: == , !=

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值