Range of variables in C

Asked

Viewed 257 times

2

I could never understand the question of the range of variables, I know that each one occupies a certain space in memory, but what does the interval mean? When I say a variable char assumes values in an interval between -128 to 127, this value means what? In numerical variables it makes sense, it would be the numerical range that the variable can assume, but the variables of the type char?

  • Lower value and higher value can be represented with such variable type, in case one byte considering signal.

  • 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

2 answers

5

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.

3

This value, 128, refers to the ASCII table (American Standard Code for Information Interchange), as described in this scientific initiation of UNICAMP:

When a variable is declared as char type the programmer already indicates your intention to use the variable to store symbols. A correspondence between symbols and numbers is given by the ASCII table (American Standard Code for Information Interchange). This table uses the numbers from 0 to 127 for letters of the English alphabet and for the most common punctuation marks. For example, in entry 65 of table ASCII find the letter 'A'. Thus, a char variable whose content is 65 would also represent the letter 'A'.
Unfortunately there is no consensus on how to treat characters numbered from 128, where would be the characters accentuated, and this causes incompatibilities between programs that compile and run on different platforms.

I recommend reading too: Types of data in C and Ascii table.

  • The answer has been edited if you would like to make any further suggestions!

  • :-) became legal now -I will delete the previous comments.

  • If you would like to suggest an interesting link or article to be attached to the reply, I will be grateful! Thank you for the comments.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.