C++primer Quick Tour of notes

Source: Internet
Author: User

1. Type conversion
  
 
  1. bool b = 42; // _b is true_
  2. int i = b; // _i has value 1_
  3. i = 3.14; // _i has value 3_
  4. double pi = i; // _pi has value 3.0_
  5. unsigned char c = -1; // _assuming 8-bit chars_, _c has value 255_
  6. signed char c2 = 256; // _assuming 8-bit chars_, _the value of c2 is__undefined_

The range of values that a type can represent determines the process of conversion:

    • 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.
    • When we assign a Boolean value to a non-Boolean type, the initial value is false and the result is O, and the initial value is
      True, the result is 1.
    • When we assign a floating-point number to an integer type, the approximation is processed. The resulting value will retain only the floating-point
      The part before the decimal point.
    • When we assign an integer value to a floating-point type, the fractional part is recorded as 0. If the integer occupies a space that is super-
      There may be a loss of precision over the capacity of floating-point types.
    • When we assign an unsigned type to a value that exceeds the range it represents, the result is an initial value pair of unsigned type table
      Shows the remainder of the total number of values after modulo. For example, a 8-bit-size unsigned char can represent a value within a range of 0 to 255, 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.
    • The result is undefined (undefined) when we assign a symbol type and a value that exceeds the range it represents.
      At this point the process may continue to Guiren, may crash, or may generate garbage data.


From for notes (Wiz)

C++primer Quick Tour of notes

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.