C language, data type conversion

Source: Internet
Author: User

Implicit type-conversion rules:
C language automatic conversion of different types of behavior is called implicit type conversion, the basic principle of conversion is:The low-precision type converts to a high-precision type, specifically:
unsigned, unsigned int--------long long---unsigned long long Gt Long Double

Note that the order above does not necessarily apply to your machine, such as when int and long have the same word length, the precision of unsigned int is higher than the precision of long (in fact most compilers for 32 machines are). It is also important to note that Char and short are not written on, because they can be promoted to an int and may be promoted to unsigned int.
Improving the accuracy of your data is usually a smooth, non-damaging process, but reducing the accuracy of your data can lead to real problems. The reason is simple: a lower-precision type may not be large enough to hold a complete data with higher accuracy. A 1-byte char variable can hold an integer of 101 but cannot hold an integer 12345. When converting floating-point type data to integer types, they are truncated or rounded.
To force type conversions:
Usually we should avoid automatic type conversion, when we need to manually specify an exact data type, we can use the coercion type conversion mechanism to achieve our purpose, the use of the method is very simple, the need to cast a type of variable or constant preceded by (type), for example (double) I; That is, the variable i is cast to double type.
---------------------------------------------------------------------------------------------
Why does an int type negative number become a large integer when it is converted to a unsigned int? This
This involves the storage of integers, the binary of integers has the original code, the inverse code, the complement,
Its transformation in memory is that the shaping data is present in memory in the complement of 2 decimal digits,
Positive numbers of the original code, anti-code, and complement are the same, and negative numbers are different,-3 of the original code is 1000 0000 0000 0011 (The first 1 on the left is the match bit, 1 is negative, 0 is positive), the inverse code is 1111 1111 1111 1100 (the original code except the sign bit is reversed); the complement is 1111 1111 1111 1101 (Inverse code + 1), the output is also in the complement, so the final twos complement value is the type converted value, that is, b=65533 (this is on the TC2.0 (16 bit)).

——————————————————————————————

To get to the point, first look at a simple code:

  1 #include <stdio.h>  2   3 int array[] = {1, 2, 3, 4, 5, 6, 7};  4 #define Total_elements (sizeof (array)/sizeof (ARRAY[0))  5   6 int main ()  7 {  8     int i =-1;  9     int x, ten     if (i <= total_elements-2) {         x = array[i + 1];         printf ("x =%d.\n", x); 1 4     }     printf ("Now i =%d.\n", total_elements);     0 return; 19}

Execution Result:

[Email protected]:~/c_language$./a.out

Now i = 7.

Isn't it weird? Why not line13 x =?.

That is true. This small example has three points worth noting:

1.sizeof () is an operator that returns a type that is unsigned, that is, a non-negative number.

The 2.if statement is judged between singned int and unsigned int, and the value of the original type is converted to the int type if all the values of the primitive types are represented by the int type, based on the integer promotion rule of the C language. Otherwise, it will be converted to the unsigned int type. So I will be upgraded to an unsigned type at this point.

3.i = 1 is promoted to unsigned, what is the value? This is done using an integer conversion rule: K&R, the method of converting any integer to a specified number of unsigned number types is to find the smallest non-negative value that is equal to this integer by adding 1 to the maximum that the unsigned number type can represent. Listen to very awkward, in fact, as long as the original integer to know the binary expression method, and then the type to be converted to parse, you get the value of the upgrade. For example-1, negative numbers in the computer with a complement, 0xFFFFFFFF, that upgrade to unsigned, the value is 0xFFFFFFFF, obviously larger than total_elements (7).

————————————————————————————

Written questions-C language type conversion (2012.3.23 interview)

C Language Supplements (i): Integral type Lifting

C language, data 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.