[C ++ Note] 1. Data Type and type conversion. Note: Data Type

Source: Internet
Author: User

[C ++ Note] 1. Data Type and type conversion. Note: Data Type
How to select a type

1) when the value cannot be negative, select the unsigned type.

2) When the value exceeds the int value range, use long.

3) do not use char or bool in arithmetic expressions. They are used only when strings or boolean values are stored. Because the char type is signed in some machines, and the char type is unsigned in other machines. If you need to use a small integer, specify that its type is signed char or unsigned char.

4) double is used for floating-point calculation. This is because float is usually not accurate enough and the computing cost of double-precision floating-point and single-precision floating-point is almost the same. In fact, for some machines, Double Precision operations are even faster than single precision. The precision provided by long double is not necessary in general, and the computing time consumed by it cannot be underestimated.

Type conversion

1) When we assign a non-Boolean arithmetic value to the boolean type, if the initial value is 0, the result is false. Otherwise, the result is true.

2) When a Boolean value is assigned to a non-boolean type, if the initial value is false, the result is 0. If the initial value is true, the result is 1.

3) When we assign a floating point number to the integer type, we perform myopia. The result value retains the portion before the decimal point in the floating point number.

4) When we assign the unsigned Type a value that exceeds the value range, the result is the remainder of the initial value to the unsigned type that represents the total number of values after the modulo operation. For example, an 8-bit unsigned char can represent a value in the range 0 to 255. If we assign a value other than the range, the actual result is the remainder of the value after the modulo of 256. Therefore, the result of assigning-1 to an 8-bit unsigned char is 255.

5) When we assign a value with a signed type that exceeds the value indicated by it, the result is undefined ). At this time, the program may continue to work, crash, or generate junk data.

Conversion of unsigned numbers

The following shows an interesting piece of code.

Unsigned u = 10; int I =-42; cout <I + I <endl; // output:-84 cout <u + I <endl; // output: 4294967264

The first output should be needless to say. For the second output, first convert-42 to the unsigned number, and convert the negative number to the unsigned number, similar to directly assigning a negative value to the unsigned number, the result is equal to the negative number plus the unsigned number. Is there some interface? What about the following code?

Int I =-42; unsigned w = I; cout <w <endl; // output: 4294967254

Let's take a look at the following code:

Unsigned u1 = 42, u2 = 10; cout <u2-u1 <endl; // output: 4294967264 unsigned u =-32; cout <u <endl; // output: 4294967264 is the same as the above two sections of code, which converts-32 to the unsigned number.

Note the following. For example, when writing a descending output of numbers from 10 to 0, considering that the unsigned number is not less than 0, will you write it like this?

for(unsigned i =10; i>=0; --i)    cout<<i<<endl;

This is a trap ...... When I is equal to 0, the output 0 is obvious. However, after the auto-subtraction operation in the for loop is executed, the-1 and-1 do not meet the requirements of the unsigned number, therefore, as in the previous example, it is converted into a valid unsigned number (4294967295 ).

The key to solving this problem is to subtract 1 before the output variable. In this case, if the loop starts from 10, after subtracting 1, the output result will be 9 to 0. Therefore, set it to 11 during initialization.

In addition, the final truncation point should not be I = 0, But I> 0, so it should be changed as follows:

for(unsigned i =11; i>0;){    --i;    cout<<i<<endl;}

Of course, since the truncation point is greater than 0, this can use the while loop.

Unsigned I = 11; while (I> 0) // you can write while (I) Here {-- I; cout <I <endl ;}

Note: This [C ++ Note] series references the book C ++ Prime and network resources. It is mainly used for review and summarization and is also helpful to readers.

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.