Cprogramming 简明教程

Arithmetic Operators in C

C 中的算术运算符是某些特殊符号,预定义为执行算术运算。我们熟悉基本算术运算 − 加法、减法、乘法和除法。C 是一种计算语言,因此这些运算符对于执行计算机化进程至关重要。

Arithmetic operators in C are certain special symbols, predefined to perform arithmetic operations. We are familiar with the basic arithmetic operations − addition, subtraction, multiplication and division. C is a computational language, so these operators are essential in performing a computerised process.

除了分别分配给四个符号 @s26@s27@s28@s29 的上述运算之外,C 中还有另一个算术运算符,称为 @s30,我们针对它使用 %* 符号。

In addition to the above operations assigned to the four symbols +, , *, and / respectively, C has another arithmetic operator called the modulo operator for which we use the *%*symbol.

下表列出了 C 中的算术运算符 −

The following table lists the arithmetic operators in C −

Operator

Description

+

Adds two operands.

Subtracts second operand from the first.

*

Multiplies both operands.

/

Divides numerator by denominator.

%

Modulus Operator and remainder of after an integer division.

++

Increment operator increases the integer value by one.

 — 

Decrement operator decreases the integer value by one.

@s31@s32 运算符也列在上面表中。将我们在单独的章节中学习增量和减量运算符。

The ++ and -- operators are also listed in the above table. We shall learn about increment and decrement operators in a separate chapter.

Example: Arithmetic Operators in C

以下示例演示了如何在 C 程序中使用这些算术运算符- -

The following example demonstrates how to use these arithmetic operators in a C program −

#include <stdio.h>

int main(){

   int op1 = 10;
   int op2 = 3;

   printf("Operand1: %d Operand2: %d \n\n", op1, op2);
   printf("Addition of op1 and op2: %d\n", op1 + op2);
   printf("Subtraction of op2 from op1: %d\n", op1 - op2);
   printf("Multiplication of op1 and op2: %d\n", op1 * op2);
   printf("Division of op1 by op2: %d\n", op1/op2);

   return 0;
}

Output

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Operand1: 10 Operand2: 3

Addition of op1 and op2: 13
Subtraction of op2 from op1: 7
Multiplication of op1 and op2: 30
Division of op1 by op2: 3

Type Casting in C

前三个结果和预期一样,但除法的结果却不是。预期 10/3 是一个分数(3.333333)。是否是因为我们使用 %d 格式说明符来打印除法结果?如果我们更改代码的最后一行如下- -

The first three results are as expected, but the result of division is not. You expect 10/3 to be a fractional number (3.333333). Is it because we used the %d format specifier to print the outcome of the division? If we change the last line of the code as follows −

printf("Division of op1 by op2: %f\n", op1/op2);

现在除法运算的结果将是 "0.000000",这更加令人惊讶。C 之所以这样表现是因为整数与另一个整数的除法总是返回一个整数。

Now the outcome of the division operation will be "0.000000", which is even more surprising. The reason why C behaves like this is because the division of an integer with another integer always returns an integer.

若要获得浮点除法运算,至少一个操作数必须是浮点数,或者需要使用类型转换运算符将一个整数操作数更改为浮点数。

To obtain floating-point division, at least one operand must be a float, or you need to use the typecast operator to change one of the integer operands to float.

现在,将给定程序的最后一个 printf 语句更改为如下所示- -

Now, change the last printf statement of the given program as follows −

printf("Division of op1 by op2: %f\n", (float)op1/op2);

在进行此更改后再次运行代码时,它将显示正确的除法运算结果- -

When you run run the code again after making this change, it will show the correct division −

Division of op1 by op2: 3.333333

Note: 如果对浮点表达式使用 %d 格式说明符,它将始终导致 "0"。

Note: If you use %d format specifier for a floating-point expression, it will always result in "0".

Example

使用至少一个浮点数(或双精度数)操作数的算术运算结果始终为浮点数。请参阅以下示例- -

The result of arithmetic operations with at least one float (or double) operand is always float. Take a look at the following example −

#include <stdio.h>

