- Basics of C++
- Structure of a program
- Variables and types
- Basic Input/Output
String class has been briefly introduced in earlier section. It is very powerful class to handle and manipulate strings of characters. However, because strings are, sequences of characters, we can also represent them as plain arrays of character type.
Example, the following array:
is an array that can store up to 20 elements of type char. And it can be represented as:
Therefore, in this array, we can store sequences of characters up to 20 characters. But we can also store shorter sequences. Example, Array Jay could store at some point in a program either the sequence "Hello" or the sequence "Merry christmas", since all are shorter than 20 characters.
Therefore, since the array of characters can store shorter sequences than its total length, a special character is used to signal end of the valid sequence: i.e., the null character, whose literal constant can be written as '\0' (backslash and zero).
Our array of 20 elements of char type, called Jay, can be represented storing the characters sequences "Hello" and "Merry Christmas" as:
Notice: after the content of string itself, null character ('\0') has been added in order to indicate the end of sequence. Panels in gray color represent elements of char type with undetermined values.
Initialization of character sequences that is null-terminated
Because arrays of characters are ordinary arrays, they follow the same rules. E.g.,
Above we declares an array of six elements of type char initialized with the characters that form the word "Tuton" plus a null character '\0' at the end to indicate the end of string sequence.
But arrays of character have another way to be initialized: using string literals directly.
Used in some examples in previous chapters. These are specified by enclosing the text between double quotes("").
These string literals, probably used in some earlier example.
Sequences of characters enclosed in double-quotes(") are literal constants. And their type is a null-terminated array of characters. Means that string literals always have a null character ('\0') automatically appended at the end.
So, the array of char elements called myword can be initialized with a null-terminated sequence of characters by either one of these given 2 statements:
char myword = "Tuton";
Strings and null-terminated character sequence:
Plain arrays with an null-terminated sequences of characters are the typical types used in the C language to represent a strings (that is why they are also known as C-strings). In C++, even though standard library defines a specific type for strings (class string), still, plain arrays with an null-terminated sequences of characters are a natural way of representing strings; in fact, string literals still always produces an null-terminated character sequences, & not string objects.
In standard library, both representations for strings (both C-strings and library strings) coexist, and most functions requiring strings are overloaded to support both.
Example, cin and cout supports null-terminated sequences directly, allowing them to be directly extracted from cin or inserted into cout, just like strings.
using namespace std;
int main ()
char question1 = "What is your name? ";
string question2 = "Where do you live? ";
char answer1 ;
cout << question1;
cin >> answer1;
cout << question2;
cin >> answer2;
cout << "Hello, " << answer1;
cout << " from " << answer2 << "!\n";
Where do you live? Greece
Hello, Homer from Greece!
In the example, both arrays of characters using an null-terminated sequence and strings. They are quite interchangeable in their use together with functions cin and cout, but there is a difference to be noted that in their declarations: arrays have a fixed size that needs to be specified by either implicit or explicitly when they are declared; question1 has a size of exactly 20 char (including the null-character) and answer1 has a size of 80 char; while strings are simply strings, no size is specified there. This is due to fact that strings have a dynamic size determined during the runtime, while size of arrays is determined on the compile time, before program runs.
In any case, an null-terminated character sequences and arrays are easily transformed from one another:
Null-terminated character sequences can be transformed into strings implicitly, and also strings can be transformed into null-terminated character sequences by using string's member functions c_str or data:
string mystring = tuton; // convert c-string to string
cout << mystring; // printed as a library string
cout << mystring.c_str(); // printed as a c-string
Note: that both c_str and data members of string are equivalent