Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<string.h> or <string>?

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!

like image 989
Overflowh Avatar asked Nov 29 '11 15:11

Overflowh


People also ask

Is it string H or strings H?

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.

What is difference between string and string h?

<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.

Should I include string h?

#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.

What is string h in C++?

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).


3 Answers

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).
like image 75
Michael Krelin - hacker Avatar answered Nov 03 '22 11:11

Michael Krelin - hacker


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>

like image 21
triclosan Avatar answered Nov 03 '22 13:11

triclosan


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'.

like image 43
Sadique Avatar answered Nov 03 '22 11:11

Sadique