操作员或系统管理员
C offers us a wide variety of operators that we can use to operate on data.
C为我们提供了多种运算符,可用于对数据进行运算。
In particular, we can identify various groups of operators:
特别是,我们可以识别出不同类型的运营商:
- arithmetic operators 算术运算符
- comparison operators 比较运算符
- logical operators 逻辑运算符
- compound assignment operators 复合赋值运算符
- bitwise operators 按位运算符
- pointer operators 指针运算符
- structure operators 结构算子
- miscellaneous operators 杂项运算符
In this blog post I’m going to detail all of them, using 2 imaginary variables a
and b
as examples.
在这篇博客文章中,我将使用2个虚构变量a
和b
作为示例来详细说明所有这些。
I am keeping bitwise operators, structure operators and pointer operators out of this list, as I will dedicate them a specific blog post.
我将按位运算符,结构运算符和指针运算符排除在此列表之外,因为我将专门为他们写一篇特定的博客文章。
算术运算符 (Arithmetic operators)
In this macro group I am going to separate binary operators and unary operators.
在这个宏组中,我将分开二进制运算符和一元运算符。
Binary operators work using two operands:
二进制运算符使用两个操作数进行工作:
Operator | Name | Example |
---|---|---|
= | Assignment | a = b |
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulo | a % b |
操作员 | 名称 | 例 |
---|---|---|
= | 分配 | a = b |
+ | 加成 | a + b |
- | 减法 | a - b |
* | 乘法 | a * b |
/ | 师 | a / b |
% | 模数 | a % b |
Unary operators only take one operand:
一元运算符只能使用一个操作数:
Operator | Name | Example |
---|---|---|
+ | Unary plus | +a |
- | Unary minus | -a |
++ | Increment | a++ or ++a |
-- | Decrement | a-- or --a |
操作员 | 名称 | 例 |
---|---|---|
+ | 一元加 | +a |
- | 一元减 | -a |
++ | 增量 | a++ 或++a |
-- | 减量 | a-- 或--a |
The difference between a++
and ++a
is that a++
increments the a
variable after using it. ++a
increments the a
variable before using it.
之间的差a++
和++a
是a++
递增a
使用后的变量。 ++a
增量a
使用它之前的变量。
For example:
例如:
int a = 2;
int b;
b = a++ /* b is 2, a is 3 */
b = ++a /* b is 4, a is 4 */
The same applies to the decrement operator.
递减运算符也是如此。
比较运算符 (Comparison operators)
Operator | Name | Example |
---|---|---|
== | Equal operator | a == b |
!= | Not equal operator | a != b |
> | Bigger than | a > b |
< | Less than | a < b |
>= | Bigger than or equal to | a >= b |
<= | Less than or equal to | a <= b |
操作员 | 名称 | 例 |
---|---|---|
== | 平等算子 | a == b |
!= | 不等于运算符 | a != b |
> | 大于 | a > b |
< | 少于 | a < b |
>= | 大于或等于 | a >= b |
<= | 小于或等于 | a <= b |
逻辑运算符 (Logical operators)
!
NOT (example:!a
)!
不(例如:!a
)&&
AND (example:a && b
)&&
AND(例如:a && b
)||
OR (example:a || b
)||
或(例如:a || b
)
Those operators are great when working with boolean values.
这些运算符在使用布尔值时非常有用。
复合赋值运算符 (Compound assignment operators)
Those operators are useful to perform an assignment and at the same time perform an arithmetic operation:
这些运算符对于执行赋值和同时执行算术运算非常有用:
Operator | Name | Example |
---|---|---|
+= | Addition assignment | a += b |
-= | Subtraction assignment | a -= b |
*= | Multiplication assignment | a *= b |
/= | Division assignment | a /= b |
%= | Modulo assignment | a %= b |
操作员 | 名称 | 例 |
---|---|---|
+= | 加法分配 | a += b |
-= | 减法分配 | a -= b |
*= | 乘法分配 | a *= b |
/= | 部门分配 | a /= b |
%= | 模分配 | a %= b |
杂项运营商 (Miscellaneous operators)
三元运算符 (The ternary operator)
The ternary operator is the only operator in C that works with 3 operands, and it’s a short way to express conditionals.
三元运算符是C中唯一可以使用3个操作数的运算符,这是表达条件的一种简便方法。
This is how it looks:
看起来是这样的:
<condition> ? <expression> : <expression>
Example:
例:
a ? b : c
If a
is evaluated to true
, then the b
statement is executed, otherwise c
is.
如果将a
评估为true
,则执行b
语句,否则执行c
。
The ternary operator is functionality-wise same as an if/else conditional, except it is shorter to express and it can be inlined into an expression.
三元运算符在功能上与if / else条件相同,只不过它的表示较短,可以内联到表达式中。
大小 (sizeof)
The sizeof
operator returns the size of the operand you pass. You can pass a variable, or even a type.
sizeof
运算符返回您传递的操作数的大小。 您可以传递变量,甚至可以传递类型。
Example usage:
用法示例:
#include <stdio.h>
int main(void) {
int age = 37;
printf("%ld\n", sizeof(age));
printf("%ld", sizeof(int));
}
运算符优先级 (Operator precedence)
With all those operators (and more, which I haven’t covered in this post, including bitwise, structure operators and pointer operators), we must pay attention when using them together in a single expression.
对于所有这些运算符(以及我在本文中未介绍的更多运算符,包括按位运算符,结构运算符和指针运算符),在单个表达式中一起使用它们时必须注意。
Suppose we have this operation:
假设我们执行以下操作:
int a = 2;
int b = 4;
int c = b + a * a / b - a;
What’s the value of c
? Do we get the addition being executed before the multiplication and the division?
c
的值是多少? 我们是否在加法和除法之前执行加法?
There is a set of rules that help us solving this puzzle.
有一套规则可以帮助我们解决这个难题。
In order from less precedence to more precedence, we have:
为了从低优先级到高优先级,我们有:
the
=
assignment operator=
赋值运算符the
+
and-
binary operators+
和-
二进制运算符the
*
and/
operators*
和/
运算符the
+
and-
unary operators+
和-
一元运算符
Operators also have an associativity rule, which is always left to right except for the unary operators and the assignment.
运算符还具有关联规则,除一元运算符和赋值外,该规则始终从左到右。
In:
在:
int c = b + a * a / b - a;
We first execute a * a / b
, which due to being left-to-right we can separate into a * a
and the result / b
: 2 * 2 = 4
, 4 / 4 = 1
.
我们首先执行a * a / b
,由于从左到右,我们可以将其分成a * a
和结果/ b
: 2 * 2 = 4
4 / 4 = 1
。
Then we can perform the sum and the subtraction: 4 + 1 - 2. The value of c
is 3
.
然后,我们可以执行的总和和减法:4 + 1 - 2的值c
是3
。
In all cases, however, I want to make sure you realize you can use parentheses to make any similar expression easier to read and comprehend.
但是,在所有情况下,我都想确保您意识到可以使用括号使任何相似的表达式更易于阅读和理解。
Parentheses have higher priority over anything else.
括号优先于其他任何内容。
The above example expression can be rewritten as:
上面的示例表达式可以重写为:
int c = b + ((a * a) / b) - a;
and we don’t have to think about it that much.
而且我们不必考虑太多。
操作员或系统管理员