Presentation range for C-language data types

Source: Internet
Author: User

The basic data types in the 1, C, and C + + languages are: Character type (char), shape (short, int, long), float (float, double)

Type

Number of bytes

Type

Number of bytes

Char

1

Short

2

int

2 or 4

long

4

Float

4

Double

8

At the same time, character and shape can continue to be divided into signed and unsigned, by default, is signed, if you want to use the unsigned, if it is signed, then the highest level will represent the symbol bit, the maximum bit is 0 is a positive number, the highest bit is 1 is a negative number, Therefore, depending on this, you can get the range of data represented by different types in both signed and unsigned cases:

Type

Range

Type

Range

(signed ) char

-128 -- 127

unsigned char

0 --255

(Signed) short

-32768 --32767

unsigned short

0 --65535

(signed) int

-32768 --32767

0 --65535

(signed) long

-214748364 8 --2147483647

unsigend long

0 --4294967295

Float

-3.4*10-38--3.4*1038

Double

-1.7*10-308--1.7*10308

2. For character types and shaping, it is particularly important to note the scope of the representation, if the operation results in a process beyond its scope, such as the following code:
    1. int main ()
    2. {
    3. Signed char ch = 127;
    4. ch + = 1;
    5. printf ("%d\n", ch);
    6. return 0;
    7. }
The output is:-128, note that the value given when defining CH is 127, is already the maximum number that char can represent, and the end result of the 128 is this: the first step: first, 127 in the computer with a binary representation as: 01111111; Step two: 01111111 + 00000001 = 10000000; third step: Because CH is signed, when the highest bit is 1 o'clock, the expression is negative, and the negative number of the computer is to use the complement to store, recall calculation of negative complement method (first take negative value, then the binary system, the binary inversion, in the inverse of the value plus 1, That is, the complement of negative numbers); Fourth step: So according to the reverse step of the complement, we come to find the most primitive negative, because the subject of the complement is 10000000, first 10000000-00000001 = 01111111, and then 01111111 is reversed to 10000000, 10000000 = 128, because it is a negative number, that is, CH is-128. Let's look at the following code:
    1. int main ()
    2. {
    3. unsigned char ch = 255;
    4. ch + = 1;
    5. printf ("%d\n", ch);
    6. return 0;
    7. }
The output is: 0; the analysis is: first ch = 255 is promoted to plastic, the computer is stored as: (000 ...). ) 11111111, then (000 ....) ) 11111111+1 = (000 ... ) 0001 00000000; then intercept the last eight-bit binary to CH, since the last eight bits are all 0, so ch = 0. Similarly, we can also analyze the result when we encounter the above situation in shaping int short, long, and so on.

Presentation range for C-language data types

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.