编程注意事项:关于有符号数和无符号数混用的危险性!!!

本文通过一个具体例子揭示了在C/C++中进行不同数据类型比较时可能遇到的问题,特别是当涉及到有符号数与无符号数之间的比较时。文章强调了正确理解和处理类型转换的重要性,并给出了若干避免常见错误的建议。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 

         DWORD _b = 2;

 

         int _a = -1;

         if (_a < _b)

         {

                   AfxMessageBox(…);          //请问:此处会弹出对话框吗?

}

 

答案:不会!

在进行类型转换时,优先转为无符号数,而负数转换后显然比所有正数都大,结果比较结果与我们想象的相反。

 

强烈建议:

1、   如果不是确实必要的话,请尽量不要使用无符号数,尽量使用有符号数。

2、   打开编译器的警告开关(Compiler Warning (level 3) C4018, Compiler Warning (levels 3 and 4) C4244),对此类的类型转换进行警告。请不要忽视此类警告!

3、   如果确信代码无误,请使用强制类型转换,优先采用转换成有符号数的方法。

4、   建议:每个程序员再仔细阅读一遍:

Usual Arithmetic Conversions

Most C operators perform type conversions to bring the operands of an expression to a common type or to extend short values to the integer size used in machine operations. The conversions performed by C operators depend on the specific operator and the type of the operand or operands. However, many operators perform similar conversions on operands of integral and floating types. These conversions are known as “arithmetic conversions.” Conversion of an operand value to a compatible type causes no change to its value.

The arithmetic conversions summarized below are called “usual arithmetic conversions.” These steps are applied only for binary operators that expect arithmetic type and only if the two operands do not have the same 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.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值