C Language Type casting

Source: Internet
Author: User
Tags type casting

C language Type castingCoercion of type conversions is accomplished through type conversion operations. Its general form is: (type specifier) (an expression) whose function is to cast the result of an expression into a type that is represented by the represented descriptor. Directory

1 Basic Introduction

2 precautions

1Basic Introductory edit Coercion of type conversions is accomplished through type conversion operations. Its general form is: (type specifier) (an expression) whose function is to cast the result of an expression into a type that is represented by the represented descriptor. Automatic conversions occur when the source and destination types are compatible, and when the target type is wider than the source type, one type-to-another conversion occurs. For example: (float) A converts a to a real type, (int) (X+Y) Converts the result of the x+y to an integral type. You should be aware of the following issues when using casts:2 notes edit 1. Both the type specifier and the expression must be enclosed in parentheses (a single variable can be without parentheses), such as (int) (X+Y) written (int) X+y is converted to the int type and then added to Y. 2. Whether it is a cast or an automatic conversion, it is only a temporary conversion of the data length of the variable for the purpose of this operation, without altering the type defined for the variable at the time of the data description. Example 1:main () {float f=5.75;printf ("f=%d,f=%f\n", (int) f,f);} f=5,f=5.750000 the float f cast to int F float f=5.75;printf ("(int) f=%d,f=%f\n", (int) f,f); This example shows that although F is coerced to int, it only works in the operation, it is temporary, and the type of F itself does not change. Therefore, the value of (int) F is 5 (the decimal is deleted) and the value of F is still 5.75. Example 2: For example, we can (int) ' A ', so the converted result is an ASCII value of a, because that block of memory is the number that is stored, just a different form of use. Knowing the above principle, we can convert any data type, but the result of the conversion may not be the result of your imagination, what is the result of example (int) ' 9 '? Not 9 but 0x39. What's the output of a highly advanced printf ("%d", ' 12 ')? The correct answer is 12594, because printf ("%d", ' 12 ') prints the contents of the memory address where 12 is stored, that is, the ASCII value 2 is stored in the low, 1 is in the high-level address, 0x32 is 2 ASCII, and 0x31 is 1 ASCII code, So is 0x3132, converted into 10 binary is 12594! The value of the   character variable is essentially a 8-bit integer value, so the range of values is generally -128~127,char variable or modifier unsigned, unsigned char The value range of the type variable is 0~255 (some machines treat the char type as unsighed char, and the value range is always 0~255). If the operand types are different on either side of an operator, first convert it to the same type, that is, the lower type is converted to a higher type, and then participate in the operation, as shown in the conversion rule. Double←──float high ↑long↑unsigned↑int←──char,short The horizontal arrows represent the necessary conversions, such as two float types, although they are of the same type, but must be converted to a double type before the operation. The result is also double type. The vertical arrows represent conversions when the operands on either side of the operator are of different types, such asLong data with an int type of data, you need to convert the int data to a long type, and then the two operations, the result is a long type. All of these transformations are automated by the system, and you only need to know the type of the result when you use it. These conversions can be said to be automatic, and the C language, of course, provides a mechanism for casting types in an explicit form. When a lower type of data is converted to a higher type, it is generally only a change in form, without affecting the substance of the data, while a higher type of data is converted to a lower type when there may be some data loss. 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 you convert floating point numbers (single-double precision) to integers, the fractional part of the floating-point number is discarded, leaving only the integer portion. 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 assigning 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);} The result is: (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 is 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.    This article originates from Http://baike.baidu.com/link?url=7GWYR-mFbdN_ Jfcctfqozwju6cxd-9p5nlvlkrk4xqzfhyhkvrewhdz0k1ppntuh0ns10wh0bcjfuf4vaqx8uk

C language Type casting

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.