My Journey with C Language: 7 – Expressions and Operators

1. Common Operators in C Language: Classification and Function Introduction

Classification

Operator Symbol

Function Description

Arithmetic Operators

<span>+</span><span>-</span><span>*</span><span>/</span><span>%</span><span>++</span><span>--</span>

Used for basic mathematical operations such as addition, subtraction, multiplication, division, modulus, as well as increment and decrement operations. The division operator <span>/</span> uses <span>truncation towards zero</span> (after C99), for example: -7/2 = -3. The modulus operator <span>%</span> is calculated using the formula:<span>a - (a/b)*b</span>.

Relational Operators

<span>></span><span><</span><span>>=</span><span><=</span><span>==</span><span>!=</span>

Used to compare the size or equality of two values, resulting in a boolean value (<span>true</span> or <span>false</span>), be cautious with floating-point comparisons!

Logical Operators

<span>&&</span> (logical AND), <span>||</span><span> (logical OR), </span><span>!</span> (logical NOT)

Combine multiple conditional expressions or negate conditions

Assignment Operators

<span>=</span><span>+=</span><span>-=</span><span>*=</span><span>/=</span><span>%=</span><span>&=</span><span>^=</span><span>|=</span><span><<=</span><span>>>=</span>

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

<span>&</span> (bitwise AND), <span>|</span> (bitwise OR), <span>^</span> (bitwise XOR), <span>~</span> (bitwise NOT), <span><<</span> (left shift), <span>>></span> (right shift)

Operate on the binary bits of integers

Conditional Operator

<span>?:</span>

The ternary operator, used to select one of two values based on the result of a conditional expression. The format is <span>expression ? expression1 : expression2</span>, if the expression is true, it returns the value of expression1, otherwise it returns the value of expression2.

Comma Operator

<span>,</span>

Used to concatenate multiple expressions, the value of the entire comma expression is the value of the rightmost item. Mostly used in for loops.<span>,</span> ensures that the expressions it separates are evaluated from left to right (the comma is a sequence point)

Pointer Operators

<span>*</span> (dereference), <span>&</span> (address of)

Used to manipulate pointers, obtaining the value pointed to by the pointer or the address of a variable

Subscript Operator

<span>[]</span>

Used to access array elements, obtaining the value at a specific position in the array through an index

Member Access Operators

<span>.</span> (dot operator), <span>-></span> (arrow operator)

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

<span>sizeof</span>

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

Leave a Comment