Implicit type conversion of C

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 operation, and symbol operation), different types of data operations must be converted to the same type.

Only data can be computed. The arithmetic conversion principle is:

1.1 Integer increase:

For all types smaller than int, including char, signed Char, unsigned char, short, unsigned short,

First, it will be upgraded to the 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 Conversion

Completed.

1.2 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 Modulo calculation for the number of unsigned values. For example, if-1 is assigned to an 8-bit unsigned char, the result is 255.

(-1 to 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 type is greater than that of the left-side Navigation Pane, 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 the printf function is used to output data in a specified format in a program, when the salt data type to be output does not match the output format

If a long data is output in the integer format (% d), it is equivalent to converting the long data into an integer.

(INT) data output; When a character (char) type data is output in integer format, it is equivalent to converting char type to int type output.

Note:: When long data is converted to short data output, the value cannot exceed the value range allowed by short data. Otherwise

Error. For example:

long a=80000;printf("%d",a);

The running result is 14464. Because the maximum allowed value of int type is 32767,80000 exceeds this value, the result is set to 32768.

Is the remainder of the modulo, that is, the remainder operation is performed as follows:

(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.