Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including standard header files. string.h or cstring? or both? [duplicate]

Tags:

c++

c

header

Possible Duplicate:
Difference between string.h and cstring?

What is better programming practice in C++ when including the standard header files with respect to

including cmath in place of math.h or vice-versa?

including cstring in place of string.h or vice-versa?

and for other <c*> and <*.h> header files which apparently seem to accomplish the same thing?

like image 990
smilingbuddha Avatar asked Jan 05 '12 20:01

smilingbuddha


People also ask

Is string H and cstring same?

Apparently cstring is for C++ and string. h is for C. One thing worth mentioning is, if you are switching from string. h to cstring , remember to add std:: before all your string function calls.

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.

Should I use cstring or string?

Use string. cstring is so 1970's. string is a modern way to represent strings in c++. you'll need to learn cstring because you will run into code that uses it.

What is cstring header file?

The cstring header file contains definitions for C++ for manipulating several kinds of strings. Include the standard header into a C++ program to effectively include the standard header <string. h> within the std namespace.


1 Answers

<cstring> is newer; <string.h> is really there for backwards compatibility (and for C, of course). The difference is that <cstring> puts the string functions in the std namespace, while <string.h> puts them in the global namespace.

In addition, <cstring> changes the types of certain functions to promote type-safety. E.g., the C declaration

char *strchr(char const *, int);

is replaced by the overloads (in the std namespace)

char       *strchr(char       *, int);
char const *strchr(char const *, int);

In the case of <cmath> there are further differences with <math.h> which make <cmath> more idiomatic and less C-like.

Prefer <cstring> for new code and use the std:: prefix on the functions.

like image 179
Fred Foo Avatar answered Oct 16 '22 17:10

Fred Foo