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

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

问题描述

我只是在写C code和事实证明它没有一个布尔/布尔数据类型。是否有任何C库,我可以包括给我回一个布尔值/ 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>

另外,你可以定义你自己,如果你愿意的话。根据您希望如何使用它(你是否希望能够编译code和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天全站免登陆