"C + + Considerations" 1 data types and type conversions

Source: Internet
Author: User

How to choose a type

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

2) Select Long Long when the value exceeds the representation range of Int.

3) do not use char or bool in arithmetic expressions, only use them when storing strings or Boolean values. Because type char is symbolic in some machines, it is unsigned in other machines. If you need to use a small integer, then explicitly specify that it is of type signed char or unsigned char.

4) Perform a floating-point operation with a double, because float is usually not accurate and the calculation cost of double-precision floating-point numbers and single-precision floating-point numbers is comparable. 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 computational time consumption it brings is not to be underestimated.

Type Conversions

1) When we assign an arithmetic value of a non-Boolean type to a Boolean type, the initial value is 0 and the result is false, otherwise the result is true.

2) When we assign a Boolean value to a non-Boolean type, the initial value is false and the result is 0, and the initial value is true and the result is 1.

3) When we assign a floating-point number to an integer type, the myopia is treated. The result value retains the portion before the decimal point of the float.

4) When we assign an unsigned type to a value that exceeds the range it represents, the result is the remainder of the initial value that represents the total number of numeric values for the unsigned type. For example, a 8-bit size unsigned char can represent a value of 0 to 255 intervals, and if we assign a value other than an interval, the actual result is the remainder of the value on 256 modulo. Therefore, the result of assigning 1 to a 8-bit size unsigned char is 255.

5) The result is undefined (undefined) when we assign a value to a signed type that exceeds its range. At this point, the program may continue to work, may crash, or may generate junk data.

the conversion of unsigned numbers

Here's an interesting piece of code to show.

unsigned10;int i = -42;cout<<i + i<<endl;  // 输出:-84cout<<u + i<<endl;  // 输出:4294967264

The first output should be needless to say, for the second, first converts 42 to an unsigned number, converting negative numbers to unsigned numbers is analogous to assigning a negative value directly to an unsigned number, and the result is equal to this negative number plus the touch of unsigned numbers. Is it a bit of a mouthful? What about the code below?

int i = -42;unsigned w=i;cout<<w<<endl;  // 输出为:4294967254

Take a look at the following code:

unsigned u1=42,u2=10;cout<<u2-u1<<endl;  // 输出:4294967264unsigned u=-32;cout<<u<<endl;  // 输出:4294967264  和上面的两段代码一样,都是将-32转换为无符号数

There is one more thing to keep in mind. For example, if you write a descending output of numbers from 10 to 0, considering that the unsigned number is not less than 0, will you write like this?

fori =10i>=0; --i)    cout<<i<<endl;

This is a trap ... Because the output 0 is obvious when I equals 0, the -1,-1 does not satisfy the unsigned number requirement after continuing the self-subtraction operation in the For loop, so it is converted to a valid unsigned number (4294967295) as in the previous example.

The key to solving this problem is to subtract 1 before outputting the variable. In this case, if the loop starts at 10, the output will be 9 to 0 after subtracting 1, so it should be set to 11 when initializing.

And the final truncation point should not be i==0, but should be i>0, so it should be changed:

fori =11i>0;){    --i;    cout<<i<<endl;}

Of course, since the truncation point is greater than 0, this can be used in a while loop.

unsigned i =11;while(i>0)  // 此处写成 while(i) 也是可以的{    --i;    cout<<i<<endl;}

"C + + Considerations" 1 data types and type conversions

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.