在 c 语言中使用布尔值
今天的教程演示了如何使用 bool、enum、typedef 和 #define 在 c 编程语言中使用布尔值。
c 中的布尔值
bool(布尔数据类型)在 c99 和更新版本中原生可用; 我们必须包含 stdbool.h 库才能使用此数据类型。 在 c99 中,本机数据类型称为 _bool
。
另一方面,bool 是在 stdbool.h 中定义的标准库宏。 _bool
数据类型的对象包含 1 或 0,而 true 和 false 是来自 stdbool.h 库的宏。
这意味着 c 预处理器会将 #if true
解释为 #if 1
,除非 stdbool.h 包含在 c 程序中。 同时,c 预处理器必须将 true 本机识别为语言文字。
在 c 中使用 bool 类型作为布尔值
示例代码:
// library to use `bool`
#include
// library to use `printf()`
#include
// main function
int main(){
bool variablex = true;
if(variablex){
printf("the value of 'variablex' is true");
}
else{
printf("the value of 'variablex' is false");
}
return 0;
}
输出:
the value of 'variablex' is true
如果我们有 c99 或更新版本,我们可以使用 bool,_bool 的别名。 我们必须包含一个名为 stdbool.h 的库(也称为头文件)以使用 bool; 否则,我们会得到一个错误。
请参阅上面给出的程序作为使用 bool 类型的实际示例。
在 c 中对布尔值使用枚举
示例代码:
// library to use `printf()`
#include
// enum's declaration
typedef enum { false, true } boolean;
// main function
int main(){
boolean variablex = false;
if(variablex){
printf("the value of 'variablex' is true");
}
else{
printf("the value of 'variablex' is false");
}
return 0;
}
输出:
the value of 'variablex' is false
上面给出的代码片段是为那些使用 c89/90 或不想使用原生 bool 类型的人提供的ag捕鱼王app官网的解决方案。 我们可以使用枚举函数来创建 bool 类型。
枚举(也称为 enumeration)是 c 编程中的一种用户定义数据类型,用于为整数常量分配名称; 为什么? 这些名称使程序易于阅读、理解和维护。
我们通过为布尔类型创建一个新名称来使用布尔值。 为此,我们使用 typedef 关键字,该关键字用于为类型赋予新名称(请参阅本节的示例代码,其中我们为 bool 类型提供新名称,即布尔值)。
一旦我们执行上面给出的代码,一个枚举将被创建为 bool 类型。 进一步,将枚举的元素设置为假和真。
第一和第二个位置将被 false 占用以保持 0 和 true 以保持 1。
在 c 中对布尔值使用 typedef 和 #define
我们可以通过以下两种方式使用布尔值作为第二种方法的替代方法,我们将枚举用于布尔值。
示例代码 1:
// library to use `printf()`
#include
// declare a variable of `int` type
typedef int boolean;
// declare enum
enum { false, true };
// main function
int main(){
boolean variablex = true;
if(variablex){
printf("the value of 'variablex' is true");
}
else{
printf("the value of 'variablex' is false");
}
return 0;
}
示例代码 2:
// library to use `printf()`
#include
// declare a variable of `int` type
typedef int boolean;
//define macros
#define true 1;
#define false 0;
// main function
int main(){
boolean variablex = true;
if(variablex){
printf("the value of 'variablex' is true");
}
else{
printf("the value of 'variablex' is false");
}
return 0;
}
这两个代码示例都产生了下面给出的相同结果。
输出:
the value of 'variablex' is true
在示例代码 2 中,我们使用了 #define
(也称为宏指令),它是用于在 c 程序中定义宏的预处理器指令。
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在c中将整数转换为字符
发布时间:2024/01/03 浏览次数:131 分类:c语言
-
本教程介绍了在c中将整数转换为字符的不同方法。在c编程语言中,将整数转换为字符在各种情况下都很重要。在c中,字符是以ascii值表示的,因此转换过程相对简单。
发布时间:2023/05/07 浏览次数:364 分类:c语言
-
本文介绍了如何在 c 语言中使用 typedef enum。使用 enum 在 c 语言中定义命名整数常量 enum 关键字定义了一种叫做枚举的特殊类型。
发布时间:2023/05/07 浏览次数:129 分类:c语言
-
本文演示了如何在 c 语言中使用前缀增量与后缀增量运算符。c 语言中 i 和 i 记号的主要区别
发布时间:2023/05/07 浏览次数:275 分类:c语言
-
本文演示了如何在 c 语言中获取当前工作目录。使用 getcwd 函数获取当前工作目录的方法
发布时间:2023/05/07 浏览次数:177 分类:c语言
-
本文介绍了如何在 c 语言中使用位掩码。使用 struct 关键字在 c 语言中定义位掩码数据
发布时间:2023/05/07 浏览次数:212 分类:c语言
-
本文演示了如何在 c 语言中使用标准库排序函数。使用 qsort 函数对 c 语言中的整数数组进行排序
c 语言中的 extern 关键字
发布时间:2023/05/07 浏览次数:131 分类:c语言
-
本文介绍了如何在 c 语言中使用 extern 关键字。c 语言中使用 extern 关键字来声明一个在其他文件中定义的变量