Categories
linuxhint.com

How to use C++ String Literal

A character literal is a single character in single quotes. An escape sequence is a character that can also be in single quotes. A string literal is a sequence of characters in double-quotes. The equality operators work with string literals, but the relational operators do not work with string literals. This article explains how to use C++ string literals.

The computer keyboard has characters printed on them. When you press a key, you see the character on the screen. Note: space is also a character. A string literal is a sequence of characters. This article explains how to use C++ string literals. You should know about C++ arrays and pointers to understand this article.

Character Literal

A character literal is a character in single quotes. So,

char ident1 = ‘A’; char ident2 = ‘b’; char ident3 = ‘4’; char ident4 = ‘6’;

are all different definitions of characters. Note that a digit in single quotes is a character and not an integer.

An escape sequence such as ” (see below) in single quotes, is a character. So,

char ident1 = ‘”‘;

is a character.

A single symbol in double-quotes is not a character; it is a string of one character. So “A” or “c” or “2” is not a character but is a string of one character each.

The variable of a char can be reassigned, later down in the program, as follows:

char ident = ‘x’;
ident = ‘Y’;

To stop a character assigned to an identifier from being changed, later down in the program, precede the definition with the reserved word, const, as follows:

const char ident = ‘d’;

The variable, ident is said to be read-only.

String Literal

A string literal is a sequence of characters in double quotes. So,

char ident1[] = “I love you”; char ident2[] = “I hate 3 of you”; char ident3[]
= “we are the world”; char ident4[] = “Hello World!”;

are all different definitions of string literals. Note the use of double-quotes. There is nothing like an ordinary variable for a string. A string literal is an array of chars, where instead of delimiting with {}, the sequence is delimited with “”. The chars are not separated by commas. Any number greater than the number of chars in the string literal can be placed in the square brackets. However, it is better to leave the square brackets empty.

A single character in double-quotes is not a character; it is a string of one character. So “A” or “c” or “2” is not a character, but a string of one character each.

A string variable does not allow re-assignment of the complete literal, later down in the program – see below. However, individual characters can be re-assigned – see below.

Single and Double Quote in Character or Literal

To have a single quote as a character, do something like,

char ident = ;

To have a double quote as a character in a string literal, do something like,

char ident[] = “ab”cd“;

The backslash is used in an escape sequence, to avoid conflict with delimiters. To have a double quote as a character, there is no need for the backslash: ‘”‘ is alright. To have a single quote in a string literal, there is no need for the backslash:”ab’cd” is alright.

Since the backslash is used to escape a character, it has to be escaped with another backslash when used as a character or in a string literal.

Escape Sequence

An escape sequence is one of:

‘ ” ? \ a b f n r >t v

Each escape sequence is normally typed either as a character within single quotes or as an escape sequence within double-quotes.

’ : is used as a single quote character, within single quotes.

” : is used as a double quote character, within a literal.

? : since ? is a reserved character, it should be escaped in a literal.

\ : the backslash should be escaped as a character or in a string literal, so as not to result in some other meaning.

a : sounds an alarm bell once, when used as a character or within a string literal.

b : results as a backspace in the display within a string literal, taking off the previous character.

f : causes the next page to be fed to the printer when used as a character or within a literal.

r : returns the cursor, where the next character is to be printed, but within the current line.

n : returns the cursor to the beginning of the next line or just to the next line, depending on the operating system.

t : creates a horizontal tab.

v : creates a vertical tab.

Operations with Characters

Concatenation

At definition, two string literals can be joined with space as follows:

char ident[] = “abc” “def”;
cout << ident << n;

The output is: abcdef . This definition can be extended to more than two literals. Note: the statement is a definition, not just an assignment. The definition can even continue to the next line with space separating the lines as follows:

char ident[] = “abc” “def”
“ghi”;
cout << ident << n;

The output is, abcdefghi.

Note: Characters cannot be concatenated in this way, as the single quotes for the character cannot have more than one symbol.

Equality Operators

Same characters at the same case are equal. They are not equal if they are not of the same case. Consider,

bool result = ‘B’ == ‘B’;
cout << result << n;

== means equals, while = means assigned-to and not equals. The output is 1 for true. Consider,

bool result = ‘B’ == ‘b’;
cout << result << n;

The output is 0 for false. Consider,

bool result = ‘b’ == ‘c’;
cout << result << n;

The output is 0 for false. Consider,

bool result = ‘B’ != ‘B’;
cout << result << n;

!= means not-equal, while = means assigned-to and not not-equal. The output is 0 for false. Consider,

bool result = ‘B’ != ‘b’;
cout << result << n;

The output is 1 for true. Consider,

bool result = ‘b’ != ‘c’;
cout << result << n;

The output is 1 for true.

So, == and != are equality operators.

Relational Operators

For ordinary characters in C++, in ascending order, numbers come before uppercase letters, which come before lowercase letters.

So < will return true (1) when the left character is less than the right character. The other relational operators, <=, >, >= are similarly explained.

The String Literal as an Object

The array is a constant pointer to the beginning of a particular data type sequence. Similarly, the string is a constant pointer to the beginning of a character sequence. Compare the following definitions:

