在VC++4.1以前的版本中,使用的库称为运行库(run-time library),头文件名都是“*.h”。从VC++4.2版本开始使用标准C++库(standard C++ library),标准C++库是符合ANSI标准的,它使你的程序可以在不同的编译系统和平台间移植。新的头文件名不再有“.h”扩展名,不过标准C++库仍然保留了18个带有“.h”扩展名的C头文件。 在程序中,既可以选择使用旧版本的头文件(".h"形式的头文件),也可以使用新的标准C++库头文件(无扩展文件名的头文件)。在连接时,编译系统会根据头文件名自动确定连接哪一个库。下面是c:/.../VC98/Include/CSTDIO文件中的内容:// cstdio standard header
#if _MSC_VER > 1000#pragma once#endif
#ifndef _CSTDIO_#define _CSTDIO_#ifdef _STD_USING #undef _STD_USING #include <stdio.h> #define _STD_USING#else #include <stdio.h>#endif /* _STD_USING */#endif /* _CSTDIO_ */
/* * Copyright (c) 1994 by P.J. Plauger. ALL RIGHTS RESERVED. * Consult your license regarding permissions and restrictions. */从上面了解到,<cstdio>包含了<stdio.h>。请看例子1:#include<stdio.h>#include<math.h>void main(){ float i=89.0000; float d=sin(i); printf("%f/n%f/n",i,d);}例子1的程序是正确的。例子2:#include<cstdio>#include<math.h>void main(){ float i=89.0000; float d=sin(i); printf("%f/n%f/n",i,d);}例子2的程序也是正确的。我们可以在程序中同时使用c++标准库文件和c语言风格的头文件。例子3:#include<iostream>#include<math.h>void main(){ float i=89.0000; float d=sin(i); printf("%f/n%f/n",i,d);}例子3的程序也是正确的。<iostream>兼容了c语言中的<stdio.h>。例子4:#include<stdio.h>#include<cmath>using namespace std;void main(){ float i=89.0000; float d=sin(i); printf("%f/n%f/n",i,d);}例子4的程序运行时会出现错误“ 'std' : does not exist or is not a namespace。”,因此在包含c语言头文件时,其后不能使用命名空间std。例子5:#include<iostream>#include<cmath>using namespace std;void main(){ float i=89.0000; float d=sin(i); printf("%f/n%f/n",i,d);}例子4的程序是正确的。使用c++库时,在紧接着所以的include指令之后,应该加入"using namespace std;"这一条语句来将指定的命名空间中的名称引入到当前命名空间中。 当我们自己写了一个c++头文件并要使用它是时,我们应该把它放在我们所建的工程文件夹的根目录下,然后在c++源文件中#include"自己写的头文件名.h"。如果不写后缀.h,程序编译时就会出错。