What's the difference between cstdlib and stdlib.h?

C++Std

C++ Problem Overview


When writing C++ code is there any difference between:

#include <cstdlib>

and

#include <stdlib.h>

other than the former being mostly contained within the std:: namespace?

Is there any reason other than coding standards and style to use one over the other?

C++ Solutions


Solution 1 - C++

The first one is a C++ header and the second is a C header. Since the first uses a namespace, that would seem to be preferable.

Solution 2 - C++

No, other than the namespace situation, they're essentially identical.

Solution 3 - C++

> Is there any reason other than coding standards and style to use one over the other?

Yes. The fact that stdlib.h is deprecated is a very good reason to not use it. It was actually deprecated in the very first standard that came 1998. Sure, it still existed in C++14, and possibly or even probably in C++17 (I don't have access to the C++17 standard) but since it is deprecated it is strong signal that you should not use it. Maybe the risk of removal isn't very high, but why even risk it while writing new code when it is so easy to avoid?

From C++14 standard:

> These are deprecated features, where deprecated is defined as: Normative for the current edition of the Standard, but having been identified as a candidate for removal from future revisions.

>...

You should have a pretty strong argument to use stdlib.h instead of cstdlib

Apart from that, you also have the practical matter that cstdlib is using a namespace, which is preferable in most cases.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFree WildebeestView Question on Stackoverflow
Solution 1 - C++Brendan LongView Answer on Stackoverflow
Solution 2 - C++Jerry CoffinView Answer on Stackoverflow
Solution 3 - C++kluttView Answer on Stackoverflow