c运算符优先级
Operators are essential components in programming for logical and mathematical functions. Operators in C programming are categorized in the following ways.
运算符是逻辑和数学功能编程中必不可少的组成部分。 C编程中的运算符按以下方式分类。
![C Operators](https://i-blog.csdnimg.cn/blog_migrate/bf4f7c8ff1d36612a3bfd717e135eae0.png)
C Operators
C操作员
C中的算术运算符 (Arithmetic Operators in C)
二进制算术运算符 (Binary Arithmetic operators)
These are of 5 types.
We are assuming a=8 and b=2.
这些有5种类型。
我们假设a = 8和b = 2 。
- Addition: ‘ + ‘
加法:“ + ”a + b //Answer: 10
- Subtraction: ‘ – ‘
减法:' – 'a - b //Answer: 6
- Multiplication: ‘ * ‘
乘法:' * 'a * b //Answer: 16
- Division: ‘ / ‘
师:' / 'a / b //Answer: 4
- Modulus : ‘ % ‘
模量:' % 'a % b //Answer: 0 (Because reminder of 8/2 is 0)
Note: The division operator gives quotient, while modulus operator gives reminder.
注意:除法运算符给出商,而模数运算符给出提醒。
一元算术运算符 (Unary Arithmetic operators)
These are of 2 types. These operators can be used before or after the operand.
这些有2种类型。 这些运算符可以在操作数之前或之后使用。
- Increment Operator ++ (Plus-Plus) 增量运算符++ (Plus-Plus)
It increments the value of given operand by 1.
它将给定操作数的值加1。
a = 10; a++; //incremented value by 1, a is now 11 b=10; ++b; //incremented value by 1, b is now 11
- Decrement Operator – – (Minus-Minus) 减量运算符– – (减号-减号)
It decrements the value of given operand by 1
将给定操作数的值减1
a = 10; a--; //decremented value by 1, a is now 9 b = 10; --b; //decremented value by 1, b is now 9
The above examples have used the post and pre increment/decrement operators in same way for the basic understanding. However, there is significant difference as well in particular scenarios which shall be discussed later.
上面的示例已使用post和pre递增/递减运算符进行基本了解。 但是,在某些特定情况下也存在重大差异,稍后将进行讨论。
C中的赋值运算符 (Assignment Operators in C)
They assign the value to the identifier on the left of operator from literal, another identifier or expression on right of operator.
它们的值分配给运营商的从操作者的右文字,另一标识符或表达左侧的标识符 。
- Assign: ‘ = ‘
分配:“ = ”a = 10; //assign value 10 to a b = a; //Assign value of a to b
- Perform Operation and Assign: += -= *= /=
执行操作并分配: + = -= * = / =a = 5; //a has value 5 a += 10; //is same as (a = a + 10) i.e. a = 5 + 10 which assigns value 15 to a b = 6; //b has value 6 b -= 2; //is same as (b = b - 2) i.e. b = 6 - 2 which assigns value 4 to b
C中的关系运算符 (Relational Operators in C)
These are also called comparison operators, used to compare the values of two operands i.e. they tell whether the condition is true (1) or false (0).
这些也称为比较运算符,用于比较两个操作数的值,即它们告诉条件是true(1)还是false(0)。
- Less than < 小于<
- Greater than > 大于>
- Less than equal to <= 小于等于<=
- Greater than equal to >= 大于等于> =
- Equal equal to == 等于==
- Not equal to != 不等于!=
a = 10; //assign value 10 to a
a < 14; //is value of a less than 14 ? True. hence, result is 1
a >= 14; //is value of a greater than or equal to 14 ? False. hence, result is 0
a != 14; //is value of a not equal to 14 ? True. hence, result is 1
a == 14; //is value of a equal 14 ? False. hence, result is 0
Please Note that = is assignment operator, and == is comparison operator which checks whether values are equal or not.
请注意, =是赋值运算符,而==是比较运算符,它检查值是否相等。
C语言中的逻辑运算符 (Logical Operators in C)
These operators are used to make a decision by result of two or more conditions (relations).
这些运算符用于根据两个或多个条件(关系)的结果进行决策。
- AND & : Both conditions should be satisfied AND & :应同时满足两个条件
a = 10; a < 14 & a > 8; //a is less than 14 and a is greater than 8, hence result is 1 (true) a < 14 & a > 12; //a is less than 14 and a is not greater than 12 , hence result is 0 (false)
- OR | : at least one condition should be satisfied OR | :至少应满足一个条件
b = 10; b < 14 | b > 12; //b is less than 14 or greater than 12, hence result is 1 (true) b > 14 | b < 8; //neither b is greater than 14, nor a is not less than 8 , hence result is 0 (false)
- Short Circuit AND && 短路AND &&
- Short Circuit OR || 短路或||
These operators gives same output as their normal versions but they execute quicker.
If AND operator finds false condition, it would not check the other condition since result would be false anyway. Similarly, if OR operator finds true condition, it won’t check other conditions.这些运算符的输出与正常版本相同,但执行速度更快。
如果AND运算符发现错误条件,则不会检查其他条件,因为结果无论如何都是错误的。 同样,如果OR运算符找到真实条件,则不会检查其他条件。- NOT ! : Reverse the Output 不! :反转输出
a = 5; (! a > 10); //True. a > 10 is false, but not operator is making it true, hence result is 1;
摘要 (Summary)
This is a basic introduction about operators and their usage. These are essential elements to get started with coding. In practice, there are several more operators in C programming for specific purposes which are not discussed at this point to keep things simple.
这是有关运算符及其用法的基本介绍。 这些是开始进行编码的基本要素。 在实践中,出于特定目的,在C编程中还有更多运算符,为使事情简单起见,此处不再讨论。
c运算符优先级