C + + type conversions

Source: Internet
Author: User

1, non-Boolean variable assignment to Boolean variable: 0 is False, not 0 is true

2. Boolean variable assigned to non-Boolean variable: false is 0,true to 1

3. Floating-point numbers are assigned to integers: Only the integer portion of the floating-point number is retained, and no overflow is discussed

4, integer assigned to floating point number: The fractional part of an integer is divided into 0, the overflow situation is not discussed temporarily

5, assign a number to unsigned type, and overflow: first do the modulo operation, and then assign the value

unsigned int i;

i =-8;

Then i = 2^32-8

6. Assign a negative number to the unsigned type: first do the modulo operation, then assign the value

7, negative numbers and unsigned to do addition arithmetic operations, the first negative to the unsigned number (first modulo, then assign value), and then do the arithmetic operation

int main ()

{

unsigned int u = 10;

int i =-42;

Int J;

cout<< "U + i =" <<u+i<<endl;

cin>>j;

return 0;

}

The result is: (2^32-42) +10=4294967264

8, the value of the small unsigned number and the value of a large unsigned subtraction arithmetic operation, first do subtraction, and then the negative result into an unsigned number

int main ()

{

unsigned int u = 42;

unsigned int i = 10;

Int J;

cout<< "i-u =" <<i-u<<endl;

cin>>j;

return 0;

}

The result is: (10-42) +2^32 =4294967264

C + + type conversions

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.