C++实操 - True and false

本文详细解读了C11和C++中布尔类型true/false、bool以及_Bool的用法,包括它们在运算符中的行为和stdbool.h头文件的引入,展示了C++11标准下的关键字定义。
在C11标准文档中,规定了关系运算符 <、> 、<= 、>=的运算结果,真时返回1,假时返回0,返回类型为整型。
运算符==、!=和关系运算符类似,除了运算优先级较低以外,也是返回1或0。
真(True)的定义是非0,所以假(False)的定义就是整型的0值。
C语言本身只有一个_Bool定义,是一个关键字。
_Bool类型是一个对象,存储0和1两个值,是一个无符号的整型。
如下程序所示,_Bool只有0和1,即假和真两个值,赋值时非0都看作1。
任何一个标量值给_Bool类型变量赋值,如果等于0,赋值为0,否则就赋值为1。
#include <stdio.h>
int main()
{
  _Bool varA;
  varA = 2;
  printf("varA:%d.\n",varA);
  varA = -1;
  printf("varA:%d.\n",varA);
  varA = 0;
  printf("varA:%d.\n",varA);
  printf("Hello world!\n");
  return 0;
}

$ gcc -o tof tof.c
$ ./tof
varA:1.
varA:1.
varA:0.
Hello world!

为了更方便程序员对布尔类型的使用,C语言的标准库,头文件<stdbool.h>,定义了和布尔操作相关的类型。
stdbool.h
​
/* Copyright (C) 1998, 1999, 2000, 2009 Free Software Foundation, Inc.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<Licenses- GNU Project - Free Software Foundation>.  */
/*
* ISO C Standard:  7.16  Boolean type and values  <stdbool.h>
*/
#ifndef _STDBOOL_H
#define _STDBOOL_H
#ifndef __cplusplus
#define bool        _Bool
#define true        1
#define false        0
#else /* __cplusplus */
/* Supporting <stdbool.h> in C++ is a GCC extension.  */
#define _Bool        bool
#define bool        bool
#define false        false
#define true        true
#endif /* __cplusplus */
/* Signal that all the definitions are present.  */
#define __bool_true_false_are_defined        1
#endif        /* stdbool.h */
​

C里的头文件,stdbool.h,定义了bool类型,其实就是_Bool。
并定义了true为1,false为0,方便使用。
这几个宏按照上面的定义展开为类型_Bool以及常数1和0。
使用了stdbool.h的C程序:
#include <stdio.h>
#include <stdbool.h>
int main()
{
  bool varA;
  varA = 2;
  printf("varA:%d.\n",varA);
  varA = -1;
  printf("varA:%d.\n",varA);
  varA = 0;
  printf("varA:%d.\n",varA);
  varA = true;
  printf("varA:%d.\n",varA);
  varA = false;
  printf("varA:%d.\n",varA);
  printf("Hello world!\n");
  return 0;
}

$ gcc -o tof tof.c
$ ./tof
varA:1.
varA:1.
varA:0.
varA:1.
varA:0.
Hello world!
同时我们看到了stdbool.h里面还使用了__cplusplus这个C++编译器的宏开关,如果使用C++编译器来编译C程序时,就是用下面的宏定义。
这时定义了4个,bool、false、和true都原封不动,说明C++语言本身自带定义。而_Bool转换为bool,表明C++里没有_Bool,转而使用bool。
==================================== 
下面我们来看一下C++里面的true、false的定义:
查看C++11标准文档,C++里bool、true、false都是关键字。
true、false是字面常量,bool类型的变量值是true或者false。
如下程序所示:
#include <stdio.h>
int main()
{
  bool varA;
  printf("false:%d,true:%d.\n", false, true);
  varA = 2;
  printf("varA:%d.\n", varA);
  varA = -1;
  printf("varA:%d.\n", varA);
  varA = 0;
  printf("varA:%d.\n", varA);
  printf("Hello world!\n");
  return 0;
}

$ g++ -o tofplus tof.cpp
$ ./tofplus
false:0,true:1.
varA:1.
varA:1.
varA:0.
Hello world!

