C ++ Data Types

Source: Internet
Author: User

C ++ data types define how the compiler stores information in memory. In some programming languages, you can assign any numeric type to a variable, next we will analyze and learn the C ++ data types, hoping to bring you what you want.

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 ++.

 
 
  1. 1: #include <iostream.h>   
  2.  
  3. 2: #include <conio.h>   
  4.  
  5. 3: #pragma hdrstop   
  6.  
  7. 4:   
  8.  
  9. 5: int main(int argc,char **argv)   
  10.  
  11. 6: {   
  12.  
  13. 7:short x = 32767;   
  14.  
  15. 8:cout << " x = " << x << endl;   
  16.  
  17. 9:x++;   
  18.  
  19. 10: cout << " x = " << x << endl;   
  20.  
  21. 11: getch();   
  22.  
  23. 12: return 0;   
  24.  
  25. 13: }  

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 ++ Builder only generates 32-bit programs, 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.

This statement is automatically added when the C ++ data type generates a new console application. This is not necessary in the program you are using, so it is omitted in the code list. The program running results are consistent regardless of whether this statement exists. Analysis output: x = 32767 x = 32768 if int data type is used, this problem does not occur.

Because the value range of the C ++ data type is between 2 billion positive values, there is generally no detour problem. However, the program may be slightly larger, because int requires 4-byte storage, while short only needs 2-byte storage. For most applications, this difference is not significant. The preceding section describes automatic type conversion. Sometimes C ++ Cannot be converted. In this case, a compilation error may occur in the compiler, that is, Cannot convert from x to y (Cannot be converted from x to Y ).

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

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.