C 乘法运算符C Multiplicative Operators

乘法运算符执行乘法 (*)、除法 (/) 和余数 (%) 运算。The multiplicative operators perform multiplication (\), division (/*), and remainder (%) operations.

语法Syntax

multiplicative-expressionmultiplicative-expression:
cast-expressioncast-expression

multiplicative-expression \* cast-expressionmultiplicative-expression \* cast-expression

multiplicative-expression / cast-expressionmultiplicative-expression / cast-expression

multiplicative-expression % cast-expressionmultiplicative-expression % cast-expression

余数操作符 (%) 的操作数必须是整数。The operands of the remainder operator (%) must be integral. 乘法 (*) 和除法 (/) 运算符可采用整型或浮点类型操作数;操作数的类型可以是不同的。The multiplication (\) and division (/*) operators can take integral- or floating-type operands; the types of the operands can be different.

乘法运算符对操作数执行常用算术转换。The multiplicative operators perform the usual arithmetic conversions on the operands. 结果的类型是转换后操作数的类型。The type of the result is the type of the operands after conversion.

备注

由于在溢出或下溢条件不提供由乘法运算符执行的转换,因此,如果乘法操作的结果在转换后不能用操作数类型表示,则信息可能丢失。Since the conversions performed by the multiplicative operators do not provide for overflow or underflow conditions, information may be lost if the result of a multiplicative operation cannot be represented in the type of the operands after conversion.

C 乘法运算符的描述如下:The C multiplicative operators are described below:

运算符Operator 描述Description
\* 乘法运算符使其两个操作数相乘。The multiplication operator causes its two operands to be multiplied.
/ 除法运算符使第一个操作数除以第二个操作数。The division operator causes the first operand to be divided by the second. 如果两个整数操作数相除,结果不是整数,则根据下列规则截断它:If two integer operands are divided and the result is not an integer, it is truncated according to the following rules:
- 根据 ANSI C 标准,被 0 除的结果是不确定的。- The result of division by 0 is undefined according to the ANSI C standard. Microsoft C 编译器将在编译时或运行时生成错误。The Microsoft C compiler generates an error at compile time or run time.
- 如果两个操作数都为正或无符号,则结果将截断到 0。- If both operands are positive or unsigned, the result is truncated toward 0.
- 如果其中一个操作数为负,则不管操作结果是小于或等于代数商的最大整数还是大于或等于代数商的最小整数,结果均为定义的实现。- If either operand is negative, whether the result of the operation is the largest integer less than or equal to the algebraic quotient or is the smallest integer greater than or equal to the algebraic quotient is implementation defined. (请参阅下面的 Microsoft 专用部分。)(See the Microsoft Specific section below.)
% 第一个操作数除以第二个操作数时,余数运算符的结果是余数。The result of the remainder operator is the remainder when the first operand is divided by the second. 如果除法不精确,则结果将由下列规则确定:When the division is inexact, the result is determined by the following rules:
- 如果右操作数为零,则结果是不确定的。- If the right operand is zero, the result is undefined.
- 如果两个操作数均为正或无符号,则结果为正。- If both operands are positive or unsigned, the result is positive.
- 如果其中一个操作数为负,并且结果不精确,则结果将是定义的实现。- If either operand is negative and the result is inexact, the result is implementation defined. (请参阅下面的 Microsoft 专用部分。)(See the Microsoft Specific section below.)

Microsoft 专用Microsoft Specific

在其中一个操作数为负的除法中,截断的方向将是朝向 0。In division where either operand is negative, the direction of truncation is toward 0.

如果使用余数运算符的除法中任一操作数为负,则结果与被除数(表达式中的第一个操作数)有相同的符号。If either operation is negative in division with the remainder operator, the result has the same sign as the dividend (the first operand in the expression).

结束 Microsoft 专用END Microsoft Specific

示例Examples

如下所示的声明将用于下列示例:The declarations shown below are used for the following examples:

int i = 10, j = 3, n;  
double x = 2.0, y;  

此语句使用乘法运算符:This statement uses the multiplication operator:

y = x * i;  

在此示例中,x 乘以 i 将得到值 20.0。In this case, x is multiplied by i to give the value 20.0. 结果为 double 类型。The result has double type.

n = i / j;  

在此示例中,10 除以 3。In this example, 10 is divided by 3. 结果将被截断到 0,同时产生整数值 3。The result is truncated toward 0, yielding the integer value 3.

n = i % j;  

当 10 除以 3 时,此语句为 n 分配整数余数 1。This statement assigns n the integer remainder, 1, when 10 is divided by 3.

Microsoft 专用Microsoft Specific

余数的符号与被除数的符号相同。The sign of the remainder is the same as the sign of the dividend. 例如:For example:

50 % -6 = 2  
-50 % 6 = -2  

在所有情况下,502 具有相同的符号。In each case, 50 and 2 have the same sign.

结束 Microsoft 专用END Microsoft Specific

另请参阅See Also

乘法运算符和取模运算符Multiplicative Operators and the Modulus Operator