C Operators - Complete Reference Guide
Master all C operators with detailed explanations, usage examples, and programming best practices for effective C programming.
7 Categories
Comprehensive coverage
Practical Examples
Real-world usage
Precedence Rules
Operator hierarchy
Introduction to C Operators
Operators are special symbols that perform operations on variables and values. C provides a rich set of operators to manipulate data, make decisions, and control program flow.
Key Characteristics
- Operators work on operands (variables, constants, expressions)
- Operators have precedence (order of evaluation)
- Operators have associativity (left-to-right or right-to-left)
- Some operators can be overloaded (used for different operations)
- Operators can be unary, binary, or ternary
Operator Categories
- Arithmetic Operators: Mathematical calculations
- Relational Operators: Comparison operations
- Logical Operators: Boolean logic operations
- Bitwise Operators: Bit-level operations
- Assignment Operators: Value assignment
- Other Operators: Special purpose operators
Important Note About Operators
Operator precedence determines which operations are performed first in an expression. When in doubt, use parentheses () to explicitly define the order of operations.
C Operators Classification
Here is a comprehensive classification of all C operators organized by their operation type:
| Operation Type | Operators |
|---|---|
|
Unary Operators
Operate on a single operand
|
++
--
++ (increment), -- (decrement)
|
|
Arithmetic Operators
Perform mathematical calculations
|
+
-
*
/
%
+ (add), - (subtract), * (multiply), / (divide), % (modulus)
|
|
Logical Operators
Perform Boolean logic operations
|
&&
||
!
&& (AND), || (OR), ! (NOT)
|
|
Relational Operators
Compare values and return Boolean results
|
<
<=
>
>=
==
!=
<, <=, >, >=, ==, !=
|
|
Bit-wise Operators
Perform operations at bit level
|
&
|
<<
>>
~
^
& (AND), | (OR), << (left shift), >> (right shift), ~ (NOT), ^ (XOR)
|
|
Assignment Operators
Assign values to variables
|
=
+=
-=
*=
/=
%=
=, +=, -=, *=, /=, %=
|
|
Ternary or Conditional Operator
Compact if-else statement replacement
|
? :
? : (conditional operator)
|
Complete C Operators Reference
Here is the complete list of C operators with their categories, descriptions, and examples:
| Operator | Category | Description & Purpose | Example |
|---|---|---|---|
| + | Arithmetic | Addition - Adds two operands | int sum = a + b; |
| - | Arithmetic | Subtraction - Subtracts right operand from left | int diff = a - b; |
| * | Arithmetic | Multiplication - Multiplies two operands | int prod = a * b; |
| / | Arithmetic | Division - Divides left operand by right | int quot = a / b; |
| % | Arithmetic | Modulus - Returns remainder of division | int rem = a % b; |
| ++ | Arithmetic | Increment - Increases value by 1 | a++; or ++a; |
| -- | Arithmetic | Decrement - Decreases value by 1 | a--; or --a; |
| == | Relational | Equal to - Checks if two values are equal | if (a == b) { ... } |
| != | Relational | Not equal to - Checks if two values are not equal | if (a != b) { ... } |
| > | Relational | Greater than - Checks if left is greater than right | if (a > b) { ... } |
| < | Relational | Less than - Checks if left is less than right | if (a < b) { ... } |
| >= | Relational | Greater than or equal to | if (a >= b) { ... } |
| <= | Relational | Less than or equal to | if (a <= b) { ... } |
| && | Logical | Logical AND - True if both operands are true | if (a > 0 && b > 0) |
| || | Logical | Logical OR - True if at least one operand is true | if (a > 0 || b > 0) |
| ! | Logical | Logical NOT - Reverses logical state | if (!flag) { ... } |
| & | Bitwise | Bitwise AND - Performs AND on each bit | int result = a & b; |
| | | Bitwise | Bitwise OR - Performs OR on each bit | int result = a | b; |
| ^ | Bitwise | Bitwise XOR - Performs XOR on each bit | int result = a ^ b; |
| ~ | Bitwise | Bitwise NOT - Inverts all bits (one's complement) | int result = ~a; |
| << | Bitwise | Left shift - Shifts bits to the left | int result = a << 2; |
| >> | Bitwise | Right shift - Shifts bits to the right | int result = a >> 2; |
| = | Assignment | Simple assignment - Assigns right value to left | int a = 10; |
| += | Assignment | Add and assign - a += b is same as a = a + b | a += 5; |
| -= | Assignment | Subtract and assign - a -= b is same as a = a - b | a -= 3; |
| *= | Assignment | Multiply and assign - a *= b is same as a = a * b | a *= 2; |
| /= | Assignment | Divide and assign - a /= b is same as a = a / b | a /= 2; |
| %= | Assignment | Modulus and assign - a %= b is same as a = a % b | a %= 3; |
| ?: | Other | Ternary/Conditional operator - Short form of if-else | max = (a > b) ? a : b; |
| sizeof() | Other | Returns size of variable or type in bytes | int size = sizeof(int); |
| & | Other | Address of operator - Returns memory address | int *ptr = &a; |
| * | Other | Pointer dereference - Accesses value at address | int value = *ptr; |
| -> | Other | Arrow operator - Accesses struct member via pointer | ptr->member = 10; |
| . | Other | Dot operator - Accesses struct/union member | obj.member = 10; |
| , | Other | Comma operator - Evaluates multiple expressions | a = (b=3, b+2); // a=5 |
= (assignment), + - * / (arithmetic), == != < > (relational), && || ! (logical), and ++ -- (increment/decrement).
Detailed Operator Examples
#include <stdio.h>
int main() {
int a = 15, b = 4;
printf("a = %d, b = %d\n", a, b);
printf("Addition: a + b = %d\n", a + b);
printf("Subtraction: a - b = %d\n", a - b);
printf("Multiplication: a * b = %d\n", a * b);
printf("Division: a / b = %d\n", a / b);
printf("Modulus: a %% b = %d\n", a % b);
// Increment/Decrement
int x = 5;
printf("Original x: %d\n", x);
printf("Post-increment: x++ = %d\n", x++);
printf("After post-increment: x = %d\n", x);
printf("Pre-increment: ++x = %d\n", ++x);
return 0;
}
#include <stdio.h>
int main() {
int a = 10, b = 20, c = 10;
// Relational operators
printf("a == b: %d\n", a == b); // 0 (false)
printf("a != b: %d\n", a != b); // 1 (true)
printf("a < b: %d\n", a < b); // 1 (true)
printf("a > b: %d\n", a > b); // 0 (false)
printf("a <= c: %d\n", a <= c); // 1 (true)
printf("a >= c: %d\n", a >= c); // 1 (true)
// Logical operators
int age = 25;
int hasLicense = 1;
printf("Can drive? %d\n", age >= 18 && hasLicense);
printf("Is teenager? %d\n", age >= 13 && age <= 19);
printf("Not a teenager? %d\n", !(age >= 13 && age <= 19));
return 0;
}
#include <stdio.h>
int main() {
unsigned int a = 12; // Binary: 1100
unsigned int b = 5; // Binary: 0101
printf("a = %u (binary: 1100)\n", a);
printf("b = %u (binary: 0101)\n", b);
printf("Bitwise AND (a & b): %u (binary: 0100)\n", a & b);
printf("Bitwise OR (a | b): %u (binary: 1101)\n", a | b);
printf("Bitwise XOR (a ^ b): %u (binary: 1001)\n", a ^ b);
printf("Bitwise NOT (~a): %u\n", ~a);
printf("Left shift (a << 2): %u (binary: 110000)\n", a << 2);
printf("Right shift (a >> 2): %u (binary: 0011)\n", a >> 2);
return 0;
}
Operator Precedence and Associativity
Operator precedence determines the order in which operators are evaluated in an expression. When operators have the same precedence, associativity determines the order.
| Precedence | Category | Operators | Associativity |
|---|---|---|---|
| 1 (Highest) | Function call, Array subscript | () [] . -> | Left to Right |
| 2 | Unary | ! ~ ++ -- + - * & sizeof | Right to Left |
| 3 | Multiplicative | * / % | Left to Right |
| 4 | Additive | + - | Left to Right |
| 5 | Bitwise shift | << >> | Left to Right |
| 6 | Relational | < <= > >= | Left to Right |
| 7 | Equality | == != | 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 | Conditional | ?: | Right to Left |
| 14 | Assignment | = += -= *= /= %= &= ^= |= <<= >>= | Right to Left |
| 15 (Lowest) | Comma | , | Left to Right |
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 2;
int result;
// Example 1: Multiplication before addition
result = a + b * c; // 10 + (5 * 2) = 20
printf("a + b * c = %d\n", result);
// Example 2: Using parentheses to change order
result = (a + b) * c; // (10 + 5) * 2 = 30
printf("(a + b) * c = %d\n", result);
// Example 3: Complex expression
result = a * b + c / 2 - 1; // (10 * 5) + (2 / 2) - 1 = 50 + 1 - 1 = 50
printf("a * b + c / 2 - 1 = %d\n", result);
// Example 4: Assignment vs comparison
int x, y = 5;
x = y == 5; // First y == 5 evaluates to 1, then x = 1
printf("x = y == 5: x = %d\n", x);
return 0;
}
Common Mistakes with Operators
Key Takeaways
- C provides 7 categories of operators: Arithmetic, Relational, Logical, Bitwise, Assignment, and Others
- Operator precedence determines evaluation order; use parentheses for clarity
- == is for comparison, = is for assignment - don't confuse them!
- Integer division truncates the fractional part; cast to float for precise division
- ++ and -- can be prefix (++x) or postfix (x++) with different behaviors
- Bitwise operators work on individual bits; logical operators work on boolean values
- The ternary operator (?:) provides a compact if-else alternative
- Assignment operators can be combined with arithmetic (+=, -=, *=, /=, %=)
- sizeof() returns the size in bytes of a variable or type