Char in C++, What is it.

I've been coding for a while and i'm working on variables now, I know some like int, or double, or bool. but I can't figure out what Char is for. can someone help me.
A variable of type char can hold a character of the implementation’s character set. For example:
char ch = 'a';
Almost universally, a char has 8 bits so that it can hold one of 256 different values. Typically, the character set is a variant of ISO646, for example ASCII, thus providing the characters appearing on your keyboard. Many problems arise from the fact that this set of characters is only partially standardized.
Serious variations occur between character sets supporting different natural languages and also between different character sets supporting the same natural language in different ways. However, here we are interested only in how such differences affect the rules of C++. The larger and more interesting issue of how to program in a multilingual, multicharacterset environment is beyond the scope of this book, although it is alluded to in several places.
It is safe to assume that the implementation character set includes the decimal digits, the 26 alphabetic characters of English, and some of the basic punctuation characters. It is not safe to assume that there are no more than 127 characters in an 8-bit character set (e.g., some sets provide 255 characters), that there are no more alphabetic characters than English provides (most European languages provide more), that the alphabetic characters are contiguous (EBCDIC leaves a gap between 'i' and 'j'), or that every character used to write C++ is available (e.g., some national character sets do not provide { } [ ] | \). Whenever possible, we should avoid making assumptions about the representation of objects. This general rule applies even to characters.
Each character constant has an integer value. For example, the value of 'b' is 98 in the ASCII character set. Here is a small program that will tell you the integer value of any character you care to input:
1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
	char c;
	std::cin >>c ;
	std::cout <<"the value of '"<<c<<"' is "<<int(c)<<'\n';
}

The notation int(c) gives the integer value for a character c. The possibility of converting a char to an integer raises the question: is a char signed or unsigned? The 256 values represented by an 8-bit byte can be interpreted as the values 0 to 255 or as the values -127 to 127. Unfortunately, which choice is made for a plain char is implementation defined. C++ provides two types for which the answer is definite; signed char, which can hold at least the values -127 to 127, and unsigned char, which can hold at least the values 0 to 255. Fortunately, the difference matters only for values outside the 0 to 127 range, and the most common characters are within that range.
Values outside that range stored in a plain char can lead to subtle portability problems.
A type wchar_t is provided to hold characters of a larger character set such as Unicode. It is a distinct type. The size of wchar_t is implementation defined and large enough to hold the largest character set supported by the implementation’s locale. The strange name is a leftover from C. In C, wchar_t is a typedef rather than a built-in type. The suffix _t was added to distinguish standard typedefs.
Note that the character types are integral types so that arithmetic and logical operations apply.
Last edited on
Thank You sir. That helps a lot.
@helios
very well said, i think you should move this to the article section.
Meh. It's just a quote from TC++PL3.
so that's why it is in qoute tags..
Topic archived. No new replies allowed.