自增自减运算的作用对象

发布时间:2024-04-27 14:05 发布:上海旅游网

问题描述:

发现练习册上有道题说++与--不能作用与实型变量,还给了好几行解释,显然不是印刷错误。不过课本上并没有提到过啊。而且我在vc环境下c跟c++程序都可以对实型变量进行++与--运算。
不知道有没有了解这方面的,有这种说法吗

问题解答:

自增自减运算符只能用于变量,不能用于常量和表达式
可以用于实型变量
你再看看练习册

可以用于++与--
msdn如下解释,明确提到操作对象integral, floating, or pointer type

Prefix Increment and Decrement Operators: ++, --
unary-expression :

++ unary-expression
-- unary-expression

The unary operators (++ and --) are called “prefix” increment or decrement operators when the increment or decrement operators appear before the operand. Postfix increment and decrement has higher precedence than prefix increment and decrement operators. The operand must have integral, floating, or pointer type and must be a modifiable l-value expression (an expression without the const attribute). The result is not an l-value.

When the operator appears before its operand, the operand is incremented or decremented and its new value is the result of the expression.

For more information, see Postfix Increment and Decrement Operators.

Example

In the following example, first the variable nNum2 is incremented. Then the sum of nNum1 and nNum2 is placed into nTotal.

// Example of the prefix increment operator
int nNum1=1, nNum2=2, nTotal;

nTotal = (nNum1) + (++nNum2); // nTotal now is 4

++与--不能作用与实型变量是正确的

没听说过

热点新闻