Popular Google Pages:
◕ This article is regarding char Type: The Smallest Integer in C++.
Last updated on: 10th December 2016.
◕ What is char Type?
- We can say the char Type is the smallest Integer Type. It can store characters, such as letters and numeric digits. The char Type is most often used to handle characters, but we can also use it as an integer type which is smaller than the short Type.
The char Type is large enough to store the entire range of basic symbols, such as all the letters, digits, punctuations etc. In normal life, any computer supports maximum 128 kinds of characters, so a single byte can represent the whole range.
Storing numbers is not a big deal for C++.
Programming languages, like C++ takes an easy way to store a letter, by using number code of that letter.
C++ follows the ASCII code for this number code.
For an example,
65 is the number code for the character A
66 is the number code for the character B
67 is the number code for the character C.
Let we have a program:
using namespace std;
char apple = 'A';
int india = apple;
cout << "The code for " << apple << " is " << india << endl;
cout << "If we add one with the character code -" << endl;
apple = apple + 1;
india = apple;
cout << "Now the code for " << apple << " is " << india << endl;
It will give us the result:
The code for A is 65
If we add one with the character code -
Now the code for B is 66
In the above program if we look into the memory location of the char apple, we will find 65 is stored there. Now we add one with the char apple. Hence this memory location stored the value 66 which is B.
Also please note that:
We have enclosed the character A within single quotation marks, like 'A'.
Double quotation marks are used for the String, like "We are Indian."
◕ How these stored values convert to the characters?
- This conversion of char type happens due to the cin and cout manipulator.
When we input a character like A, cin converts this input to its corresponding value which is 65.
While on output, the manipulator cout converts this value, which is 65 to its corresponding character A.
The Type of the variable guide the cin and cout to do this job.
How to display a value in Hexadecimal form in C++?
How C++ identify the base of a number?
Symbolic Constant in C++
Top of the page