📄 hfiles.html
字号:
<HTML>
<HEAD>
<TITLE>ANSI C++: Standard header files</TITLE>
<META NAME="description" CONTENT="New improvements on header files inclusion">
<META NAME="keywords" CONTENT="#include iostream.h iostream stdio cstdio">
</HEAD>
<body bgcolor="white" >
<!--captut-->
<CENTER>
<TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=1 BORDER=0>
<TR><TD WIDTH=90%>
<FONT SIZE=4> ANSI C++ </FONT><BR>
<FONT SIZE=5><B> Standard header files </B></FONT>
</TD><TD VALIGN="bottom"><a href="http://www.cplusplus.com">
<IMG SRC="/img/mini.gif" ALT="cplusplus.com" BORDER=0></A></TD></TR>
<TR><TD BGCOLOR="#0000FF" ALIGN="center" COLSPAN=2>
<IMG SRC="/img/head0.gif" WIDTH=2 HEIGHT=2 BORDER=0></TD></TR>
</TABLE>
</CENTER>
<!--/captut-->
<P>
In ANSI-C++ the way to include header files from the standard library has changed.
<P>
The standard specifies the following modification from the C way of including standard header files:
<UL>
<LI> Header file names no longer maintain the <TT><B>.h</B></TT> extension
typical of the C language and of pre-standard C++ compilers, as in the case of
<TT><B>stdio.h</B></TT>, <TT><B>stdlib.h</B></TT>, <TT><B>iostream.h</B></TT>, etc.
This extension <TT><B>h</B></TT> simply disappears and files previously known as
<TT><B>iostream.h</B></TT> become <TT><B>iostream</B></TT> (without <TT><B>.h</B></TT>).
<LI> Header files that come from the C language now have to be preceded by a
<TT><B>c</B></TT> character in order to distinguish them from the new C++ exclusive
header files that have the same name. For example <TT><B>stdio.h</B></TT>
becomes <TT><B>cstdio</B></TT> .
<LI> All classes and functions defined in standard libraries are under
the <TT><B>std</B></TT> <I>namespace</I> instead of being global. This not applies to
C macros that remain as C macros.
</UL>
<P>
Here you have a list of the standard C++ header files:
<BLOCKQUOTE><TT>
<algorithm>
<bitset>
<deque>
<exception>
<fstream>
<functional>
<iomanip>
<ios>
<iosfwd>
<iostream>
<istream>
<iterator>
<limits>
<list>
<locale>
<map>
<memory>
<new>
<numeric>
<ostream>
<queue>
<set>
<sstream>
<stack>
<stdexcept>
<streambuf>
<string>
<typeinfo>
<utility>
<valarray>
<vector>
</TT></BLOCKQUOTE>
And here is a list of the C header files included in ANSI-C++
with their new name and their equivalents in ANSI-C:
<BLOCKQUOTE><TABLE BORDER=1 WIDTH=300>
<TR><TD BGCOLOR="silver" WIDTH=50%><B>ANSI-C++</B></TD>
<TD BGCOLOR="silver" WIDTH=50%><B>ANSI-C</B></TD></TR>
<TR><TD><TT><cassert></TT></TD><TD><TT><assert.h></TT></TD></TR>
<TR><TD><TT><cctype></TT></TD><TD><TT><ctype.h></TT></TD></TR>
<TR><TD><TT><cerrno></TT></TD><TD><TT><errno.h></TT></TD></TR>
<TR><TD><TT><cfloat></TT></TD><TD><TT><float.h></TT></TD></TR>
<TR><TD><TT><ciso646></TT></TD><TD><TT><iso646.h></TT></TD></TR>
<TR><TD><TT><climits></TT></TD><TD><TT><limits.h></TT></TD></TR>
<TR><TD><TT><clocale></TT></TD><TD><TT><locale.h></TT></TD></TR>
<TR><TD><TT><cmath></TT></TD><TD><TT><math.h></TT></TD></TR>
<TR><TD><TT><csetjmp></TT></TD><TD><TT><setjmp.h></TT></TD></TR>
<TR><TD><TT><csignal></TT></TD><TD><TT><signal.h></TT></TD></TR>
<TR><TD><TT><cstdarg></TT></TD><TD><TT><stdarg.h></TT></TD></TR>
<TR><TD><TT><cstddef></TT></TD><TD><TT><stddef.h></TT></TD></TR>
<TR><TD><TT><cstdio></TT></TD><TD><TT><stdio.h></TT></TD></TR>
<TR><TD><TT><cstdlib></TT></TD><TD><TT><stdlib.h></TT></TD></TR>
<TR><TD><TT><cstring></TT></TD><TD><TT><string.h></TT></TD></TR>
<TR><TD><TT><ctime></TT></TD><TD><TT><time.h></TT></TD></TR>
<TR><TD><TT><cwchar></TT></TD><TD><TT><wchar.h></TT></TD></TR>
<TR><TD><TT><cwtype></TT></TD><TD><TT><wtype.h></TT></TD></TR>
</TABLE></BLOCKQUOTE>
<P>
Since now classes and functions of the standard libraries are located within
the <TT><B>std</B></TT> <I>namespace</I> we must use the C++ <TT><B>using</B></TT> directive
for that these become usable in the same way they were in pre-standard code. For example,
to be able to use all the standard classes of <TT><B>iostream</B></TT>
we would have to include something similar to this:
<blockquote ><TT>
#include <iostream><BR>
using namespace std;
</TT></BLOCKQUOTE>
that would be equivalent to the old expression
<blockquote ><TT>
#include <iostream.h>
</TT></BLOCKQUOTE>
previous to the standard.
<P>
Nevertheless for compatibility with ANSI-C, the use of <I>name</I><TT>.h</TT> way
to include C header files is allowed.
Therefore, both following examples are valid and equivalent in a compiler which fulfills
<TT><B>ANSI-C++</B></TT> specifications:
<P>
<TABLE WIDTH=100% CELLPADDING=5 CELLSPACING=5><TR>
<TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// ANSI C++ example</I>
#include <cstdio>
using namespace std;
int main ()
{
printf ("Hello World!");
return 0;
}
</PRE></TT>
</TD><TD BGCOLOR="#FFFFBF" WIDTH=50% VALIGN="top">
<TT><PRE><I>// pre ANSI C++ example
// also valid under ANSI C++, but deprecated</I>
#include <stdio.h>
int main ()
{
printf ("Hello World!");
return 0;
}
</PRE></TT></TD></TR></TABLE>
<P>
In all the examples of the current version of
<B><a href="../tutorial/index.html">The C++ tutorial</A></B>
it has been chosen to include the old way because to date is the most compatible format
(and also shorter), although if you have a compiler that supports ANSI-C++ standard I
recommended you the use of the new format in your programs.
<!--cua-->
<P>
<CENTER><TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 BORDER=0>
<TR><TD BGCOLOR="#0000FF"><IMG SRC="/img/2x2.gif" WIDTH=2 HEIGHT=2></TD></TR>
<TR><TD ALIGN="right"><FONT FACE="arial,helvetica" SIZE=1>©The C++ Resources Network, 2000 - All rights reserved</FONT></TD></TR>
<TR><TD ALIGN="center">
<TABLE CELLPADDING=0 CELLSPACING=0>
<TR><TD>Return <a href="javascript:history.go(-1)" >back</A></TD></TR>
<TR><TD>Return to <a href="/doc/">Documents section</A></TD></TR></TABLE>
</TD></TR>
</TABLE></CENTER>
<!--/cua-->
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -