繁体   English   中英

为什么VC ++ 2010 Express在此程序中不需要stdio.h但gcc ++却需要stdio.h?

[英]Why doesn't VC++ 2010 Express require stdio.h in this program but gcc++ does?

我有以下代码

#include <iostream>

using namespace std;

void WaitForEnter()
{
    while(1)
    {
        if('\n' == getchar()) 
        {
            break;
        }
    }
    return;
}

int main()
{
    cout<< "Press Enter to Exit... ";
    WaitForEnter();
}

这将在Microsoft Visual C ++ 2010 Express上进行编译,并达到了我的预期。 在使用code :: blocks和gcc ++ 4.7的Ubuntu上,构建失败,并出现以下error: 'getchar' was not declared in this scope. 如果我添加#include "stdio.h" ,程序将以预期的行为编译并运行。 为什么该程序在不带stdio.h情况下使用MVC ++ 2010 Express进行编译,而在Ubuntu上使用gcc ++ 4.7而不对code :: blocks进行编译。

使用MSVC时,包含<stdio.h>作为包含<iostream>副作用。 查看预处理后的输出,或遵循MSVC文件中的#include路径。

最简单的答案是,该标准允许任何标准标头包含任何其他标头。 另一方面,如果要编写可移植的代码,则不应依赖于此,而应包括翻译单元所需的所有标头。

在Visual Studio中,当您创建一个新项目时,它会为您提供stdafx.h。 在此文件中,它包括:

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM