涡轮C ++:为什么printf的打印预期值,当没有变量传递给它? [英] Turbo C++: Why does printf print expected values, when no variables are passed to it?

查看:114
本文介绍了涡轮C ++:为什么printf的打印预期值,当没有变量传递给它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个问题是问一个选择题测试:什么将是下列程序的输出:

A question was asked in a multiple choice test: What will be the output of the following program:

#include <stdio.h>

int main(void)
{
    int a = 10, b = 5, c = 2;

    printf("%d %d %d\n");

    return 0;
}

和的选择是10个不同排列,5和2。出于某种原因,它工作在Turbo C ++,这是我们在大学使用。但是,它在用gcc编译时(这使在启用-Wall警告)或铛(这使-Wformat,默认情况下给出警告),或在Visual C ++。输出是,正如所料,垃圾值。我的猜测是,它牵扯到的事实,无论是涡轮C ++为16位和32位Windows XP上运行,或者说,TCC是可怕的,当涉及到标准。

and the choices were various permutations of 10, 5, and 2. For some reason, it works in Turbo C++, which we use in college. However, it doesn't when compiled with gcc (which gives a warning when -Wall is enabled) or clang (which has -Wformat enabled and gives a warning by default) or in Visual C++. The output is, as expected, garbage values. My guess is that it has something to do with the fact that either Turbo C++ is 16-bit, and running on 32-bit Windows XP, or that TCC is terrible when it comes to standards.

推荐答案

在code的未定义行为

在涡轮C ++中,它只是恰巧,这三个变量住在堆栈中的准确位置在哪里丢失的的printf()参数会。这会导致不确定的行为由印有正确的价值观表现出来。

In Turbo C++, it just so happens that the three variables live at the exact positions on the stack where the missing printf() argument would be. This results in the undefined behaviour manifesting itself by having the "correct" values printed.

但是,你不能合理靠这个是事实。甚至构建环境丝毫的改变(例如不同的编译器选项)可以打破东西任意讨厌的方式。

However, you can't reasonably rely on this to be the case. Even the slightest change to your build environment (e.g. different compiler options) could break things in an arbitrarily nasty way.

这篇关于涡轮C ++:为什么printf的打印预期值,当没有变量传递给它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