Operators
The tables below describe some of the operators available in the C programming language, and their order of precedence.
Operator name | Example | Description |
---|---|---|
Assignment | a = b | Assigns the value of b to a |
Unary plus | +a | Signifies that a has a positive value |
Addition | a + b | Returns the sum of a and b |
Prefix increment | ++a | Increments a and returns its new value |
Postfix increment | a++ | Increments a and returns its original value |
Assignment by addition | a += b | Assigns the value of a plus b to a |
Unary minus | -a | Signifies that a has a negative value |
Subtraction | a - b | Returns the value a minus b |
Prefix decrement | --a | Decrements a and returns its new value |
Postfix decrement | a-- | Decrements a and returns its original value |
Assignment by subtraction | a -= b | Assigns the value of a minus b to a |
Multiplication | a * b | Returns the value of a times b |
Assignment by multiplication | a *= b | Assigns the value of a times b to a |
Division | a / b | Returns the value of a divided by b |
Assignment by division | a /= b | Assigns the value of a divided by b to a |
Modulus | a % b | Returns the remainder of a divided by b |
Assignment by modulus | a %= b | Assigns the remainder of a divided by b to a |
Operator name | Example | Description |
---|---|---|
Less than | a < b | Returns 1 if a is less than b, otherwise 0 |
Less than or equal to | a <= b | Returns 1 if a is less than or equal to b, otherwise 0 |
Greater than | a > b | Returns 1 if a is greater than b, otherwise 0 |
Greater than or equal to | a >= b | Returns 1 if a is greater than or equal to b, otherwise 0 |
Not equal to | a != b | Returns 1 if a is not equal to b, otherwise 0 |
Equal to | a == b | Returns 1 if a is equal to b, otherwise 0 |
Operator name | Example | Description |
---|---|---|
Logical negation | !a | Returns true if a is false, and false if a is true |
Logical AND | a && b | Returns true if both a and b are true |
Logical OR | a || b | Returns true if either a or b (or both) are true |
Operator name | Example | Description |
---|---|---|
Bitwise left shift | a << b | Returns the value of a with its bits shifted b places to the left |
Assignment by bitwise left shift | a <<= b | Assigns the value of a with its bits shifted b places to the left to a |
Bitwise right shift | a >> b | Returns the value of a with its bits shifted b places to the right |
Assignment by bitwise right shift | a >>= b | Assigns the value of a with its bits shifted b places to the right to a |
Bitwise one's complement | ~a | Returns the one's complement of a |
Bitwise AND | a & b | Returns the result of a bitwise AND of a and b |
Assignment by bitwise AND | a &= b | Assigns the value of a bitwise AND of a and b to a |
Bitwise OR | a | b | Returns the result of a bitwise OR of a and b |
Assignment by bitwise OR | a |= b | Assigns the value of a bitwise OR of a and b to a |
Bitwise XOR | a ^ b | Returns the result of a bitwise XOR of a and b |
Assignment by bitwise XOR | a ^= b | Assigns the value of a bitwise XOR of a and b to a |
Operator Precedence
Operators have a set order of precedence during evaluation. Items within parenthesis (brackets) are evaluated first, and have the highest precedence. The following table shows the order of operator precedence, with the items at the top having the highest precedence. Operators in the same cell have the same precedence.
Operator | Description |
---|---|
++, -- | Postfix increment and decrement |
++, -- +, - !, ~ | Prefix increment and decrement Unary plus and minus Logical NOT and bitwise NOT |
*, /, % | Multiplication, division, and modulus |
+, - | Addition and subtraction |
<<, >> | Bitwise left shift and right shift |
<, <= >, >= | Relational "less than" and "less than or equal to" Relational "greater than" and "greater than or equal to" |
==, != | Relational "equal to" and "not equal to" |
& | Bitwise AND |
^ | Bitwise XOR (exclusive or) |
| | Bitwise OR (inclusive or) |
&& | Logical AND |
|| | Logical OR |
= +=, -= *=, /=, %= <<=, >>= &=, ^=, |= | Assignment Assignment by sum and difference Assignment by product, quotient, and remainder Assignment by bitwise left shift and right shift Assignment by bitwise AND, XOR, and OR |