Data type of C language Note (i)

Source: Internet
Author: User

In this article, "numbers in the eyes of computers," I mentioned that bytes are the smallest addressable unit of a computer, and that the address corresponds to a byte, not every bit of the byte. The reason for this is simple-a single bit can represent too little information, only two states 0 and 1, and only enough bits can be combined to represent a sufficiently rich set of messages. So why must it be 8? Because everyone is doing this ... Well, for certain historical reasons, I will not delve into it.

However, even a byte of 8 bits, the amount of information it can represent is still limited, because at most only 256 of the state combination, if each state corresponds to the number between 0~255, then cannot represent 256 this number. This is the time to use two bytes to represent a number greater than 255. In the same way, when the number is larger than the limit of the number of two bytes that can be expressed, it takes four bytes ... Yes, you've also found that the number of bytes is always doubling, why not 3 bytes? Even, why not use 1.5 bytes? For the former, because of the reason for the alignment, there is too much to say, I will not unfold, and the latter, in fact, because there is no bit of the address, unable to drill into the inside of the byte to pull out the data. Therefore, the storage will certainly have a certain waste, inevitable.


above wordy so much, in fact just to elicit this article of the pig's foot--c language data type, why C language to divide so many types? Because the amount of storage space required is different for a number of different sizes. If you use 4 bytes of storage, then certainly not the data type, but a good waste oh ~ So, in the memory-saving considerations, the data type is born. The data types of C are divided into basic data types and composite data types, which are just some combination of the former. The basic data types are divided into integer types and floating-point number types according to how they are stored on the computer.


One, integer type

Integer types include char, short, int, long, long long, and they have no fractional parts. Char is a character type, but because it stores ASCII code, it is essentially stored as an integer, so it belongs to this category. Different integer types have different number of bytes, which takes up different storage space. However, this is uncertain, the exact number of bytes depends on the specific machine and compiler-the machine not only brands, but also 32-bit and 64-bit points. The table below gives a typical number of bytes for different types (source "in-depth computer system"):

Type 32-bit machine 64-bit Machine
Char 1 1
Short 2 2
Int 4 4
Long 4 8
Long Long 8 8
char * 4 8
Float 4 4
Double 8 8


Note that the above table is just a typical value, taking 32 bits as an example: on some machines, short and int are 2 bytes, Long is 4 bytes, and on some machines, short is 2 bytes, and int and long are 4 bytes. The C language only stipulates: short <= int <= long, then char is 1 bytes. However, for most machines, the above table is sufficient. To see how many bytes each type occupies on your machine, use sizeof (type) to view it.

For integer types, C also uses signed and unsigned to decorate them to obtain signed and unsigned numbers, which, by default, are considered to be signed. As mentioned in the computer eye number, the binary representation of signed and unsigned numbers may be the same, and the difference is just the way it is interpreted.

After the various types (that is, after the number of bytes are determined), you can specify the range of the numbers they represent. The following table is a typical range of 32-bit machines for various types of values:

Signed Unsigned
Char -128 ~ 127 0 ~ 255
Short -32768 ~ 32767 0 ~ 65535
Int -2147483648 ~ 2147483647 0 ~ 4294967295
Long -2147483648 ~ 2147483647 0 ~ 4294967295
Long Long -9223372036854775808 ~ 9223372036854775807 0 ~ 18446744073709551615


These threshold values can be viewed by including limits.h header files, such as for various ranges of INT values, which can be printed by Int_max Int_min

Uint_max Uint_min to see.


Let's take a few examples to see the conversion of signed and unsigned between the same types.

1. Cast signed to unsigned:

<span style= "FONT-SIZE:18PX;" > #include <stdio.h>int main (void) {    int a =-1;     unsigned int b = (unsigned int) A;    printf ("A =%d, B =%d\n", A, b);     printf ("A =%u, B =%d\n", A, b);     printf ("A =%d, B =%u\n", A, b);     printf ("A =% #x, b =% #x \ n", A, b);     return 0;} </span>

The results of the operation are as follows:

<span style= "FONT-SIZE:18PX;" >a =-1, b = -1a = 4294967295, b = -1a =-1, b = 4294967295a = 0xFFFFFFFF, B = 0xffffffff</span>

It is obvious that casting a negative number to an unsigned count does not change its bit pattern (binary representation), it is still stored as it was originally , and the result of line fourth proves that, and the first three rows show that Even if you don't do a forced type conversion of signed to unsigned, you can achieve the same result by changing the output format when you print. (Here I start to wonder what is the point of declaring a variable as unsigned?) )


And what about converting a signed positive number to the same type of unsigned numbers?

2. Cast unsigned to signed:

<span style= "FONT-SIZE:18PX;" > #include <stdio.h>int main (void) {    unsigned short a = 32767;    Short B = (short) A;    printf ("A =%u, B =%u\n", A, b);     printf ("A =%u, B =%d\n", A, b);     printf ("A =%d, B =%u\n", A, b);     printf ("A =% #x, b =% #x \ n", A, b);     return 0;} </span>
The results are as follows:

<span style= "FONT-SIZE:18PX;" >a = 32767, b = 32767a = 32767, b = 32767a = 32767, b = 32767a = 0x7fff, B = 0x7fff</span>
As you can see, when a non-negative number is in the range of 0~tmax (TMax represents the maximum value of a certain type of symbol), the result is the same regardless of whether the binary representation is interpreted as signed or unsigned. This is because, in this part of the range of the number, converted to binary, the highest is 0, if interpreted by signed, it is a positive number, the digital part is 7FFF, and according to unsigned interpretation, 07fff = = 7FFF, the results are always the same.

What if the range of positive numbers exceeds Tmax?

<span style= "FONT-SIZE:18PX;" > #include <stdio.h>int main (void) {    unsigned int a = 2147483648u;    int b = (int) A;    printf ("A =%u, B =%d\n", A, b);     printf ("A =%d, B =%u\n", A, b);     printf ("A =% #x, b =% #x \ n", A, b);     return 0;} </span>

Results:

<span style= "FONT-SIZE:18PX;" >a = 2147483648, b = -2147483648a = -2147483648, b = 2147483648a = 0x80000000, B = 0x80000000</span>
As you can see, the underlying binary representation is still consistent, but the interpretation has changed: Since the highest bit is 1, it is a negative number when interpreted as signed, and the interpretation into unsigned is still positive and the result is different.

Note: Because the variables are divided into signed and unsigned, the corresponding constants are also divided into signed and unsigned; The default is signed when the type is not decorated, the corresponding, a constant number defaults to signed, that is, the number of symbols. If you want a constant number to be an unsigned number, add the letter ' U ' or ' u ' at the end.

Data type of C language Note (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.