常数乘法的奇怪值
Strange value of the constant multiplication
本文关键字:常数乘 更新时间:2023-10-16
#include<iostream>
using namespace std;
#define P d2 + 2
int main()
{
int d2 = 4;
cout << P * 2;
getchar();
return 0;
}
为什么这段代码返回 8 而不是 12?当我引用 P 时,它有 6 个值。
在编译器之前运行的 C(和 C++)预处理器在使用指令 #include
和 #define
时会执行严格的替换。换句话说,在预处理器运行后,编译器看到的只是
cout << d2 + 2 * 2;
你应该尝试
#define P (d2 + 2)
甚至最好完全避免宏。
你忘记了大括号。宏在代码中直接替换。所以你的陈述是:
cout << d2 + 2 * 2
这是d2 + 4
.
将宏编辑为
#define P (d2 + 2)
相关文章:
- 在 C++ 中的数组上使用阶乘函数
- 阶乘问题在 c++ 中给出错误的输出
- 加、乘、除、减复数的问题 C++ - 运算符重载
- 为什么 g++ 使用 movabs 和一个奇怪的常数来简单还原?
- 大数的阶乘给出错误的输出
- 什么模板用法在阶乘中更好
- 为什么我在C++阶乘函数中出现编译错误?
- 计算阶乘的 C++17 倍表达式中的错误
- 为什么 Lisp 中 1000 阶乘的计算如此之快(并显示正确的结果)?
- 在C++中执行 N 阶乘编译时间的 3 种不同/相同方法
- 平方模型为零,阶乘模型问题
- 阶乘元规划
- 如何制作每秒从 30 乘 1 倒计时的计时器?
- 阶乘-c++ 我想以不同的方式打印
- C++14 将元组扩展三乘三
- 为什么在C++负数static_cast如果<unsigned>数字是常数,则不同
- 阶乘函数只返回C++中输入的答案
- 表达式必须具有常数值,变量不能用作定义数组大小的常数
- 阶乘使用循环递增给出垃圾值
- 特征误差:INVALID_MATRIX_PRODUCT乘向量和矩阵