Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

std

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?

like image 470
Free Wildebeest Avatar asked May 24 '10 22:05

Free Wildebeest


People also ask

Is Stdlib H same as cstdlib?

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. Show activity on this post. No, other than the namespace situation, they're essentially identical.

What is cstdlib used for?

The C++ Standard Library header file (cstdlib in C++) is the header for one of the most widely used libraries by programmers of the language. This header defines a collection of functions and macros to facilitate efficient, high-performing, standardized C++ code across teams and platforms.

What is difference between Stdlib H and Stdio H?

One easy way to differentiate these two header files is that “<stdio. h>” contains declaration of printf() and scanf() while “<stdlib. h>” contains declaration of malloc() and free(). In that sense, the main difference in these two header files can considered that, while “<stdio.

Do I need to include cstdlib?

But yes, you should not count on this and always include cstdlib if you want to use rand . And in C++ code don't use rand , there are better ways to generate random numbers.


2 Answers

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.

like image 118
Brendan Long Avatar answered Oct 11 '22 15:10

Brendan Long


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

like image 16
Jerry Coffin Avatar answered Oct 11 '22 15:10

Jerry Coffin