![]() |
Characters |
A character is a symbol that displays on your screen. It can be a letter such as a, b, c, d, e, f, g, h, I, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, and z. It can also be a letter from A to Z. It can also be a digit such as 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. It can also by a special characters such as ` ~ # $ ! @ % ^ & * ( { [ ) } ] | \ : ; “ ‘ + - < _ ? > , / =. A character is really an integer whose value can range from -128 to 127. To declare a character variable, you use the char keyword. Here is an example: using namespace System; int main() { char AlphaLetter; Console::WriteLine(); return 0; } Since a char variable represents one symbol, to initialize it, enclose the initial value in single quotes. Here are examples: using namespace System; int main() { char AlphaLetter = 'd'; char Pick('G'); return 0; } A character variable can also be declared with the signed char data type. This type of variable would be an integer whose value can range from –128 to +127. One of the limitations of the char data type is that it can fit only on a single character (limited to 8 bits). To support a wider range of characters, you can use the Char or the __wchar_t data types to declare a character variable. In fact, you should use either Char or __wchar_t whenever you need a character. The Char and the __wchar_t data types are defined in the System namespace. Here is an example: using namespace System; int main() { __wchar_t AlphaLetter = 'F'; Console::WriteLine(AlphaLetter); return 0; } This would produce: F Press any key to continue Based on their structures, Char and __wchar_t variable can accommodate letters in many other languages than Latin-based. When initializing a variable declared with Char or __wchar_t, to indicate that you are using a Unicode character, you can precede the quoted value with L. Here is an example: using namespace System; int main() { Char Letter = L'D'; Console::WriteLine(Letter); return 0; } This would produce: D Press any key to continue
A string is a group of characters considered as one unit, as opposed to a single character as we introduced above. Unlike some other languages such as (Object) Pascal or (Visual) Basic, C++ doesn't have a data type that can hold a group of characters as one entity. The alternative was to "create" and adapt a string data type in additional library. This problem was partially solved in a library called Standard Template Library (STL). Therefore, the STL provides the string data type. To declare a variable that can hold a string, you can use the string data type (the string is a class but we will consider it a data type for simplicity). The value of the string must be included in double-quotes. Here is an example: string Course = "Business Mathematics"; The string data type is part of the std namespace and it is defined in the string library. When using the string data type, make sure you include the string library and use the std namespace. To display the value of a string variable, you can use the cout extractor. Here is an example: #include <string> #include <iostream> using namespace std; using namespace System; int main() { string Course = "Business Mathematics"; cout << Course; Console::WriteLine(); return 0; } This would produce: Business Mathematics Press any key to continue
|
|
||
Previous | Copyright © 2006-2016, FunctionX, Inc. | Next |
|