C Language -- Data Type -- value range, -- Data Type Value

Source: Internet
Author: User

C Language -- Data Type -- value range, -- Data Type Value

 

 

 

Basic Data Type of C language: Random (char) INTEGER (short, int, long) floating point (float, double)

 

Note: The following types of bytes are generally different on different platforms. You can use the sizeof keyword to test the number of bytes.

 

Type Bytes Type Bytes
Char 1 Short 2
Int 2 (16-bit system) or 4 (32-bit System) Long 4
Float 4 Double 8

 

 

 

 

 

 

 

 

 

 

 

 

Type Range Type Range
(Signed) char -128 ~~ 127 Unsigned char 0 ~~ 255
(Signed) short -32768 ~~ 32767 Unsigned short 0 ~~ 65535
(Signed) int -32768 ~~ 32767 Unsigned int 0 ~~ 65535
(Signed) long

-2147483648 ~~ 2147483647

Unsigned long

0 ~~ 4294967295

Float

-3.4*10-38 ~~ 3.4*1038

Double

-1.7*10-308 ~~ 1.7*10308

 

 

 

 

 

 

 

 

 

 

 

 

 

1. the operation will cause the overflow type/Integer to its value range.

Eg.

int main() {     signed char ch = 127;    ch += 1;    printf("%d\n", ch);    return 0; }

The output result is-128. Note that the value given when ch is defined is 127, which is already the maximum number that char can represent. The final result of-128 is as follows:

Step 1: first, 127 is expressed as 01111111 in binary format in the computer;

Step 2: 01111111 + 00000001 = 10000000;

Step 3: Because ch is signed, when the maximum bit is 1, it indicates that it is a negative number, and the negative computer uses the complement code for storage, the method for calculating the negative complement code (first, take the absolute value of the negative number, then obtain the binary value, get the inverse of the binary value, and Add 1 to the value after the inverse value, that is, the complement code of the negative number );

Step 4: Calculate the original negative number based on the reverse step of the complement code. Because the complement code in this question is 10000000, first 10000000-00000001 = 01111111, then, convert 01111111 to 128, and then convert 128 to. Because it is a negative number, that is, ch is.

 

Eg.

int main() {     unsigned char ch = 255;    ch += 1;    printf("%d\n", ch);    return 0; }

The output result is: 0;

The analysis is as follows: first, ch = 255 is upgraded to an integer, and the storage on the computer is: (000 ....) 11111111, and then (000 ....) 11111111 + 1 = (000 ...) 0001 00000000; then extract the last eight binary values to ch. Since the last eight digits are all 0, ch = 0.

Likewise, we can analyze the result in the case of integer int short and long.

 

2. Use the preprocessing command # define to declare a constant to indicate how many seconds are there in a year?

#define SECOND_PER_YEAR (60*60*24*365)UL

 

① # Basic knowledge of define syntax (for example, it cannot end with a semicolon or use of parentheses)

All macro definitions, enumeration constants, and read-only variables are named in uppercase letters and words are separated by underscores.

Const int MAX_LENGTH = 100; // This is not a constant, but a read-only variable. For details, refer

 

# Define FILE_PATH "/usr/tmp"

② Writing out how you calculate the number of seconds in a year rather than calculating the actual value will be more valuable

③ Realize that this expression will overflow an int number, so it is best to use a long integer, then you will add points for yourself.

 

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.