【发布时间】:2021-04-28 17:51:28
【问题描述】:
为什么下面的代码输出为 '0'?
#include <bits/stdc++.h>
int main() {
int max = 5;
std::cout << (false) ? "impossible" : std::to_string(max);
}
【问题讨论】:
-
因为
false打印为0。该语句等效于(std::cout << false) ? "impossible" : std::to_string(max);。你可能想要std::cout << (false ? "impossible" : std::to_string(max));
标签: c++ c++11 conditional-operator