I l@ve RuBoard |
![]() ![]() |
4.10 CharactersThe type char represents single characters. The form of a character declaration is: charvariable; //comment Characters are enclosed in single quotation marks ('). 'A', 'a' and '!' are character constants. The backslash character (\) is called the escape character . It is used to signal that a special character follows. For example, the character \t can be used to represent the single character "tab." \n is the newline character. It causes the output device to go to the beginning of the next line, similar to a return key on a typewriter. The character \\ is the backslash itself. Finally, characters can be specified by \nnn, where nnn is the octal code for the character. Table 4-3 summarizes these special characters. For a full list of ASCII character codes, see Appendix A.
Example 4-5 demonstrates the use of character variables by reversing three characters. Example 4-5. print3/print3.cpp#include <iostream> char char1; // first character char char2; // second character char char3; // third character int main( ) { char1 = 'A'; char2 = 'B'; char3 = 'C'; std::cout << char1 << char2 << char3 << " reversed is " << char3 << char2 << char1 << "\n"; return (0); } When executed, this program prints: ABC reversed is CBA ![]() |
I l@ve RuBoard |
![]() ![]() |