类型转换 (C)Type Conversions (C)

类型转换取决于指定的运算符以及操作数或运算符的类型。Type conversions depend on the specified operator and the type of the operand or operators. 下列情况下将执行类型转换:Type conversions are performed in the following cases:

  • 当将一个类型的值赋给其他类型的变量或运算符在执行运算前转换了其一个或多个操作数的类型时When a value of one type is assigned to a variable of a different type or an operator converts the type of its operand or operands before performing an operation

  • 当一个类型的值显式强制转换为其他类型时When a value of one type is explicitly cast to a different type

  • 当值作为参数传递给函数时,或当类型从函数返回时When a value is passed as an argument to a function or when a type is returned from a function

    字符、短整数或整数位域(无论带符号还是无符号)或枚举类型的对象均可在可使用整数的表达式中使用。A character, a short integer, or an integer bit field, all either signed or not, or an object of enumeration type, can be used in an expression wherever an integer can be used. 如果一个 int 可表示原始类型的所有值,则该值将转换为 int;否则,该值将转换为 unsigned intIf an int can represent all the values of the original type, then the value is converted to int; otherwise, it is converted to unsigned int. 此过程称为“整型提升”。This process is called "integral promotion." 整型提升将保留值。Integral promotions preserve value. 即,保证提升后的值与提升前一样。That is, the value after promotion is guaranteed to be the same as before the promotion. 有关详细信息,请参阅常用算术转换See Usual Arithmetic Conversions for more information.

请参阅See Also

表达式和赋值Expressions and Assignments