C and C++ type conversions and usual arithmetic conversions [c和c++中的类型转换以及常用算数转]

  1. Type Conversion

Type conversion in C is the process of converting one data type to another.

1.1 Two types of Conversion

  • Implicit Type Conversion. Also known as ‘automatic type conversion’. Done by the compiler on its own, without any external trigger from the user. Generally takes place when in an expression more than one data type is present. In such conditions type conversion (type promotion) takes place to avoid loss of data.
  • Explicit Type Conversion. This process is also called type casting and it is user-defined. Here the user can typecast the result to make it of a particular data type.

1.2 Scenarios of type conversions

  • 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

1.3 Integral type conversion table

FromToMethodConsequence
unsigned charsigned charPreserve bit pattern; high-order bit becomes sign bitMisinterpreted
unsigned charsigned short intZero-extendSafe
unsigned charsigned long intZero-extendSafe
unsigned charunsigned short intZero-extendSafe
unsigned charunsigned long intZero-extendSafe
unsigned short intsigned charPreserve low-order byteLost
unsigned short intsigned short intPreserve bit pattern; high-order bit becomes sign bitMisinterpreted
unsigned short intsigned long intZero-extendSafe
unsigned short intunsigned charPreserve low-order byteLost
unsigned long intsigned charPreserve low-order byteLost
unsigned long intsigned short intPreserve low-order wordLost
unsigned long intsigned long intPreserve bit pattern; high-order bit becomes sign bitMisinterpreted
unsigned long intunsigned charPreserve low-order byteLost
unsigned long intunsigned short intPreserve low-order wordLost
signed charshort intSign-extendSafe
signed charlong intSign-extendSafe
signed charunsigned charPreserve bit pattern; high-order bit no longer sign bitMisinterpreted
signed charunsigned short intSign-extend to short; convert short to unsigned shortLost
signed charunsigned long intSign-extend to long; convert long to unsigned longLost
signed short intcharTruncate to low-order byteLost
signed short intlong intSign-extendSafe
signed short intunsigned charTruncate to low-order byteLost
signed short intunsigned short intPreserve bit pattern; high-order bit no longer sign bitMisinterpreted
signed short intunsigned long intSign extend to long; convert long to unsignedLost
signed long intcharTruncate to low order byteLost
signed long intshort intTruncate to low order bytesLost
signed long intunsigned charTruncate to low order byteLost
signed long intunsigned short intTruncate to low order bytesLost

2. Usual Arithmetic Conversions

The arithmetic conversions summarized below are called “usual arithmetic conversions.” These steps are applied only for binary operators that expect arithmetic type. The purpose is to yield a common type which is also the type of the result. To determine which conversions actually take place, the compiler applies the following algorithm to binary operations in the expression. The steps below are not a precedence order.

  1. If either operand is of type long double, the other operand is converted to type long double.
  2. If the above condition is not met and either operand is of type double, the other operand is converted to type double.
  3. If the above two conditions are not met and either operand is of type float, the other operand is converted to type float.
  4. If the above three conditions are not met (none of the operands are of floating types), then integral conversions are performed on the operands as follows:
    • If either operand is of type unsigned long, the other operand is converted to type unsigned long.
    • If the above condition is not met and either operand is of type long and the other of type unsigned int, both operands are converted to type unsigned long.
    • If the above two conditions are not met, and either operand is of type long, the other operand is converted to type long.
    • If the above three conditions are not met, and either operand is of type unsigned int, the other operand is converted to type unsigned int.
    • If none of the above conditions are met, both operands are converted to type int.

Example

float   fVal;
double  dVal;
int   iVal;
unsigned long ulVal;

dVal = iVal * ulVal; /* iVal converted to unsigned long
                      * Uses step 4.
                      * Result of multiplication converted to double
                      */
dVal = ulVal + fVal; /* ulVal converted to float
                      * Uses step 3.
                      * Result of addition converted to double
                      */

References

https://learn.microsoft.com/en-us/cpp/c-language/type-conversions-c?view=msvc-170

  • 19
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值