A note about headers c vs c++

I know a lot of beginners can become overwhelmed with the vast amount of information out there about how to code C++. One thing that always gets me burning is when I see headers declared <xxxx.h> in C++ files and older C-Style syntax such as 'printf' in C++ example files and tutorials.
Even some on this website...
I realize the C++ is fully compatible with all ANSI C-Style coding but it's simply bad practice if there is already a way inside C++ to do the same functions.

It should be noted and this information is not easy to find and many are led to believe that two header files are very different when in fact they are essentially exactly the same...
Below is an example of just the 'time' header from the STL
<time.h> - Header for C programs
<ctime> - Header for C++ programs

What is the difference?
(NOTE this answer applies to most if not all STL headers for C++)

Well in C programs by the ANSI standards there is no 'namespace' keyword or entity. Thus when importing headers into your programs any traditional C-Style .h headers will go into the global name space i.e. ::Function

When you use the C++ style header, most if not all include those functions/members/etc... into the 'standard' std namespace.

In small programs this might not make a difference but as your programs become larger and you import more classes, namespaces into your applications it can cause problems. As a good practice, you should avoid any C-Style <xxxx.h> files in your programs, instead use the c++ version <xxxx> which will inherently import the <xxxx.h> file and place it into the appropriate namespace.

Hope this helps clear up some header file confusion out there.
Another difference is that the C headers don't have overloaded versions of some functions because C does not allow function overloading.

This means that the C++ header <cmath> will have a version of sqrt/pow that takes ints, another that takes floats, and another that takes doubles.

Whereas the C header <math.h> will only have one that takes doubles.
When you have to use <windows.h>, you have to use <windows.h>.
Topic archived. No new replies allowed.