Complete C operators reference guide: arithmetic, relational, logical, bitwise, and assignment operators with precedence rules and code.
Overview of C Operators
An operator in C is a symbol that instructs the compiler to perform specific mathematical, logical, or relational manipulations on operands.
C provides a rich set of built-in operators categorized by function and precedence level.
1. Arithmetic Operators
Arithmetic operators perform basic mathematical calculations on numerical operands:
| Operator | Meaning | Example | Result (if a = 10, b = 3) |
|---|---|---|---|
+ | Addition | a + b | 13 |
- | Subtraction | a - b | 7 |
* | Multiplication | a * b | 30 |
/ | Division (Truncated for integers) | a / b | 3 |
% | Modulo (Remainder after division) | a % b | 1 |
++ | Increment (Pre / Post) | ++a / a++ | 11 |
-- | Decrement (Pre / Post) | --a / a-- | 9 |
c (ISO Standard)#include <stdio.h> int main() { int x = 20; int y = 6; printf("Addition: %d\n", x + y); printf("Integer Division: %d\n", x / y); printf("Remainder Modulo: %d\n", x % y); return 0; }
2. Relational & Equality Operators
Relational operators compare two values and evaluate to 1 (true) or 0 (false):
| Operator | Description | Example |
|---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
3. Logical Operators
Logical operators combine boolean expressions:
| Operator | Description | Behavior |
|---|---|---|
&& | Logical AND | Returns 1 if both operands are non-zero (short-circuits on false). |
|| | Logical OR | Returns 1 if at least one operand is non-zero (short-circuits on true). |
! | Logical NOT | Inverts the truth value of the operand. |
4. Bitwise Operators
Bitwise operators manipulate integer data at the individual bit level:
| Operator | Meaning | Description |
|---|---|---|
& | Bitwise AND | Bit is 1 if both corresponding bits are 1. |
| | Bitwise OR | Bit is 1 if either corresponding bit is 1. |
^ | Bitwise XOR | Bit is 1 if bits differ (exclusive OR). |
~ | Bitwise NOT | Flips all bits (one's complement). |
<< | Left Shift | Shifts bits left, multiplying by powers of 2. |
>> | Right Shift | Shifts bits right, dividing by powers of 2. |
c (ISO Standard)#include <stdio.h> int main() { unsigned char a = 0b00001100; /* 12 */ unsigned char b = 0b00011001; /* 25 */ printf("a & b = %d\n", a & b); /* 8 */ printf("a | b = %d\n", a | b); /* 29 */ printf("a << 1 = %d\n", a << 1); /* 24 */ return 0; }
5. Operator Precedence & Associativity Table
When an expression contains multiple operators, precedence determines evaluation order (higher numbers bind first):
| Precedence Level | Operators | Associativity |
|---|---|---|
| 1 (Highest) | (), [], ->, ., ++ (postfix), -- (postfix) | Left-to-Right |
| 2 | + (unary), - (unary), !, ~, * (deref), & (addr), sizeof, ++ (prefix), -- (prefix) | Right-to-Left |
| 3 | * (multiply), /, % | Left-to-Right |
| 4 | + (add), - (subtract) | Left-to-Right |
| 5 | <<, >> | Left-to-Right |
| 6 | <, <=, >, >= | Left-to-Right |
| 7 | ==, != | Left-to-Right |
| 8 | & (bitwise AND) | Left-to-Right |
| 9 | ^ (bitwise XOR) | Left-to-Right |
| 10 | | (bitwise OR) | Left-to-Right |
| 11 | && (logical AND) | Left-to-Right |
| 12 | || (logical OR) | Left-to-Right |
| 13 | ?: (ternary conditional) | Right-to-Left |
| 14 | =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |= | Right-to-Left |
| 15 (Lowest) | , (comma) | Left-to-Right |
Related Language References
- Check primitive memory requirements in the C Data Types Reference.
- Review beginner syntax and fundamentals in C Programming Basics.
- Test expressions interactively in the Online C Compiler.