Contrary to what the name says, and popular belief, the type char
is not a type of text, it is a one-byte numeric type that allows 256 different values. It happens to be used to express graphic symbols of text, but this is neither mandatory and in fact it is not possible to store something like this.
Abstraction
In fact all kind is only numerical if it lowers the abstraction adopted a little, and is always mounted through signals 0 and 1 that people know as binary.
But the guy char
nor need to lower the abstraction to understand that it is numerical, it is just that. If you have print the value of a type char
without saying that want a text what will be printed is a number (actually it is almost that, see more detail below).
Value and size range
The real value range of the type needs to be consulted when using according to the CHAR_MIN
and CHAR_MAX
available in the archive limits.h
. There are no guarantees in language specification that is the value reported in the question, it can be from 0 to 255 as well. It could be other things, but it’s very rare or nonexistent.
What the specification says is that this type must possess a byte. Some people use sizeof char
to find out the size where you need this information, but it doesn’t make sense, it’s guaranteed to be this size, it’s different from other types that actually size can vary. It is possible not to have 8 bits, but this is very rare. Then you would have to consult the CHAR_BIT
available from the compiler in the same file above.
If you want to know why it can always represent 256 different values is because the byte has 8 bits (and because it is bits can only have two values, 0 or 1), then 2 high to 8 gives 256, as well as in decimal we have 10 digits possible and if you have capacity for 4 digits the maximum that can reach is 10 to the 4, so 10,000.
Character
So we can consider that this type is a byte. By chance in most situations we use it as if it were a character.
This character is obtained through a table called ASCII. It says which character should be considered for each numerical value found in some value of this type. It is a representation map of the text to be used.
There are control codes, all letters of the lower and upper case alphabet, numerals, mathematical symbols and for other functions. The original table can go from 0 to 127, but some cases use from 128 to 255 as well. See what is the number of each character in the link above.
When you have explicitly or implicitly in some function print the character and not the number that is used in the value then the character will exit on the hardware used for printing, internally on the computer is just number.
So what you’re seeing there is a textual representation of a numeric code.
Let’s say you had the character 0 printed. The numeric code of that was 48. Yes, the code of the character '0' is not 0. So the data of the type char
is number 48.
If you print a numeric code 0 nothing will be printed, because it is used to indicate nothing, again, see the table.
So when you have a number actually printed it will not be printed as it is on the computer, there will be a conversion of the number to a sequence of character(s) and it is this sequence of character(s) that will be printed. Because that’s how humans understand. The same number is mounted in a way in the computer that the human does not understand, it is only a sequence of zeros and a "nonsense". And note that I’m not saying that zeros and ones would be printed, it’s even more confusing because you can only print text so there will always be a conversion to characters before printing, but without a suitable algorithm the conversion will be made to characters that do not make sense to you.
For example, an integer would be converted, most of the time since it depends on the platform size, into 4 characters.
You know those binary to decimal conversion exercises? That’s kind of what he does internally, only in the right and efficient way. These exercises give the impression that you are converting number, but are converting number to textual representation, numbers are numbers, they cannot be converted to each other because it is the same thing. The text is meant for humans, the text is used because we understand it that way. The number 123 is worth 123 units no matter if you are seeing decimal, hexadecimal or binary which is only the form you read.
When you look at a piece of paper written a number, you always think of it as a number, but actually there is only one text with numerals. You use another abstraction to better understand what that is, but concretely it’s just a text. Start thinking differently about it.
Completion
Then one understands the range of values of other integer types as short
, int
or long
, understands the char
also, just didn’t think he was an integer type.
Some prefer to be more explicit and use the complete type signed char
or unsigned char
. That can be more confusing in certain scenarios.
Lower value and higher value can be represented with such variable type, in case one byte considering signal.
– anonimo
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site
– Maniero