Coercion type conversions in assignments

Source: Internet
Author: User

Type conversions in assignments when the operand types are different on either side of the assignment operator, a type conversion will occur, and the rule of conversion is to convert the type of the expression on the right side of the assignment operator to the type of the left variable. The specific conversions are as follows: (1)   floating point and integer when converting floating point numbers (single and double precision) to integers, the fractional part of the floating-point number (which is discarded directly and the compiler does not automatically round) is discarded, leaving only the integer part. Assigns an integer value to a floating-point variable with a constant value, changing the form to a floating-point form only, which is a number of 0 after the decimal point. Note: The type conversion is actually mandatory when assigning a value. (2) Single-and double-precision floating-point type because the floating-point value in C is always represented by double precision, the float type data is only extended at the tail plus 0 for the double type data, and then directly assigns the value. When the double data is converted to float type, it is achieved by truncating the mantissa and rounding is done before truncation. (3) when the char type and int type int values are assigned to a char variable, only the lowest 8 bits are retained, and the high part is discarded. When a char-type value is assigned to an int variable, some compilers handle it regardless of their value size, while others are processed as negative numbers if the char data value is greater than 127 when the conversion occurs. For the user, if the original char data takes positive values, the conversion is still positive, and if the original char value can be positive, the conversion will still remain the original value, but the internal representation of the data is different. (4) when the int type and long type Long data are assigned to the int variable, the low 16-bit value is given to the int variable, and the high 16-bit truncation is discarded. (This assumes that the int type is two bytes). When you send int data to a long variable, its external values remain unchanged, and the internal form changes. (5)   unsigned integers assign a unsigned data to an integer variable that occupies the same length of storage unit (e.g. Unsigned→int, unsigned long→long,unsigned short→short), the original value is assigned, Internal storage does not change, but external values can change. When you assign a non-unsigned integer data to a unsigned variable of the same length, the internal storage form is unchanged, but the external representation is always unsigned. /* Example: assignment operator Example */main () {unsigned a,b;int i,j;a=65535;i=-1;j=a;b=i;printf ("(unsigned)%u→ (int)%d\n", a,j);p rintf ("(int) %d→ (unsigned)%u\n ", i,b);} Run Result: (unsigned) 65535→ (int)-1 (int) -1→ (unsigned) 65535 the data in the computer is in the complement representation, the highest bit of the int is the sign bit, 1 indicates a negative value, and 0 indicates a positive value. If the value of an unsigned number is less than 32768, the highest bit is 0, and after the int variable is assigned, the positive values are obtained. If the unsigned number is greater than or equal to 32768, the highest bit is 1, and a negative integer value is given after the integer variable is assigned. Conversely, when a negative integer is assigned to the unsigned type variable, the resulting unsigned value is a value greater than 32768. The type conversion form of C, when assigned, can make people feel less sophisticated and less restrictive because, regardless of the value of an expression, the system automatically converts it to the type of the left variable of the assignment operator. The data may be different after the change, and may lead to errors when not being noticed. This is indeed a shortcoming and has been criticized by many people. But it should not be forgotten that: C language was originally designed to replace assembly language, so the type transformation is more casual. Of course, it is a good habit to use coercion type conversions, so that at least you can see what you want to do from the program.

Coercion type conversions in assignments

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.