c - 为什么此代码在 Dev-C++ 中有效,但在 Visual C++ 中无效?

标签 c dev-c++

#include <stdio.h>

int main() {

int f,c;
int lower,upper,step;

lower = 0;
upper = 300;
step = 20;


while (f <= upper) {

    c = 5 * (f - 32) / 9;
    printf("%d\t%d\n",f,c);
    f = f + step;
}

return 0;

}

我忘了写f = lower,但它也能运行并得到正确答案。我无法发布图片。我认为这是错误的,因为它不能与 Visual C++ 一起运行,但为什么呢?

最佳答案

当您使用时:

int lower,upper,step;

标准不保证变量被初始化为0,但一些编译器可以。如果我没记错的话,Visual Studio 在调试版本中将函数局部变量初始化为 0,但在发布版本中没有。

线

while (f <= upper) {

不能保证以可预测的方式运行,因为 f 未在您的程序中显式初始化。

关于c - 为什么此代码在 Dev-C++ 中有效,但在 Visual C++ 中无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27286144/

相关文章:

c - 自动堆栈使用分析器

c - gprof : function 'etext' is taking 100. 05% 的运行时间

c - 整个一个核心专用于单个进程

c++ - 在终端窗口上显示大输出

c - dev-c++ 套接字错误

c - devc++程序执行失败

c - 第二次使用 sqlite3_prepare_v2 时 SQLite 崩溃

c++ - 在 C++ 中的宏中禁用逗号

c - 分配给具有数组类型的表达式并请求非结构或 union 中的成员

c++ - 使用 MinGW (Dev-C++) 编译 64 位二进制文​​件