C string is defined as a character array, which can be defined in two ways: character array or string literal: character array: char str[size], where str is the string name and size is the number of characters plus 1. String literal: "string literal", automatically allocated memory and terminated by the null character.
String defined in C
In C, a string is an array of characters. There are two ways to define a string:
1. Character array
The syntax for using a character array to define a string is as follows:
<code class="cpp">char str[size];</code>
str
is the name of the string, size
is the size of the array (number of characters 1).
Example:
<code class="cpp">char name[10];</code>
2. String literal
String literal is another way to define a string. The syntax is as follows:
<code class="cpp">"string literal"</code>
String literals automatically allocate memory and end with a null character ('\0').
Example:
<code class="cpp">string name = "John Doe";</code>
Note:
std::string
class, which is a variable-length string type that provides more advanced functions. Such as splicing, comparison, search, etc. The above is the detailed content of How to define string in c++. For more information, please follow other related articles on the PHP Chinese website!