int main(){

   int op1 = 10;
   float op2 = 2.5;

   printf("Operand1: %d Operand2: %f\n", op1, op2);
   printf("Addition of op1 and op2: %f\n", op1 + op2);
   printf("Subtraction of op2 from op1: %f\n", op1 - op2);
   printf("Multiplication of op1 and op2: %f\n", op1 * op2);
   printf("Division of op1 by op2: %f\n", op1/op2);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Operand1: 10 Operand2: 2.500000
Addition of op1 and op2: 12.500000
Subtraction of op2 from op1: 7.500000
Multiplication of op1 and op2: 25.000000
Division of op1 by op2: 4.000000

Arithmetic Operations with char Data Type

在 C 中, char 数据类型是 int 类型的一个子集。因此,我们可以对 char 操作数执行算术运算。

In C, char data type is a subset of int type. Hence, we can perform arithmetic operations with char operands.

Example

以下示例演示了如何对两个操作数(其中一个为 "char" 类型)执行算术运算- -

The following example shows how you can perform arithmetic operations with two operands out of which one is a "char" type −

#include <stdio.h>

int main(){

   char op1 = 'F';
   int op2 = 3;

   printf("operand1: %c operand2: %d\n", op1, op2);
   printf("Addition of op1 and op2: %d\n", op1 + op2);
   printf("Subtraction of op2 from op1: %d\n", op1 - op2);
   printf("Multiplication of op1 and op2: %d\n", op1 * op2);
   printf("Division of op1 by op2: %d\n", op1/op2);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

operand1: F operand2: 3

Addition of op1 and op2: 73
Subtraction of op2 from op1: 67
Multiplication of op1 and op2: 210
Division of op1 by op2: 23

由于 char 数据类型是 int 的子集,所以 %c 格式说明符返回与 %d 说明符返回的整数相关联的 ASCII 字符。

Since a char data type is a subset of int, the %c format specifier returns the ASCII character associated with an integer returned by the %d specifier.

如果两个 char 操作数之间的任何算术运算导致整数超出 char 的范围,那么 %c 说明符将显示为空。

If any arithmetic operation between two char operands results in an integer beyond the range of char, the %c specifier displays blank.

Modulo Operator in C

模数运算符 ( % ) 返回除法运算的余数。

The modulo operator (%) returns the remainder of a division operation.

Example

请看以下示例:

Take a look at the following example −

#include <stdio.h>

int main(){

   int op1 = 10;
   int op2 = 3;

   printf("Operand1: %d Operand2: %d\n", op1, op2);
   printf("Modulo of op1 and op2: %d\n", op1%op2);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Operand1: 10 Operand2: 3
Modulo of op1 and op2: 1

模数运算符需要两个操作数均为 int 类型。如果不是,编译器将给出 type mismatch error 。例如,在上面的代码中将 "op1" 的数据类型更改为 float 并再次运行该程序- -

The modulo operator needs both the operands of int type. If not, the compiler gives a type mismatch error. For example, change the data type of "op1" to float in the above code and run the program again −

float op1 = 10;
int op2 = 3;
printf("Modulo of op1 and op2: %d\n", op1%op2);

现在,你将收到以下消息的类型不匹配错误- -

Now, you will get a type mismatch error with the following message −

error: invalid operands to binary % (have 'float' and 'int')

不过,它确实允许对模数运算使用 char 操作数。

It does allow char operands for modulo operations though.

Negation Operator in C

由符号 ++-- 表示的增量和递减运算符是一元运算符。它们已在单独的一章中介绍。表示减法运算符的 " " 符号也充当一元否定运算符。

The increment and decrement operators represented by the symbols ++ and -- are unary operators. They have been covered in a separate chapter. The "" symbol, representing subtraction operator, also acts a unary negation operator.

Example

以下示例突出显示了如何在 C 中使用否定运算符- -

The following example highlights how you can use the negation operator in C −

#include <stdio.h>

int main(){

   int op1 = 5;
   int op2 = -op1;

   printf("Operand1: %d Operand2: %d\n", op1, op2);

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Operand1: 5 Operand2: -5

在上面的示例中,"–" 符号返回 op1 的负值并将该值分配给 op2。

In the above example, the "–" symbol returns the negative value of op1 and assigns the same to op2.