C data type conversion


When the computer performs an arithmetic operation, each type requires operands have the same size (number of bits stored) and storage, will not char type (1 byte) data type int (2, 4 or 8 bytes) of data directly involved operation; due to the different ways of storage, nor can an int and double-data directly involved in computing.

However, because of the flexibility of the C language, in an expression or a statement, allowing different types of data mixing operation.

Mechanical flexibility of the C computer language is a contradiction, such as bad treatment, will produce erroneous results. May be implicitly performed automatically for certain types of converters compiler, without programmer intervention, called the conversion to automatic conversion ; and some type of conversion requires the programmer to explicitly specify this type of conversion is referred to cast .

An automatic type conversion

A mixing operation between different types of expression occurs, lower to higher conversion type automatic type.

Differences between different types of data that the range and accuracy of the data, in general, the larger the range of the data, the higher the accuracy, the more the type "High."

Integer type level from low to high as follows:

signed char->unsigned char->short->unsigned short->int->unsigned int->long->unsigned long

Float level from low to high as follows:

float->double

We float floating-point type is deprecated, so do not mention it.

1, there is no floating-point operand data

When the char, unsigned char, short, or unsigned short occurs in the computation involved in the expression, generally automatically converted to type int.

unsigned int int when mixing operation, int type automatically converted to unsigned int.

int, unsigned int and long time of mixing operation, are converted to type long.

2, the floating-point operands Data

When the number of floating-point data contained in the operation, both operands are converted to double.

E.g:

int ii=100;
double dd=200.5;
ii+dd;

The above-described arithmetic expressions dd operand as a double, it is first converted to a double float ii participation after operation, double precision floating point computation result is 300.5.

3, both types of symbol assignment operation inconsistent

When the right value is the assignment operator (which may be constant, variable or expression) is inconsistent with the type of value type left and the right value type lifting / lowering to the left of the value type. E.g:

double dd;
dd=10;  // 右值为双精度,左值为整数

Since the lvalue dd double-precision floating-point, so put the right value after the integer constant 10 promoted to double precision floating point, and then assigned to dd, not only do not lose accuracy but improved accuracy.

int ii;
ii=10.5;   // 右值10.5为双精度,左值为整型

10.5 rvalue double reduction integer value to the left, i.e. the fractional part is discarded 10.5, 10 assigned to integer variables II, this situation will lose precision.

4, the left and right values ​​beyond the range of a value type

Worse case, the range of the assignment operator beyond the left and right values ​​represent the range of values ​​of the type, the value of which will cut the right, the value assigned to the left. The results may be meaningless. E.g:

char c;             //  char占8位,取值范围是-128-127。
c=1025;             //  整数1025 对应二进制形式是100 0000 0001,超出了8位。
printf("%d",c) ;  //  以十进制输出c的值

The output is 1, since taking only the lower 8 bits 1025 0000 0001 (value of 1) assigned to the character variable C, to obtain a value meaningless.

Second, the cast

Although automatic conversion without manual intervention, easy to use, but there are advantages and disadvantages, especially when automatic type is converted from a higher to a lower type conversion type, will be cut off or reduce the precision of data, the expected result may not be obtained.

To provide more control authority to the program type conversion designers to make more flexible programming, object of the conversion clearer, C language provides the syntax may explicitly specify the type of conversion, commonly referred to as cast.

Cast format:

(目标类型) 表达式;

E.g:

int a,b;
a=4;
b=3;
double dd;
dd=a/b;            // dd的结果将是1。
dd=(double)(a/b);  // dd的结果是1.000000。
dd=(double)a/b;   // dd的结果是1.333333

dd = a / b, dd result is 1, this is well understood that, as an integer or an integer integer division, no fractional part.

dd = (double) (a / b), dd result is 1.000000, the difficult to understand, its operation process is:

(1) first calculating a / b, the result is an integer of 1;

(2) to convert an integer to double, it is 1.000000.

dd = (double) a / b, dd 1.333333 a result, this is not well understood, its operation process is:

(1) first execution (double) a, to convert a double, i.e., 4.000000;

(2) divided by 3 to 4.00000, 1.333333 obtained, comply with the rules of automatic type conversion.

There is also a problem, programmers do not know is (double) a priority or a / b priority, the best way is written like ((double) a) / b, so there is no doubt.

Third, homework

Write the sample program, this section describes the knowledge of all the demo again, the demo program can deepen your understanding and mapping.

Fourth, the copyright notice

C Language Technology Network original article, reproduced please indicate the source link to the article, the author and original.
Source: C Language Technology Network (www.freecplus.net)
Author: Ethics code Agriculture

If the article typos, or content errors, or other suggestions and comments, please correct me message, thank you very much! ! !

Published an original article · won praise 2 · Views 6733

Guess you like

Origin blog.csdn.net/u010806950/article/details/105043999