Cprogramming 简明教程

C - Operators

operator 是一个符号,它告诉编译器执行特定的数学或逻辑函数。根据定义, operator 对操作数执行某个操作。运算符需要一个或多个操作数才能执行该操作。

根据执行操作需要的操作数数量,操作数被称为一元、二元或三元运算符。它们分别需要一个、两个或三个操作数。

  1. 一元运算符 * —— ++(增量),——(减量),!(非),~(求补),&(地址),(解引用)

  2. Binary operators —— 算术、逻辑和关系运算符(除了!)

  3. Ternary operators —— ?运算符

C 语言内置运算符丰富,并且提供了以下类型的运算符 ——

  1. Arithmetic Operators

  2. Relational Operators

  3. Logical Operators

  4. Bitwise Operators

  5. Assignment Operators

  6. Misc Operators

在本章中,我们将研究每个运算符的工作方式。在这里,你将获得所有这些章节的概述。此后,我们为这些每个运算符提供了独立的章节,其中包含大量示例,以展示这些运算符在 C Programming 中的工作原理。

Arithmetic Operators

我们最熟悉的是算术运算符。这些运算符用于对操作数执行算术运算。最常见的算术运算符是加号(+),减号(-),乘号(*)和除号(/)。

此外,模数(%)是一个重要的算术运算符,它计算一个除法运算的余数。算术运算符用于形成算术表达式。这些运算符本质上是二元的,这意味着它们需要两个操作数,并且它们作用于数字操作数,这些操作数可能是数字 literalsvariables 或表达式。

例如,请看下面这个简单的表达式 −

a + b

此处的“+”是一个算术运算符。我们在后续章节中将进一步了解 C 中的算术运算符。

下表显示 C 语言支持的所有算术运算符。假定变量 A 为 10,变量 B 为 20,则 −

Operator

Description

Example

+

Adds two operands.

A + B = 30

减去第二个运算数。

A − B = -10

*

Multiplies both operands.

A * B = 200

/

Divides numerator by de-numerator.

B / A = 2

%

求余运算符和整数除法后的余数。

B % A = 0

++

增量运算符将整数 1 加上 integer。

A++ = 11

 — 

减量运算符将整数 value 减去 1。

A-- = 9

Relational Operators

在学习中学数学时我们也了解了关系运算符。这些运算符用于比较两个操作数并返回一个 boolean 值(真或假)。它们用在一个 boolean 表达式中。

最常见的比较运算符是小于 (<)、大于 (>)、小于或等于 (⇐)、大于或等于 (>=)、等于 (==) 和不等于 (!=)。关系运算符也是二元操作符,需要两个数字操作数。

例如,在 Boolean 表达式中 −

a > b

这里,“>”是一个关系运算符。

我们将在后续的一章中学习有关关系运算符及其用法。

Operator

Description

Example

==

检查两个操作数的值是否相等。如果相等,则结果为 true。

(A == B) 不为真。

!=

检查两个操作数的值是否相等。如果相等,则结果为 true。

(A != B) 为真。

>

检查左操作数的值是否大于右操作数的值。如果大于,则结果为 true。

(A > B) 不为真。

<

检查左操作数的值是否小于右操作数的值。如果小于,则结果为 true。

(A < B) 为真。

>=

检查左操作数的值是否大于或等于右操作数的值。如果大于或等于,则结果为 true。

(A >= B) 不为真。

检查左操作数的值是否小于或等于右操作数的值。如果小于或等于,则结果为 true。

(A ⇐ B) 为真。

Logical Operators

这些运算符用于组合两个或更多 boolean 表达式。我们可以通过组合 boolean 表达式和这些运算符形成一个复合 boolean 表达式。逻辑运算符的一个例子如下 −

a >= 50 && b >= 50

最常见的逻辑运算符是 AND (&&)、OR(||) 和 NOT (!)。逻辑运算符也是二元运算符。

Operator

Description

Example

&&

称为逻辑 AND 运算符。如果两个操作数都非零,则条件变为 true。

(A && B) 为假。

称为逻辑或运算符。如果两个操作数的任何一个非零,则结果为真。

(A

B) is true.

!

称为逻辑非运算符。用于反转其操作数的逻辑状态。如果某个条件为真,则逻辑非运算符会将其变为假。

我们将在后续章节中详细讨论 C 语言中的逻辑运算符。

Bitwise Operators

位运算符使您可以操作存储在计算机内存中的数据。这些运算符用于对操作数执行按位操作。

最常见的位运算符是 AND (&)、OR (|)、XOR (^)、NOT (~)、左移 (<<) 和右移 (>>)。其中,“~”运算符是一元运算符,而大多数其他位运算符在本质上都是二元的。

按位运算符对位进行操作并执行按位操作。&, “|”和“^”的真值表如下:

p

q

p &

p

p ^ q

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

假设 A = 60,B = 13(二进制格式),它们将如下所示:

A = 0011 1100

B = 0000 1101

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

The following table lists the bitwise operators supported by C. Assume variable 'A' holds 60 and variable 'B' holds 13, then −

link:../cprogramming/c_bitwise_operators.html[Show Examples]
[%autowidth]
|===

