Which is the best way to include the standard header string.h in a C++ project?
Using the [dot]h at the end, like this:
#include <string.h>
or just writing
#include <string>
Or, maybe, using another way that I don't know?
Thanks!
h is the header in the C standard library for the C programming language which contains macro definitions, constants and declarations of functions and types used not only for string handling but also various memory handling functions; the name is thus something of a misnomer. Functions declared in string.
<string. h> contains old functions like strcpy , strlen for C style null-terminated strings. <string> primarily contains the std::string , std::wstring and other classes.
#include <string. h> Inclusion of <string> In C++ is recommended when the program needs to use string. The same purpose is there for inclusion of <string. h> in C language.
h is the header file required for string functions. This function appends not more than n characters from the string pointed to by src to the end of the string pointed to by dest plus a terminating Null-character. The initial character of string(src) overwrites the Null-character present at the end of string(dest).
Those are two different headers.
<string>
is for c++ std::string
class<string.h>
is for c string functions (like strlen()
, etc.), which should be <cstring>
for c++ project (this is the third, you didn't know of).its quite different!
<string.h>
this library for C-style strings
<string>
for C++ strings
by standard in C++ you should use <cstring>
instead <string.h>
Wiki says:
The C++ Standard Library also incorporates 18 headers of the ISO C90 C standard library ending with ".h", but their use is deprecated. All other headers in the C++ Standard Library DO NOT end in ".h".
Each header from the C Standard Library is included in the C++ Standard Library under a different name, generated by removing the .h, and adding a 'c' at the start; for example, 'time.h' becomes 'ctime'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With