C Language Automatic type conversion

Source: Internet
Author: User

Automatic conversions Follow these rules:

1) If the type of the participating operands is different, it is converted to the same type first and then the operation is performed.

2) The conversion is carried out in the direction of increasing data length to ensure that the accuracy is not reduced. (Eg:int and long operations, the int is converted into a long type before the operation.) )

A. If two types of bytes are different, convert to a type with a high number of bytes

B. If the two types have the same number of bytes, and one is signed, and one is unsigned, it is converted to an unsigned type

3) All floating-point operations are carried out in double precision, even if only the expression of float single-precision operation, it must first be converted to double type, and then the operation.

4) Char and short participate in the operation, you must first convert to int type.

5) In an assignment operation, the type of the amount on the right of the assignment number is converted to the type of the left quantity, not the same as the data type of the value on both sides. If the right amount of data type length is left long, a portion of the data is lost, which reduces precision and the missing part is rounded forward. (Align to the left of the assignment)

Implicit conversions

Implicit type conversions are divided into three types, i.e.Arithmetic ConversionsAssignment ConversionsAndOutput Conversion
1. Arithmetic Conversions
For arithmetic operations (+ 、-、 *, \, &, and symbolic Operations), different types of strokes must be converted to the same type of data in order to operate, and the arithmetic conversion principle is:
In the case of an operation, the longest type in the expression is dominated, and the other type bits are converted to that type, such as:
(1) If there is a double or float in the operand, the other type of data is converted to a double type for operation.
(2) If the longest type in the operand is long. The other types are converted to a long type number.
(3) If the longest type in the operand is int, the char type is also converted to type int for operation.arithmetic conversions are done automatically during the operation.
2. Assignment Conversions
When you perform an assignment, the data type to the right of the assignment operator must be converted to the type on the left of the assignment number, or truncated or rounded if the right data type is longer than the left.
The following example illustrates:
Char ch;
int I,result;
float F;
Double D;
result=ch/i+ (f*d-i);
(1) First calculate ch/i,ch→int type, Ch/i→int type.
(2) then calculate f*d-i, because the longest type is double type, so f→double type, i→double type, f*d-i→double type.
(3) (ch/i) and (f*d-i) to add the operation, because the f*d-i is a double type, so ch/i→double type, ch/i+ (f*d-i) →double type.
(4) Because result is an int type, ch/i+ (f*d-i) →double→int, that is, truncation and rounding, the final value is an integer type.
3. Output Conversion
When you output data in a program using the printf function in the specified format, the data type and output that you want to outputformat does not match, type conversions are performed automatically, such as when a long data is output with an integer format (%d), which is equivalent to converting a long type to an integer (int) data output, and the equivalent of converting a char type to an int output when a character (char) type is output in an integer format.
Note: When the longer data is converted to a short data output, its value cannot exceed the range of values allowed by the short data, otherwise the conversion will be error-prone. Such as:
Long a=80000;
printf ("%d", a);
The result of the operation is 14464, because the maximum allowable value of type int is 32767,80000 exceeds this value, so the result takes 32768 as the remainder of the modulus, that is, take the following remainder operation:
(80000-32768) -32768=14464;
Errors often occur when the output data type does not match the output format, such as:
int d=9;
printf ("%f", D);
Or
float c=3.2;
printf ("%d", c);
will produce the wrong result.

The same sentence or expression if you use more than one type of variable and constant (type blending), C automatically converts them to the same type. The following are the basic rules for automatic type conversions:

1. In an expression, the values of char and short type, whether signed or unsigned, are automatically converted to int or unsigned int (if the size of short is the same as int, the range of unsigned short is greater than int, in which case Unsigned short is converted to unsigned int). Because they are converted to a type that represents a larger range, the conversion is referred to as an "upgrade (promotion)".

2. Rank the various data types in order from highest to lowest: long double, double, float, unsigned long long, long long, unsigned long, long, unsigned int, and Int. There is a small exception, if long and int are the same size, then the unsigned int should be above the long. Char and short do not appear in this rank list because they should have been upgraded to int or unsigned int.

3. In any operation involving two data types, the lower rank types are converted to higher-level types.

4. In the assignment statement, = the value on the right is the first to convert the data type of the right value to the type of the left variable before giving = to the left variable. That is, the left-hand variable is the data type, and the value on the right is converted to the value of what data type. This process may cause the type of value on the right to be upgraded, or it may cause its type demotion (demotion). The so-called "demotion" refers to a higher-level type being converted to a lower-level type.

5. When passed as a parameter to a function, char and short are converted to int,float and converted to double. This automatic upgrade can be avoided by using function prototypes.

Written in the following words:

This is my first blog, now is still a hard to learn the weak dish, the above article is not original, I will continue to post the summary of others and their own comments, (in fact, mainly for their own review of convenience, but also hope to help others and I are struggling to make progress of small partners ~) hope that someday I can write original.

Original link: http://www.cnblogs.com/hoys/archive/2012/04/09/2438718.html

C Language Automatic type conversion

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.