|Operator|Description|Example
|&|Binary AND Operator copies a bit to the result if it exists in both operands.|(A & B) = 12, i.e., 0000 1100
|||Binary OR Operator copies a bit if it exists in either operand.|(A | B) = 61, i.e., 0011 1101
|^|Binary XOR Operator copies the bit if it is set in one operand but not both.|(A ^ B) = 49, i.e., 0011 0001
|~|Binary One's Complement Operator is unary and has the effect of 'flipping' bits.|(~A ) = ~(60), i.e,. -0111101
|<<|Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.|A << 2 = 240 i.e., 1111 0000
|>>|Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.|A >> 2 = 15 i.e., 0000 1111
|===


=== Assignment Operators

As the name suggests, an assignment operator "assigns" or sets a value to a named variable in C. These operators are used to assign values to variables. The "=" symbol is defined as assignment operator in C, however it is not to be confused with its usage in mathematics.

The following table lists the assignment operators supported by the C language −

link:../cprogramming/c_assignment_operators.html[Show Examples]
[%autowidth]
|===

|Operator|Description|Example
|=|Simple assignment operator. Assigns values from right side operands to left side operand|C = A + B will assign the value of A + B to C
|+=|Add AND assignment operator. It adds the right operand to the left operand and assign the result to the left operand.|C += A is equivalent to C = C + A
|-=|Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.|C -= A is equivalent to C = C - A
|*=|Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.|C *= A is equivalent to C = C * A
|/=|Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.|C /= A is equivalent to C = C / A
|%=|Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.|C %= A is equivalent to C = C % A
|<<=|Left shift AND assignment operator.|C <<= 2 is same as C = C << 2
|>>=|Right shift AND assignment operator.|C >>= 2 is same as C = C >> 2
|&=|Bitwise AND assignment operator.|C &= 2 is same as C = C & 2
|^=|Bitwise exclusive OR and assignment operator.|C ^= 2 is same as C = C ^ 2
||=|Bitwise inclusive OR and assignment operator.|C |= 2 is same as C = C | 2
|===

Hence, the expression "a = 5" assigns 5 to the variable "a", but "5 = a" is an invalid expression in C.

The "=" operator, combined with the other arithmetic, relational and bitwise operators form augmented assignment operators. For example, the += operator is used as add and assign operator. The most common assignment operators are =, +=, -=, *=, /=, %=, &=, |=, and ^=.


=== Misc Operators ↦ sizeof & ternary

Besides the operators discussed above, there are a few other important operators including *sizeof* and *? :* supported by the C Language.

link:../cprogramming/c_sizeof_operator.html[Show Examples]
[%autowidth]
|===

|Operator|Description|Example
|sizeof()|Returns the size of a variable.|sizeof(a), where a is integer, will return 4.
|&|Returns the address of a variable.|&a; returns the actual address of the variable.
|*|Pointer to a variable.|*a;
|? :|Conditional Expression.|If Condition is true ? then value X : otherwise value Y
|===


=== Operators Precedence in C

Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

link:../cprogramming/c_operators_precedence.html[Show Examples]
[%autowidth]
|===

|Category|Operator|Associativity
|Postfix|() [] -> . ++ - -|Left to right
|Unary|+ - ! ~ ++ - - (type)* & sizeof|Right to left
|Multiplicative|* / %|Left to right
|Additive|+ -|Left to right
|Shift|<< >>|Left to right
|Relational|< <= > >=|Left to right
|Equality|== !=|Left to right
|Bitwise AND|&|Left to right
|Bitwise XOR|^|Left to right
|Bitwise OR|||Left to right
|Logical AND|&&|Left to right
|Logical OR||||Left to right
|Conditional|?:|Right to left
|Assignment|= += -= *= /= %=>>= <<= &= ^= |=|Right to left
|Comma|,|Left to right
|===


=== Other Operators in C

Apart from the above, there are a few other operators in C that are not classified into any of the above categories. For example, the increment and decrement operators (++ and --) are unary in nature and can appear as a prefix or postfix to the operand.

The operators that work with the address of memory location such as the address-of operator (&) and the dereference operator (*). The sizeof operator (sizeof) appears to be a keyword but really an operator.

C also has the type cast operator (()) that forces the type of an operand to be changed. C also uses the dot (.) and the arrow (->) symbols as operators when dealing with derived link:../cprogramming/c_data_types.html[data types] such as link:../cprogramming/c_structures.html[struct] and link:../cprogramming/c_unions.html[union].

The C99 version of C introduced a few additional operators such as auto, decltype.

A single expression in C may have multiple operators of different type. The link:../compile_c_online.php[C compiler] evaluates its value based on the operator precedence and associativity of operators. For example, in the following expression −

[source, c]

a + b * c

The multiplication operand takes precedence over the addition operator.

We shall understand these properties with examples in a subsequent chapter.

Many other programming languages, which are called C-family languages (such as link:../cplusplus/index.html[C++], link:../csharp/index.html[C#], link:../java/index.html[Java], link:../perl/index.html[Perl] and link:../php/index.html[PHP]) have an operator nomenclature that is similar to C.