It depends. For ASCII encoded strings, see paragraphs C and C ++. For unicode-encoded strings, see the last paragraph.
C:
As David noted, it depends on how you use the string in C:
- as a constant:
const char s[] = "Hello World"; - as a string containing variable data, then:
char s[] = "Hello World"; - as an array of data
char *data ; Initialization must then be configured.
Note that in C all lines are null-terminated, this means a definition, for example. char s[] = "foo"; implicitly includes the NULL character at the end of s[3]='\0' .
Also note the difference in subtlety between char *s and char s[] , which can often behave the same, but sometimes not! (see Is the name of the array a pointer? ), for example:
#include <stdio.h> #include <stdlib.h> int main( int argc, char* argv[]) { char s[] = "123456789123456789123456789"; char *t = (char*) malloc( sizeof(char) * 28 ); for( size_t i = 0; i < 28; ++i ) t[ i ] = 'j'; printf( "%lu\n", sizeof(s) ); printf( "%lu\n", sizeof(t) ); printf( "%s\n", s ); printf( "%s\n", t ); return EXIT_SUCCESS; }
Therefore, I recommend using char arrays whenever you use them as strings and char pointers whenever you use them as a data array.
C ++:
C ++ has its own string data type: std::string . If you just need the C-String version for std :: string (e.g. using some C-API), just use the c_str() member:
std::string s = "Hello World"; your_c_call( s.c_str(), ... );
Unicode:
I want to have strings in Unicode, then you really have to go with something like
char utf8String[] = u8"Hello World";
and try not to use wchar_t whenever possible. See this great article on this subject: http://www.nubaria.com/en/blog/?p=289 . Please, not that there is Unicode support for C ++. But as a rule, I am tempted to say that you should go with normal characters as much as you can. Interesting resource: http://www.cprogramming.com/tutorial/unicode.html