Analysis on the value range of C ++ Data Types

Source: Internet
Author: User

C ++ data types are used to store values. They have names and data types. The data types of variables determine how to store the bits representing these values in the computer's memory, the maximum length allowed by the variable name varies with the compiler.

In C ++, you must declare the variable type before using the variable: int x1 = 1; int x = 1000; float y = 3.14; long z = 457000, the compiler can perform a type check to ensure that the program runs smoothly. Improper use of data types may cause compilation errors or warnings for Analysis and Correction before running.

Some data types are signed and unsigned. Signed) data types can contain positive and negative numbers, while unsigned data types can only contain positive numbers. Table 1.1 lists the data types, memory size, and possible value ranges in C ++.

Data Type byte value range:

 
 
  1. Char 1-128 to 126
  2.  
  3. Unsigned char 1 0 to 255
  4.  
  5. Short 2-32,768 to 32,767
  6.  
  7. Unsigned short 2 0 to 65,535
  8.  
  9. Long 4-2,147,483,648 to 2,147,483,648
  10.  
  11. Unsigned long 4 0 to 4,294,967,295
  12.  
  13. Int 4 is the same as long
  14.  
  15. Unsigned int 4 and unsigned long
  16.  
  17. Float 4 1.2E-38 to 3.4E381
  18.  
  19. Double 8 2.2E-308 to 1.8E3082
  20.  
  21. Bool 1 true or false

From the table above, we can see that int and long are the same. So why do C ++ need to differentiate these two data types? This is actually a legacy issue. In a 16-bit programming environment, int requires 2 bytes, and long requires 4 bytes. In a 32-bit programming environment, both types of data are stored in four bytes.

C ++ only generates 32-bit programs for data types, so int and long are the same. In C ++ Builder and BorLand C ++ 5.0, Bool is a real data type. Some C ++ compilers have the Bool keyword, so Bool is not a real data type.

Sometimes Bool is only a typedef, so that Bool is equivalent to int. Typedef actually creates an alias to enable the compiler to draw an equal sign between one symbol and another. The syntax of typedef is as follows: typedef int Bool; this tells the compiler that Bool is the alias of int. It indicates that only the double and float data types use floating point numbers with decimal points ).

Other data types only involve integer values. Although the integer data type can also specify a number with a decimal point, the fractional part is discarded and only the integer part is assigned to the integer variable. For example: int x = 3.75; the value of x is 3. Note that this integer is not rounded to the nearest decimal point.

By the way, floating point numbers are rarely used in most Windows programs. C ++ data types can be converted between different data types when necessary. Example: short result; long num1 = 200; long num2 = 200; result = num1 * num2; here I want to assign the product of two long integers to a short integer. Although this formula is mixed with two data types, C ++ can be used for conversion.

What will happen to the calculation result? The result will surprise you. It is 25536, which is the result of the bypass (wrop. As shown in Table 1.1, the maximum value of a short integer is 32767. What if I add 1 to the maximum value? The result is 32768. This is actually the same as the car's mileage from 99999 to 00000. To illustrate this, enter and run the program contained in listing 1.3.

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.