tho*_*mas 0 c++ ascii infinite-loop while-loop
这是我的代码:
#include <iostream>
int main()
{
char x = 32;
while (x <= 126) {
std::cout << x << "\n";
x += 1;
}
}
Run Code Online (Sandbox Code Playgroud)
到这里为止,一切顺利,但如果我将代码更改为:
#include <iostream>
int main()
{
char x = 32;
while (x <= 127 /* here the "bad" change */ ) {
std::cout << x << "\n";
x += 1;
}
}
Run Code Online (Sandbox Code Playgroud)
为了尝试打印 [del] 字符,我的程序进入无限循环并开始打印许多我不想要的其他字符。为什么?
打开你的警告选项!(-Wextra对于海湾合作委员会)
test.cpp: In function 'int main()':
test.cpp:41:15: warning: comparison is always true due to limited range of data type [-Wtype-limits]
41 | while ( x <= 127 )
| ~~^~~~~~
Run Code Online (Sandbox Code Playgroud)
我想警告消息是不言自明的。