# C Programming Language Operators

### C Operators

#### Assignment Operator

Operator Syntax Explanation
Assignment a = b assign to a the value of b

#### Arithmetic Operators

Operator Syntax Explanation
Addition a + b the value of a+b
Subtraction a - b the value of a-b
Unary plus +a the value of a
Unary negation -a the value of -a
Multiplication a * b the value of a multiplied by b
Division a / b the value of a divided by b (unless both are integers when integer division is done instead)
Modulo/Remainder a % b remainder of a divided by b (e.g. 4%3 is 1)
Increment prefix: ++a
suffix: a++
increase a by 1 and then use the value of a
use the value of a and then increase a by 1
Decrement prefix: --a
suffix: a--
decrease a by 1 and then use the value of a
use the value of a and then decrease a by 1

#### Comparison Operators

These comparisons return true or false based on evaluation, e.g., if a is 2 and b is 2, then a==b is true. These can be viewed as a question like in the explanation field of the following table:

Operator Syntax Explanation
Equality check a == b is a equal to b?
Inequality check a != b is a not equal to b?
Greater than a > b is a greater than b?
Less than a < b is a less than b?
Greater than or equal to a >= b is a greater than or equal to b?
Less than or equal to a <= b is a less than or equal to b?

#### Logical operators

Logical operators express the boolean logic operators NOT, AND, OR as show in this table:

Operator Syntax Explanation
Logical NOT !a negation of a: if a is true then false; if a is false then true
Logical AND a && b true if both a and b are true, otherwise false
Logical OR a || b true if either a or b is true, false if both are false

#### Bitwise operators

Bitwise operators operate on each bit within a value.

Operator Syntax Explanation
Bitwise NOT ~a logical negation on each bit
Bitwise AND a & b logical AND on each bit
Bitwise OR a | b logical OR on each bit
Bitwise XOR a ^ b logical eXclusive OR on each bit
Bitwise left shift a << b shifts bits of a by b positions left
Bitwise right shift a >> b shifts bits of a by b positions right

NOTE: be careful with shifting bits. There can often be undefined behaviour for shifting negative integers. Care should always be taken. One should have a firm understanding of the internal representation of binary numbers (such as two's-complement) to implement shifts.

#### Compound Assignment

Compound assignment operators do the operation and then assigned the variable. The following table shows details:

Operator Syntax Explanation
Addition assignment a += b add b to a and assign result to a (a = a+b)
Subtraction assignment a -= b subtract b from a and assign result to a (a = a-b)
Multiplication assignment a *= b multiply a by b and assign result to a (a = a*b)
Division assignment a /= b divide a by b and assign result to a (a = a/b)
Modulo/remainder assignment a %= b divide a by b and assign remainder to a (a = a%b)
Bitwise AND assignment a &= b perform a&b and assign result to a (a = a&b)
Bitwise OR assignment a |= b perform a|b and assign result to a (a = a|b)
Bitwise XOR assignment a ^= b perform a^b and assign result to a (a = a^b)
Bitwise left shift assignment a <<= b shift bits of a left by b positions and assign result to a (a = a<<b)
Bitwise right shift assignment a >>= b shift bits of a right by b position and assign result to a (a = a>>b)

#### Member operators

Operator Syntax Explanation
Array element/subscript a[b] element of array a at index b (note that first element has index 0)
Pointer dereference *a value stored at location pointed to by a