false是0,true是1。
bool类型变量的值只能是0或1。
注意:
1,关于大写的TRUE和FALSE定义,在C/C++语言和标准库里都没有定义,程序中使用的都是单独添加的。
2,本文使用的gcc版本:gcc version 9.3.0,Ubuntu虚拟机下编辑编译的示例代码。
<think>嗯,用户需要详细的教程指导在Ubuntu系统下用VSCode运行C++程序。从引用的资料来看,用户应该是个开发者,可能刚接触Linux环境或者VSCode配置,但熟悉基本命令行作。用户的需求很明确,但引用内容比较零散,需要整合成完整流程。 用户提供的四个引用涵盖了关键步骤:引用[1]讲环境安装,引用[2]涉及路径配置,引用[3][4]分别是示例代码和调试配置。不过用户特别说明这些引用仅供参考,看来更信任我能提供系统化的解决方案。 上次回复中已经给出完整教程框架,这次需要更注重细节性。特别要注意: 1. 环境安装部分要强调gcc/g++版本检查,避免用户环境差异导致问题 2. 配置task.json时解释${fileDirname}等变量的含义,减少用户困惑 3. launch.json的调试路径说明要区分编译前/后状态 4. 示例代码特意选标准输入输出,能验证I/O是否正常 用户可能遇到的坑是路径配置错误和调试符号缺失,所以注意事项里重点提了这两点。最后考虑设置三个延伸问题:编译选项优化、多文件编译、远程调试,这些都是际开发中的高频需求。 </think>以下是Ubuntu系统下使用VSCode运行C++程序的完整教程,整合关键配置步骤: ### 📦 环境准备 1. **安装编译工具链** 终端执行: ```bash sudo apt update sudo apt install build-essential gdb ``` 验证安装成功: ```bash g++ --version # 输出GCC版本号 gdb --version # 输出GDB版本号 ``` 2. **VSCode扩展安装** - 打开VSCode → 扩展市场 → 搜索安装: *C/C++* (微软官方扩展) *Code Runner* (可选,一键运行) ### ⚙️ 项目配置 1. **创建配置文件** 在项目根目录创建 `.vscode` 文件夹,内含: - `tasks.json` (编译配置) - `launch.json` (调试配置) 2. **配置编译任务** (tasks.json) ```json { "version": "2.0.0", "tasks": [{ "label": "build", "type": "shell", "command": "g++", "args": [ "-g", // 生成调试信息 "${file}", // 当前文件 "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "group": { "kind": "build", "isDefault": true } }] } ``` 3. **配置调试器** (launch.json) ```json { "version": "0.2.0", "configurations": [{ "name": "C++ Debug", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "preLaunchTask": "build" // 调试前自动编译 }] } ``` ### 🚀 运行与调试 1. **编译程序** `Ctrl+Shift+B` → 选择 `build` 任务,生成可执行文件 2. **调试程序** `F5` 启动调试,关键功能: - 断点设置:点击行号左侧 - 变量监控:调试侧边栏查看 - 单步执行:`F10`(跳过) / `F11`(进入) 3. **直接运行** (需Code Runner) 右键编辑器 → `Run Code` 或 按 `Ctrl+Alt+N` ### 💡 示例测试 创建 `test.cpp`: ```cpp #include <iostream> using namespace std; int main() { int a = 5, b = 7; cout << "Result: " << a * b << endl; return 0; } ``` 按 `F5` 调试运行,终端应输出: ```bash Result: 35 ``` ### ⚠️ 注意事项 1. 路径问题:若提示头文件缺失,在 `c_cpp_properties.json` 中添加路径: ```json "includePath": ["${workspaceFolder}/**"] ``` 2. 调试失败:确保编译时加了 `-g` 参数生成调试符号 3. 多文件编译:在 `tasks.json` 的 `args` 中添加所有 `.cpp` 文件: ```json "args": ["-g", "main.cpp", "utils.cpp", "-o", "program"] ``` > 通过以上配置,您将获得完整的编辑-编译-调试工作流。调试控制台支持输入交互,外部终端需设置 `"externalConsole": true`[^4]。 --- ### 🔍 相关问题 1. 如何配置VSCodeC++多文件项目的自动化编译? 2. 调试时出现 `"Unable to start debugging. Program path is missing."` 错误如何解决? 3. 如何在Ubuntu中为VSCode配置Clang编译器替代GCC? [^1]: 基础环境安装参考 [^2]: 路径配置方法 [^4]: 调试配置文件模板
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值