i don't understand char

i have looked at other stuff but i still don't understand char could you give me an example or explain it simply try not to get complicated.
Last edited on
char is short for character. It is a variable that just stores one character. It is commonly used when just storing one letter.
http://www.cplusplus.com/doc/tutorial/variables/
Last edited on
what does this do exactly

1
2
3
4
5
6
7
8
9
 
include <iostream>

int main()
{
	char c;
	std::cin >>c ;
	std::cout <<"the value of '"<<c<<"' is "<<int(c)<<'\n';
}
To be more precise, a char stores an integer. That integer corresponds to an ASCII character, and various aspect of C++ will treat it as a character where appropriate.

The code you've posted will:

- declare a variable c, of type char
- read in a single character on the command line, typed by the user, and store it in c
- display the value as a character
- display the value as an integer
Last edited on
char is one byte. It is a signed integer from -127 to 128 in value.
unsigned char is 0-255 in value.

It is used both as a "RAW BYTE" data type and for storing ascii characters (strings, text) (but it is not for unicode text) and in some older code, it has been used as boolean as well (before the type bool was added to the language).

The operating system knows how to print specific pixels on the screen for specific integer values. It knows to that when I type A, to print those pixels, even though the 'value' is a number (65, if memory serves, but don't quote me on that).

The letters are arranged in a sensible way in the ascii lookup table, so that B > A, 2 > 1, etc to allow alphabetical sorting of strings, etc.

@jonnin
[char] is a signed integer from -127 to 128 in value.

Not necessarily. char is different than both unsigned char and signed char, and as a result the signedness of sign-unqualified char is implementation-defined.

You should prefer unsigned char in the case where a raw byte array is required: doing bit math or arithmetic on signed values is more difficult (because the representation of signed values is implementation defined) and they are more prone to implementation-defined, introducing portability problems or undefined behavior (e.g., signed overflow is undefined behavior, but unsigned overflow isn't).

The object representation of types in C++ is explicitly unsigned char:
http://eel.is/c++draft/basic.types#4
Last edited on
That is true. Most common systems do it the way I said, but you may encounter one of these other approaches and its good to keep it in mind.
Topic archived. No new replies allowed.