Welcome! Log In Create A New Profile

Advanced

C++ Header files...

Posted by JacoBLom 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
C++ Header files...
May 19, 2008 07:57PM
Hi There,

What is the difference between #include <math.h>, and #include <cmath>?

#include <cmath> is used on page 262...
and on the following website http://www.cplusplus.com/reference/clibrary/cmath/exp.html, they use #include <math.h>, which also refers to the underlying functions withing the MATH header file.

Regards
Re: C++ Header files...
May 23, 2008 10:02AM
Hi Jaco,

#include <library.h> is the C style to include headers, while #include <library> is the C++ style to do it. I suggest you stick to the C++ style.

The difference between <math.h> and <cmath> is that <math.h> is a C library, where <cmath> is a *different* C++ library intended to recreate the functionality of <math.h> in C++.

I looked at the website you posted - It's pretty obvious that the example is straight C instead of C++. If it were C++, they would have used cout instead of printf.

Regards,
Gustav
avatar Re: C++ Header files...
May 23, 2008 01:41PM
These are all standard library files. All libraries that come from the days of C are all prefixed by 'c' (e.g. cmath, cstdio, cstddef, etc.).

It was also decided that all headers that form part of the standard library (including the standard template library, STL) should not have the extension '.h' but, since there are diehards out there, the ones with extensions still exist and you can #include them as well. To cope with the c and c++ versions, compilers usually ship with both and the extensionless version usually includes the one with the extension.

This is a bit more simplified than what it actually is. To be safe, always #include the one that does NOT have the extension. Math only exists as math.h and you should rather use that one instead of cmath.
Re: C++ Header files...
May 23, 2008 02:58PM
Quote
robanaurochs
To be safe, always #include the one that does NOT have the extension. Math only exists as math.h and you should rather use that one instead of cmath.

A bit self contradicting I think?
Re: C++ Header files...
May 23, 2008 03:19PM
Rob,

Why do you say that <math.h> should be used instead of <cmath> in a C++ program?

Gustav
avatar Re: C++ Header files...
May 27, 2008 10:16PM
I beg your pardon Gustav. I've taken a look at the headers again. cmath includes math.h and adds extra c++ goodies, so you should rather use cmath in c++ programs.

It is still a better idea, however, to use the headers in the standard library without the .h extension if you're creating c++ programs.
Re: C++ Header files...
May 28, 2008 11:00AM
Uh... that is what I said.

Quote
Gustav Bertram
#include <library> is the C++ style to do it. I suggest you stick to the C++ style.
Sorry, only registered users may post in this forum.

Click here to login