操作员或系统管理员_C操作员

操作员或系统管理员

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个虚构变量ab作为示例来详细说明所有这些。

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:

二进制运算符使用两个操作数进行工作:

OperatorNameExample
=Assignmenta = b
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
%Moduloa % b
操作员 名称
= 分配 a = b
+ 加成 a + b
- 减法 a - b
* 乘法 a * b
/ a / b
% 模数 a % b

Unary operators only take one operand:

一元运算符只能使用一个操作数:

OperatorNameExample
+Unary plus+a
-Unary minus-a
++Incrementa++ or ++a
--Decrementa-- 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++++aa++递增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)

OperatorNameExample
==Equal operatora == b
!=Not equal operatora != b
>Bigger thana > b
<Less thana < b
>=Bigger than or equal toa >= b
<=Less than or equal toa <= 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:

这些运算符对于执行赋值和同时执行算术运算非常有用:

OperatorNameExample
+=Addition assignmenta += b
-=Subtraction assignmenta -= b
*=Multiplication assignmenta *= b
/=Division assignmenta /= b
%=Modulo assignmenta %= 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和结果/ b2 * 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的值c3

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.

而且我们不必考虑太多。

翻译自: https://flaviocopes.com/c-operators/

操作员或系统管理员

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值