int arr[] = {3, 4, 5, 6, 7};
char str[] = {‘w’, ‘o’, ‘m’, ‘a’, ‘n’};
char stri[] = “woman”;

The first array is an array of ints and has five elements. The second and third arrays are arrays of chars with different names, but the same number of elements. The second and third arrays are the same, but for their names. The text content of the second array is delimited by braces; the characters are separated by commas and each character is in single quotes. The text content of the third array is delimited by double quotes; the characters are not separated by commas and each character is not in single quotes. The second and third arrays are two ways of producing a string, with the third way being the better way.

arr is a constant pointer to the first element of its array, meaning arr will always point to the location having the integer, 3 even if the value of 3 is changed. The size of the array, five elements, does not really remain constant. However, each of the values of the array can be changed.

str is a constant pointer to the first element of its array, meaning str will always point to the location having the character, ‘w’ even if the value of ‘w’ is changed. The size of the character array, five elements, does not really remain constant. However, each of the values of the literal can be changed.

stri is a constant pointer to the first element of its literal (array), meaning stri will always point to the location having the character, w even if the value of w is changed. The size of the string literal (array), five elements, does not really remain constant. However, each of the values of the literal can be modified.

What is constant in an array or string literal? The memory address of the first element of the array or literal remains as the value of the name (identifier) of the array or literal, and cannot be changed. Well, the size of the array or literal does not really remain constant. Each value in the array or literal can be changed. The following code shows how the fourth element of each of the arrays has been changed:

int arr[] = {3, 4, 5, 6, 7};
char str[] = {‘w’, ‘o’, ‘m’, ‘a’, ‘n’};
char stri[] = “woman”;

arr[3] = 9;
str[3] = ‘e’;
stri[3] = ‘e’;

cout << arr[3] << n;
cout << str << n;
cout << stri << n;

The output is:

9
womenwomen
women

Note that the elements of a defined string literal, like for the third definition above, can be accessed with the array index (subscript). The reason for the second line of the output is given below.

Definition Subscript

Note that in the above definitions, there is no integer for subscript. When the number of elements cannot be easily determined, by the programmer, the integer for the subscript should be omitted. Whatever is the case, the integer should not be less than the number of elements in the array.

For the string literal, the integer should be at least 1 higher than the number of characters in the string. This is because the null character () is always added by the compiler, at the end of an array that is a string, delimited by double-quotes. The null character is not added at the end of the second array above, because it is not an official string. The third array is an official string. The following code shows the minimum subscript values.

int arr[5] = {3, 4, 5, 6, 7};
char str[5] = {‘w’, ‘o’, ‘m’, ‘a’, ‘n’};
char stri[6] = “woman”;

In order to make the second definition an official string, the null character has to be added as follows:

int arr[5] = {3, 4, 5, 6, 7};
char str[6] = {‘w’, ‘o’, ‘m’, ‘a’, ‘n’, };
char stri[6] = “woman”;

The output should now be,

9
women
women

without the second “womenâ€. Note that the corresponding subscript for the second array is 6, and not 5 as it was.

Constant Literal Values

To stop any character in the double quotes assigned to an identifier from being modified, later down in the program, precede the definition with the reserved word, const, as follows:

const char ident[] = “I love you”;

Operations with String Literals

Equality Operations

The equality operators are == and != . When variables (identifiers) of two strings are compared, it is the pointers (addresses) of the literals that end up being compared; that is wrong. To compare strings, the literals have to be compared, as in the following code:

bool result = “woman” == “woman”;
cout << result << n;

The output is 1 for true. The comparison is done in the dictionary fashion, but with numbers coming first in ascending order, before uppercase letters, which come before lowercase letters. The output of the following code is 0, for false.

bool result = “woman” != “woman”;
cout << result << n;

Relational Operators with String Literals

Relational operators do not work with string literals.

Raw String Literal

A raw string literal, allows a string to be displayed as typed, ignoring escape sequences and respecting newlines. Consider the following code:

char str[] = R“(abc\d efg hij
klmn n
‘ opq
rst)”;
cout << str << ‘
n‘;

The output is:

abc\d efg hij
klmn n ” ‘ opq
rst

In the code, the raw string literal begins with R, followed by ” and ( . It ends with ) and “.

C++ Main String Literal Types

char

The char type is the original C++ type and would typically store a character in 8 bits.

char16_t

This stores a character in 16 bits.

char32_t

This stores a character in 32 bits.

wchar_t

char16_t and char32_t are wide characters. wchar_t is a wide-character that is proprietary and implementation-defined.

Conclusion

A character literal is a single character in single quotes. An escape sequence is a character that can also be in single quotes. A string literal is a sequence of characters in double-quotes. A string literal is an array of characters that end with . The equality and relational operators work with character literals. The equality operators work with string literals, but the relational operators do not work with string literals. Character identifiers can be used in comparisons, but string identifiers should not be used in comparisons. A raw string literal allows a string to be displayed as typed, ignoring the escape sequences and respecting newlines.

Chrys

Leave a Reply

Your email address will not be published. Required fields are marked *