Cprogramming 简明教程

Type Casting in C

术语 "类型转换" 指的是将一种数据类型转换为另一种数据类型。它也称为 "类型转换"。在某些时候,编译器会自行进行转换(隐式类型转换),以便数据类型彼此兼容。

The term "type casting" refers to converting one datatype into another. It is also known as "type conversion". There are certain times when the compiler does the conversion on its own (implicit type conversion), so that the data types are compatible with each other.

在其他情况下,C 编译器会强制执行类型转换(显式类型转换),这是由类型转换运算符引起的。例如,如果要将 'long' 值存储到一个简单整数中,那么可以将 'long' 类型转换为 'int' 类型。

On other occasions, the C compiler forcefully performs the typecasting (explicit type conversion), which is caused by the typecasting operator. For example, if you want to store a 'long' value into a simple integer then you can type cast 'long' to 'int'.

可以使用类型转换运算符将值从一种类型显式转换为另一种类型:

You can use the typecasting operator to explicitly convert the values from one type to another −

(type_name) expression

Example 1

考虑以下示例 −

Consider the following example −

#include <stdio.h>

int main() {

   int sum = 17, count = 5;
   double mean;

   mean =  sum / count;
   printf("Value of mean: %f\n", mean);

}

Output

运行代码并检查其输出:

Run the code and check its output −

Value of mean: 3.000000

虽然我们希望结果是 17/5,即 3.4,但它显示为 3.000000,因为除法表达式中的两个操作数都是{@s5}类型。

While we expect the result to be 17/5, that is 3.4, it shows 3.000000, because both the operands in the division expression are of int type.

Example 2

在 C 中,除法运算的结果始终是字节长度更大的数据类型。因此,我们必须将其中一个整数操作数类型化为{@s6}。

In C, the result of a division operation is always in the data type with a larger byte length. Hence, we have to typecast one of the integer operands to float.

强制类型转换运算符将一个整型变量与另一个整型变量之间的除法运算作为浮点运算 −

The cast operator causes the division of one integer variable by another to be performed as a floating-point operation −

#include <stdio.h>

int main() {

   int sum = 17, count = 5;
   double mean;

   mean = (double) sum / count;
   printf("Value of mean: %f\n", mean);
}

Output

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Value of mean: 3.400000

此处需要注意,强制类型转换运算符的优先级高于除法运算,因此会先将求和的值转换为 double 类型,最后将其除以计数,从而生成一个 double 值。

It should be noted here that the cast operator has precedence over division, so the value of sum is first converted to type double and finally it gets divided by count yielding a double value.

类型转换可以是隐式的,由编译器自动执行,也可以通过使用强制类型转换运算符显式指定。在需要执行类型转换时,建议将其作为良好的编程习惯。

Type conversions can be implicit which is performed by the compiler automatically, or it can be specified explicitly through the use of the cast operator. It is considered good programming practice to use the cast operator whenever type conversions are necessary.

Rules of Type Promotions

执行隐式或自动类型转换时,C 编译器遵循类型提升规则。遵循的原则是 −

While performing the implicit or automatic type conversions, the C compiler follows the rules of type promotions. Generally, the principle followed is as follows −

  1. Byte and short values − They are promoted to int.

  2. If one operand is a long − The entire expression is promoted to long.

  3. If one operand is a float − The entire expression is promoted to float.

  4. If any of the operands is double − The result is promoted to double.

Integer Promotion in C

整数提升是将类型“小于”int 或 unsigned int 的整数类型的值转换为 int 或 unsigned int 的过程。

Integer promotion is the process by which values of integer type "smaller" than int or unsigned int are converted either to int or unsigned int.

Example

考虑将字符与整数相加的示例 −

Consider an example of adding a character with an integer −

#include <stdio.h>

int main() {

   int i = 17;
   char c = 'c';  /* ascii value is 99 */
   int sum;

   sum = i + c;
   printf("Value of sum : %d\n", sum);
}

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Value of sum: 116

此处,求和的值为 116,因为编译器正在执行整数提升,并在执行实际加法运算之前将“c”转换为 ASCII。

Here, the value of sum is 116 because the compiler is doing integer promotion and converting the value of 'c' to ASCII before performing the actual addition operation.

Usual Arithmetic Conversion

对于赋值运算符或逻辑运算符 && 和 ||,不会执行常见的算术转换。

The usual arithmetic conversions are not performed for the assignment operators nor for the logical operators && and ||.

Example

让我们看以下示例,以理解这个概念 −

Let us take the following example to understand the concept −

#include <stdio.h>

int main() {

   int i = 17;
   char c = 'c';  /* ascii value is 99 */
   float sum;

   sum = i + c;
   printf("Value of sum : %f\n", sum);
}

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Value of sum: 116.000000

在此示例中,很容易理解首先将 c 转换为整数,但由于最终值是 double,因此会应用常见的算术转换,并且编译器会将 i 和 c 转换为“float”并将其相加,从而生成“float”结果。

Here, it is simple to understand that first c gets converted to an integer, but as the final value is double, usual arithmetic conversion applies and the compiler converts i and c into 'float' and adds them yielding a 'float' result.