Summary of forced data type conversion in C Language

Source: Internet
Author: User
Tags truncated

● The value of the balanced variable is essentially an integer of eight digits. Therefore, the value range is generally-128 ~ 127. The modifier unsigned can also be added to a char variable. The value range of the unsigned char variable is 0 ~ 255 (some machines treat the char type as the unsighed char type, and the value range is always 0 ~ 255 ).
● If the operation numbers on both sides of an operator are of different types, first convert them to the same type, that is, the lower type to the higher type, and then participate in the operation. The conversion rules are shown in.
Double comment ── float height
Bytes
Long
Bytes
Unsigned
Bytes
Int ── char, short is low
● The horizontal arrow in the figure indicates the necessary conversions. For example, if two float types are involved in the calculation, although they are of the same type, they must be converted to the double type before calculation. The result is also the double type. Vertical arrows indicate the conversion when the number of operations on both sides of the operator is different. For example, if a long data is computed together with an int data, you must first convert the int data to a long data type, then the two are computed and the result is long. All these conversions are automatically performed by the system. You only need to know the type of the result when using them. These conversions can be said to be automatic, but the C language also provides an explicit mechanism to force the conversion type.
● When the conversion of lower-type data to higher-type data is generally only in form, but does not affect the actual content of the data, some data may be lost when the conversion from a higher type to a lower type.

Type conversion in value assignment

If the operation object types on both sides of the value assignment operator are different, type conversion will occur. The conversion rule is to convert the type of the expression on the right of the value assignment operator to the type of the variable on the left. The specific conversion is as follows:
(1) floating point type and integer type
● When you convert a floating point number (single-and double-precision) to an integer, the fractional part of the floating point number is discarded and only the integer part is retained.
Assign the integer value to the floating point variable. The value remains unchanged. Only the floating point type is changed to the floating point type. That is, the decimal point is followed by several zeros. Note: The type conversion when values are assigned is actually mandatory.
(2) Single and Double Precision Floating Point
● Because the floating point values in the C language are always expressed in double precision, float data only adds 0 at the end and is extended to doub1e for calculation, and then directly assigns values. When data of doub1e type is converted to float type, it is achieved by the number of truncated tails. Rounding is required before truncation.
(3) Char and INT types
● When an int value is assigned to a char variable, only the minimum 8 bits are retained, and the upper part is discarded.
● Some compilation operations when Chr-type values are assigned to int-type variables Program No matter the value size, it is processed as a positive number, while in other compilation programs, if the char type data value is greater than 127, it is processed as a negative number. For the user, if the original char data is positive, the conversion is still positive. If the original char value can be positive or negative, the original value remains unchanged after conversion, the internal representation of data is different.
(4) int and 1ong
● When long data is assigned to an int type variable, the 16-bit lower value is sent to the int type variable, and the 16-bit higher value is truncated. (Assume that the int type occupies two bytes ).
When int type data is sent to long type variables, the external value remains unchanged, while the internal form changes.
(5) unsigned integer
● When an unsigned data is assigned to an integer variable occupying the same length of storage unit (for example, unsigned → int, unsigned long → long, unsigned short → short), the original value is assigned, the internal storage method remains unchanged, but the external value may change.
● When a non-unsigned integer data is assigned to an unsigned variable of the same length, the internal storage format remains unchanged, but the external representation is always unsigned.
/* Example: Value assignment operator */
Main ()
{Unsigned A, B;
Int I, J;
A = "65535 ";
I = "-1 ";
J = "";
B = "I ";
Printf ("(unsigned) % u → (INT) % d/N", A, J );
Printf ("(INT) % D → (unsigned) % u/N", I, B );
}
The running result is:
(Unsigned) 65535 → (INT)-1
(INT)-1 → (unsigned) 65535

● data in the computer is represented by a complement code. The highest bit of the int type is the sign bit. If it is 1, it indicates a negative value. If it is 0, it indicates a positive value. If the value of an unsigned number is less than 32768, the maximum bit is 0. After being assigned to the int type variable, a positive value is obtained. If the unsigned number is greater than or equal to 32768, the maximum bit is 1. After being assigned to an integer variable, a negative integer value is obtained. Otherwise, when a negative integer is assigned to an unsigned variable, the unsigned value is a value greater than 32768.
● in C language, the type conversion format may make people feel inaccurate and not strict, because no matter what the expression value is, the system automatically converts it to the type of the Left variable of the value assignment operator.
● the data may be different after the change, and errors may occur without attention. This is indeed a shortcoming and has been criticized by many people. But it should not be forgotten that the C language was originally designed to replace the assembly language, so the type conversion is relatively random. Of course, it is a good habit to use forced type conversion. In this way, you can at least see what you want from the program.

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.