the confusion on c library

that is the description of c library in the website Reference:
The elements of the C language library are also included as a subset of the C++ Standard library.
but on the meanwhile,C++ library change the name of the C library.it deletes the suffix ".h" and adds "c" ahead. Like "assert.h" to "cassert","errno.h" to "cerrno".
and now i am going to learn c library by myself. i wonder to know if there is any difference between these headfiles?if i have learned C library before,should i learn corresponding headfiles in C++ library?
At least one important difference is that <cmath> evolves as the C++ language evolves, while <math.h> is relatively stagnant and would only be updated when the C++ language is updated to reference a more up-to-date C standard library.

By this I mean, the following probably won't compile:
1
2
3
4
5
6
7
// Example program
#include <math.h>

int main()
{
    std::round(4.2);
}

6:3: error: 'round' is not a member of 'std'


This, however, will compile:
1
2
3
4
5
6
7
// Example program
#include <cmath>

int main()
{
    std::round(4.2);
}


std::round is a C++11 feature, and is properly part of the std namespace in <cmath>, but is not part of the std namespace in <math.h>. C has its own, non-namespaced (obviously) version of round: https://en.cppreference.com/w/c/numeric/math/round (the functionality I assume is the same)

Theoretically, the .h files could eventually be deprecated, but realistically, they will stick around for backwards compatibility. There's no reason to use them in new code, however.
Last edited on
This also compiles:

1
2
3
4
5
6
#include <math.h>

int main()
{
	round(4.2);
}


Right, round also exists in the C standard library, so perhaps it wasn't the best example.

A better example would be something fancy like std::riemann_zeta
https://en.cppreference.com/w/cpp/numeric/special_functions/riemann_zeta

Those newer functions are not found at all within <math.h>, because they're not in the C standard library.

Good [C++17]:
1
2
3
4
5
6
#include <cmath>
using namespace std;
int main()
{
    riemann_zeta(3.0);
}


Bad:
1
2
3
4
5
6
#include <math.h>
using namespace std;
int main()
{
    riemann_zeta(3.0);
}
Last edited on
Topic archived. No new replies allowed.