如何在 C 中使用布尔数据类型? [英] How to use boolean datatype in C?

查看:17
本文介绍了如何在 C 中使用布尔数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是用 C 编写代码,结果发现它没有布尔/布尔数据类型.是否有任何 C 库可以包含以使我能够返回 boolean/bool 数据类型?

I was just writing code in C and it turns out it doesn't have a boolean/bool datatype. Is there any C library which I can include to give me the ability to return a boolean/bool datatype?

推荐答案

如果你有支持 C99 的编译器,你可以

If you have a compiler that supports C99 you can

#include <stdbool.h>

否则,您可以根据需要定义自己的.根据您的使用方式(以及您是否希望能够将代码编译为 C++),您的实现可能很简单:

Otherwise, you can define your own if you'd like. Depending on how you want to use it (and whether you want to be able to compile your code as C++), your implementation could be as simple as:

#define bool int
#define true 1
#define false 0

不过,在我看来,您不妨只使用 int 并使用零表示假,使用非零表示真.这就是它通常在 C 中完成的方式.

In my opinion, though, you may as well just use int and use zero to mean false and nonzero to mean true. That's how it's usually done in C.

这篇关于如何在 C 中使用布尔数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