c++ - 为什么 int a= 4i;不报告语法错误(c++)

标签 c++

enter image description here

#include <iostream>
using namespace std;

int main(int argc, const char * argv[]) {
    int a = 232u;
    int b = 4i;
    cout << a << endl << b;
}

我正在复习 cpp 的基础知识,正如我拍摄的屏幕截图所示,我尝试将一个 unsigned int 签名为一个很好的 int,然后我尝试将它更改为 i 并等待错误,但是没有错误并且输出为 0。没有 i 的定义。那么发生了什么。

我在 mac 上使用 xcode,最后一张图片是build设置。

最佳答案

C++有一个字面量的概念,用来描述一个值的类型。例如,integer literal可以用来写 I want a integer 1类型 unsigned int .我们会写的1u .

在您的情况下,您可能正在使用 GNU extension for imaginary constants .你写的东西不能用 C++ 标准编译。

在 C++ 标准中使用复杂文字的好方法是包含 include <complex> .并使用 std::complex_literals ,这仅在 C++14 中可行。

关于c++ - 为什么 int a= 4i;不报告语法错误(c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44608230/

相关文章:

c++ - 使用迭代器将部分文件流读入字符串

c++ - 将 'typedef' 从基类传播到 'template' 的派生类

c++ - 线段相交,数值稳定测试

c++ - 如何将字符串作为参数插入到 QStringListModel 中

c# - 检测什么进程/程序启动我的进程/程序

c++ - HTTP_POST 请求中的 ESP8266 JSON 参数

c++ - 从函数调用返回 Mat 时出现奇怪的结果

c++ - ASIO io_service 在第二次 run() 调用时不处理后处理程序

c++ - 从 QGraphicsItem Qt C++ 获取实际对象

c++ - 视频录制在 IMFSinkWriter->Finalize() 上挂起;