1. Common Operators in C Language: Classification and Function Introduction
|
Classification |
Operator Symbol |
Function Description |
|
Arithmetic Operators |
|
Used for basic mathematical operations such as addition, subtraction, multiplication, division, modulus, as well as increment and decrement operations. The division operator |
|
Relational Operators |
|
Used to compare the size or equality of two values, resulting in a boolean value ( |
|
Logical Operators |
|
Combine multiple conditional expressions or negate conditions |
|
Assignment Operators |
|
Assign values to variables or perform compound assignments with arithmetic and bitwise operations. The assignment order in C is from right to left, for example: a=b=c=4, c is assigned 4 first, then b is assigned c, and finally a is assigned b (not recommended to write this way). |
|
Bitwise Operators |
|
Operate on the binary bits of integers |
|
Conditional Operator |
|
The ternary operator, used to select one of two values based on the result of a conditional expression. The format is |
|
Comma Operator |
|
Used to concatenate multiple expressions, the value of the entire comma expression is the value of the rightmost item. Mostly used in for loops. |
|
Pointer Operators |
|
Used to manipulate pointers, obtaining the value pointed to by the pointer or the address of a variable |
|
Subscript Operator |
|
Used to access array elements, obtaining the value at a specific position in the array through an index |
|
Member Access Operators |
|
Used to access members of structures or unions, the dot operator is used for variables, and the arrow operator is used for pointers |
|
Sizeof Operator |
|
Used to obtain the byte size of a variable or data type, when applied to variables and expressions, parentheses can be omitted, such as sizeof x |
2. Operator Precedence in C Language
|
Precedence |
Operator |
Name or Meaning |
Usage Form |
Operator Associativity |
Description |
|
1 |
[] |
Array Subscript |
ArrayName[ConstantExpression] |
Left to Right |
|
|
() |
Parentheses |
(Expression) FunctionName(ParameterList) |
|||
|
. |
Member Selection (Object) |
Object.MemberName |
|||
|
-> |
Member Selection (Pointer) |
ObjectPointer->MemberName |
|||
|
2 |
– |
Unary Minus Operator |
-Expression |
Right to Left |
Unary Operator |
|
(TypeName) |
Type Casting |
(DataType)Expression |
Should generally avoid implicit type conversions, especially downgrading types |
||
|
++ |
Increment Operator |
++VariableNameVariableName++ |
Unary Operator, if n++ is part of an expression, it can be viewed as “use n first, then increment”; while ++n means “increment n first, then use it”, for example, a=b++; means a=b, then b++; |
||
|
— |
Decrement Operator |
–VariableNameVariableName– |
Unary Operator |
||
|
* |
Dereference Operator |
*PointerVariable |
Unary Operator |
||
|
& |
Address-of Operator |
&VariableName |
Unary Operator |
||
|
! |
Logical NOT Operator |
!Expression |
Unary Operator |
||
|
~ |
Bitwise NOT Operator |
~Expression |
Unary Operator |
||
|
sizeof |
Sizeof Operator |
sizeof(Expression) |
|||
|
3 |
/ |
Division |
Expression / Expression |
Left to Right |
Binary Operator |
|
* |
Multiplication |
Expression * Expression |
Binary Operator |
||
|
% |
Modulus |
IntegerExpression % IntegerExpression |
Binary Operator |
||
|
4 |
+ |
Addition |
Expression + Expression |
Left to Right |
Binary Operator |
|
– |
Subtraction |
Expression – Expression |
Binary Operator |
||
|
5 |
<< |
Left Shift |
Variable << Expression |
Left to Right |
Binary Operator |
|
>> |
Right Shift |
Variable >> Expression |
Binary Operator |
||
|
6 |
> |
Greater Than |
Expression > Expression |
Left to Right |
Binary Operator |
|
>= |
Greater Than or Equal To |
Expression >= Expression |
Binary Operator |
||
|
< |
Less Than |
Expression < Expression |
Binary Operator |
||
|
<= |
Less Than or Equal To |
Expression <= Expression |
Binary Operator |
||
|
7 |
== |
Equal To |
Expression == Expression |
Left to Right |
Binary Operator |
|
!= |
Not Equal To |
Expression != Expression |
Binary Operator |
||
|
8 |
& |
Bitwise AND |
Expression & Expression |
Left to Right |
Binary Operator |
|
9 |
^ |
Bitwise XOR |
Expression ^ Expression |
Left to Right |
Binary Operator |
|
10 |
| |
Bitwise OR |
Expression | Expression |
Left to Right |
Binary Operator |
|
11 |
&& |
Logical AND |
Expression && Expression |
Left to Right |
Binary Operator |
|
12 |
|| |
Logical OR |
Expression || Expression |
Left to Right |
Binary Operator |
|
13 |
?: |
Conditional Operator |
Expression1 ? Expression2 : Expression3 |
Right to Left |
Ternary Operator |
|
14 |
= |
Assignment Operator |
Variable = Expression |
Right to Left |
|
|
/= |
Division Assignment |
Variable /= Expression |
|||
|
*= |
Multiplication Assignment |
Variable *= Expression |
|||
|
%= |
Modulus Assignment |
Variable %= Expression |
|||
|
+= |
Addition Assignment |
Variable += Expression |
|||
|
-= |
Subtraction Assignment |
Variable -= Expression |
|||
|
<<= |
Left Shift Assignment |
Variable <<= Expression |
|||
|
>>= |
Right Shift Assignment |
Variable >>= Expression |
|||
|
&= |
Bitwise AND Assignment |
Variable &= Expression |
|||
|
^= |
Bitwise XOR Assignment |
Variable ^= Expression |
|||
|
|= |
Bitwise OR Assignment |
Variable |= Expression |
|||
|
15 |
, |
Comma Operator |
Expression, Expression,… |
Left to Right |
3. Expressions and Statements
An expression consists of operators and operands
A statement is the basic building block of a C program. A statement is equivalent to a complete computer instruction. In C, most statements end with a semicolon. (A complete computer instruction is not necessarily a statement)
To distinguish between expressions and statements, see:
high = 200 —- This is an expression
high = 200; —- This is a statement