C implicit type conversion

Source: Internet
Author: User
Implicit type conversion of C

Implicit conversions include arithmetic conversions, value assignment conversions, and output conversions.

1. Arithmetic Conversion

When performing arithmetic operations (addition, subtraction, multiplication, division, remainder operations, and symbolic operations), different types of data operations must be converted to the same type of data for calculation. The principle of arithmetic conversion is:

Integer increase: For all types smaller than int, including char, signed Char, unsigned char, short, and unsigned short, it is first upgraded to int type.

During the operation, the longest type in the expression is used as the primary parameter, and all data of other types are converted to this type, for example:

(1) If the operation number contains the double or float type, all other types of data are converted to the double type for calculation.

(2) If the longest type of the Operation number is long, the other types are converted to long.

(3) If the longest type of the Operation number is int, the char type is also converted to int type for calculation. Arithmetic conversions are automatically completed during the operation.

Note:

Contains long and unsigned int, 32-bit machines are 4 bytes, so they are all converted to unsigned long.

Including signed and unsigned int,Signed is converted to unsigned.If int happens to be a negative number, the result is the result of modulus of the number of unsigned values. For example, if-1 is assigned to an 8-bit unsigned char, the result is 255 (the value after modulo of-1 for 256 ). Unsigned char value range: 0 ~ 255.

2. Value assignment Conversion

During the value assignment operation, the data type on the right of the value assignment operator must be converted to the type on the left of the value assignment number. If the length of the data type on the right is greater than that on the left, truncation or rounding is required.

An example is provided below:

      Char ch;

      Int I, result;

      Float F;

      Double D;

      Result = CH/I + (F * d-I );

(1) Calculate CH/I, CH → int, CH/I → Int.

(2) Calculate f * d-I. Because the longest type is double, F → double, I → double, f * d-I → double.

(3) (CH/I) and (F * d-I) are used for addition. Because f * d-I is of the double type, CH/I → double type, CH/I + (F * d-I) → double type.

(4) because the result is of the int type, CH/I + (F * d-I) → double → int indicates truncation and rounding, and the final value is an integer.

3. Output Conversion

When data is output in a specified format using the printf function in the program, the type conversion is automatically performed when the salt data type to be output does not match the output format, for example, if a long data is output in integer format (% d), it is equivalent to converting the long data into an integer (INT) data output; one character (char) when data is output in integer format, it is equivalent to converting char type to int type output.

Note: When a long data type is converted to a short data output, its value cannot exceed the value range allowed by the short data type. Otherwise, an error occurs during conversion. For example:

      Long A = 80000;

      Printf ("% d", );

The running result is 14464. Because the maximum allowed value of int type is 32767,80000 exceeds this value, the result is the remainder of the model of 32768, that is, the following remainder operation is performed:

      (80000-32768)-32768 = 14464;

Errors often occur when the output data type is inconsistent with the output format, such:

      Int d = 9;

      Printf ("% F", d );

Or

      Float c = 3.2;

      Printf ("% d", C );

An error is returned.

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.