2012-05-21 30 views
0

我有以下代码为什么VC++ 2010 Express在这个程序中不需要stdio.h但是gcC++呢?

#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.如果我添加行​​,程序将编译并以预期行为运行。为什么此程序使用MVC++ 2010 Express进行编译,而不使用stdio.h,但不适用于Ubuntu上使用gcC++ 4.7的code :: blocks。

+0

您是否使用预编译头文件(即'stdafx.h')? –

+0

@Jesse好我不熟悉预编译头文件,只是做了一个快速搜索,是的,我正在使用它们,我应该关闭它们吗? – newToProgramming

+0

我不认为visual studio会让你编译,除非你包括stdafx.h – Rhexis

回答

4

随着MSVC,<stdio.h>被包括作为包括<iostream>的副作用。查看预处理的输出,或者按照MSVC文件中的#include路径。

+2

-1:'iostream'不包含'stdio.h'。 –

+0

@EitanT:你检查过了吗?我看到了这条道路。 iostream - > istream - > ostream - > ios - > xlocnum - > cstdio - > stdio.h。 – janm

+2

这是正确的答案。 'stdafx.h'不是必需的(这可以通过直接运行'cl.exe'来轻松验证)。 – jamesdlin

0

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

#include "targetver.h" 

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

我为什么被低估? – Rhexis

2

最简单的答案是,该标准允许任何标准头包含任何其他头。另一方面,如果你想编写可移植的代码,你不应该依赖这个,并且应该包含你的翻译单元所需的所有头文件。

相关问题