04-24-2011, 05:12 PM
Dear student!
In such case where we need to store string, we use char array data type. Which is used to represent a sequence of characters regarded as a single data item. In C++, strings of characters are held as an array of characters, one character held in each array element. In addition a special null character, represented by `\0', is appended to the end of the string to indicate the end of the string. Hence if a string has n characters then it requires an n+1 element array (at least) to store it. Thus the character `a' is stored in a single byte, whereas the single-character string "a" is stored in two consecutive bytes holding the character `a' and the null character.
For example: A string variable s1 could be declared as follows:
char s1[10];
In such case where we need to store string, we use char array data type. Which is used to represent a sequence of characters regarded as a single data item. In C++, strings of characters are held as an array of characters, one character held in each array element. In addition a special null character, represented by `\0', is appended to the end of the string to indicate the end of the string. Hence if a string has n characters then it requires an n+1 element array (at least) to store it. Thus the character `a' is stored in a single byte, whereas the single-character string "a" is stored in two consecutive bytes holding the character `a' and the null character.
For example: A string variable s1 could be declared as follows:
char s1[10];