March 15th, 2011, 04:15 PM
What is the difference between the header files <header.h> and <header>?
I know that the <...> brackets indicate that the header file is located in some standard directory that the compiler can access, but why do some of these have the ".h", and some do not?
For example, I am using some mathematical functions from <cmath> in Visual Studio, which seem to be the same as with <math.h>.
March 15th, 2011, 04:22 PM
Re: Header files
In some cases, C header files like <math.h> have a different name in C++. So <cmath> is the C++ version of <math.h>, <cstdio> is the C++ name of C's <stdio.h>, etc. There may or may not be an actual difference in the content of the files, it's just good practice to select the right header for the language you're using.
In certain other cases, both standard and pre-standard versions of a header may be available. Some compilers still support <iostream.h>, a pre-standard header (which is still encouraged by a depressingly large number of tutorials), but the correct standard header is <iostream> and has been for years. Don't use pre-standard headers, as a rule.
There's at least one potentially confusing case. The C header <string.h> is available to C++ as <cstring>, but C++ *also* provides a <string> header, which contains the std::string class. So <cstring> and <string> are very different headers, but one of them is almost the same as <string.h>. Also note that the MFC class "CString" is not associated with any of these standard headers.
Last edited by Lindley; March 15th, 2011 at 05:18 PM.
March 15th, 2011, 04:26 PM
Re: Header files
It's a naming convention: C++ standard libraries header files don't have the .h extension and also the C standard libraries are named with c prefix and without extension (usually old names like math.h are still accessible).
Click Here to Expand Forum to Full Width