Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include string or string.h

Tags:

c++

To use memset(), what is the difference between

#include <string>       //did not work

and

#include <string.h>     //worked

Thanks!

like image 670
lukmac Avatar asked Oct 06 '11 11:10

lukmac


People also ask

What is the difference between #include string and #include string h?

#include <string> File extension is needed in header inclusion for C language. #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.

Is it necessary to include string h?

The <string> library is included in the <iostream> library, so you don't need to include <string> separately, if you already use <iostream>. "string. h" header file is for the function like strlen,strcpy,strcmp etc. without string header file you can't use these inbuilt function.

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.

Why do we use #include string h in C?

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.


2 Answers

<string> is a C++ standard library include, and <string.h> is C standard library include.

The equivalent of <string.h> in C++ is <cstring>, although both will work.

The difference is: <cstring> wraps everything in the std namespace whereas <string.h> puts everything in the global namespace.

Also, expect some stricter type safety rules from <cstring>.

like image 81
RushPL Avatar answered Sep 17 '22 12:09

RushPL


In a modern C++ environment, you would #include <cstring> to get memset().

like image 35
Ernest Friedman-Hill Avatar answered Sep 18 '22 12:09

Ernest Friedman-Hill