C Operators
Operators are symbols that tell the compiler to perform specific mathematical or logical operations. C is rich in built-in operators and provides the following types of operators:
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Miscellaneous Operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
Operator | Description | Example |
---|---|---|
+ | Addition | A + B |
- | Subtraction | A - B |
* | Multiplication | A * B |
/ | Division | A / B |
% | Modulus (remainder after division) | A % B |
++ | Increment (increases value by 1) | A++ or ++A |
-- | Decrement (decreases value by 1) | A-- or --A |
Examples of Arithmetic Operators
#include <stdio.h>
int main() {
int a = 10, b = 5;
printf("a + b = %d\n", a + b); // Addition
printf("a - b = %d\n", a - b); // Subtraction
printf("a * b = %d\n", a * b); // Multiplication
printf("a / b = %d\n", a / b); // Division
printf("a %% b = %d\n", a % b); // Modulus (remainder)
int c = a++; // Post-increment
printf("After a++, a = %d, c = %d\n", a, c);
int d = ++b; // Pre-increment
printf("After ++b, b = %d, d = %d\n", b, d);
return 0;
}
Output:
a + b = 15
a - b = 5
a * b = 50
a / b = 2
a % b = 0
After a++, a = 11, c = 10
After ++b, b = 6, d = 6
When dividing integers, the result is also an integer. For example, 5 / 2
gives 2
, not 2.5
. To get a decimal result, at least one operand must be a floating-point number: 5.0 / 2
gives 2.5
.
Relational Operators
Relational operators are used to compare two values:
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 |
Examples of Relational Operators
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 10;
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)
return 0;
}
Output:
a == b: 0
a != b: 1
a > b: 1
a < b: 0
a >= c: 1
a <= c: 1
Logical Operators
Logical operators are used to combine conditional statements:
Operator | Description | Example |
---|---|---|
&& | Logical AND | A && B |
|| | Logical OR | A || B |
! | Logical NOT | !A |
Examples of Logical Operators
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 0;
// Logical AND: both operands must be true (non-zero)
printf("a > 0 && b > 0: %d\n", a > 0 && b > 0); // 1 (true)
printf("a > 0 && c > 0: %d\n", a > 0 && c > 0); // 0 (false)
// Logical OR: at least one operand must be true (non-zero)
printf("a > 0 || c > 0: %d\n", a > 0 || c > 0); // 1 (true)
printf("b < 0 || c > 0: %d\n", b < 0 || c > 0); // 0 (false)
// Logical NOT: reverses the logical state
printf("!a: %d\n", !a); // 0 (false, because a is non-zero)
printf("!c: %d\n", !c); // 1 (true, because c is zero)
return 0;
}
Output:
a > 0 && b > 0: 1
a > 0 && c > 0: 0
a > 0 || c > 0: 1
b < 0 || c > 0: 0
!a: 0
!c: 1
Bitwise Operators
Bitwise operators operate on the individual bits of their operands:
Operator | Description | Example |
---|---|---|
& | Bitwise AND | A & B |
| | Bitwise OR | A | B |
^ | Bitwise XOR (exclusive OR) | A ^ B |
~ | Bitwise complement (one's complement) | ~A |
<< | Left shift | A << n |
>> | Right shift | A >> n |
Examples of Bitwise Operators
#include <stdio.h>
int main() {
unsigned int a = 60; // 00111100 in binary
unsigned int b = 13; // 00001101 in binary
printf("a & b = %u\n", a & b); // Bitwise AND: 00001100 (12)
printf("a | b = %u\n", a | b); // Bitwise OR: 00111101 (61)
printf("a ^ b = %u\n", a ^ b); // Bitwise XOR: 00110001 (49)
printf("~a = %u\n", ~a); // Bitwise complement: 11000011... (4294967235 for 32-bit int)
printf("a << 2 = %u\n", a << 2); // Left shift: 11110000 (240)
printf("a >> 2 = %u\n", a >> 2); // Right shift: 00001111 (15)
return 0;
}
Output:
a & b = 12
a | b = 61
a ^ b = 49
~a = 4294967235
a << 2 = 240
a >> 2 = 15
Assignment Operators
Assignment operators are used to assign values to variables:
Operator | Description | Example | Equivalent to |
---|---|---|---|
= | Simple assignment | a = b | a = b |
+= | Add and assign | a += b | a = a + b |
-= | Subtract and assign | a -= b | a = a - b |
*= | Multiply and assign | a *= b | a = a * b |
/= | Divide and assign | a /= b | a = a / b |
//= | Floor divide and assign | a //= b | a = a // b |
%= | Modulus and assign | a %= b | a = a % b |
**= | Exponent and assign | a **= b | a = a ** b |
&= | Bitwise AND and assign | a &= b | a = a & b |
` | =` | Bitwise OR and assign | a |= b |
^= | Bitwise XOR and assign | a ^= b | a = a ^ b |
<<= | Left shift and assign | a <<= b | a = a << b |
>>= | Right shift and assign | a >>= b | a = a >> b |
Examples of Assignment Operators
#include <stdio.h>
int main() {
int a = 10;
printf("Initial value: a = %d\n", a);
a += 5; // a = a + 5
printf("After a += 5: a = %d\n", a);
a -= 3; // a = a - 3
printf("After a -= 3: a = %d\n", a);
a *= 2; // a = a * 2
printf("After a *= 2: a = %d\n", a);
a /= 4; // a = a / 4
printf("After a /= 4: a = %d\n", a);
a %= 2; // a = a % 2
printf("After a %%= 2: a = %d\n", a);
return 0;
}
Output:
Initial value: a = 10
After a += 5: a = 15
After a -= 3: a = 12
After a *= 2: a = 24
After a /= 4: a = 6
After a %= 2: a = 0
Miscellaneous Operators
Operator | Description | Example |
---|---|---|
sizeof | Returns the size of a variable or data type | sizeof(a) |
& | Returns the address of a variable | &a |
* | Pointer to a variable | *a |
? : | Conditional expression (ternary operator) | condition ? expr1 : expr2 |
, | Comma operator | expr1, expr2 |
Examples of Miscellaneous Operators
#include <stdio.h>
int main() {
int a = 10;
int *ptr;
// sizeof operator
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of variable a: %zu bytes\n", sizeof(a));
// & operator (address-of)
ptr = &a;
printf("Address of a: %p\n", &a);
// * operator (dereferencing)
printf("Value at address stored in ptr: %d\n", *ptr);
// Ternary operator (? :)
int max = (a > 5) ? a : 5;
printf("Maximum value: %d\n", max);
// Comma operator
int b = (a = 5, a + 2); // Assigns 5 to a, then assigns (a + 2) to b
printf("a = %d, b = %d\n", a, b);
return 0;
}
Output (the memory address will vary):
Size of int: 4 bytes
Size of variable a: 4 bytes
Address of a: 0x7ffd382a57a4
Value at address stored in ptr: 10
Maximum value: 10
a = 5, b = 7
Operator Precedence
Operators have different precedence levels, which determine the order in which they are evaluated in an expression.
Precedence | Operator | Description | Associativity |
---|---|---|---|
1 | () [] -> . | Function call, array subscript, member access | Left to right |
2 | ! ~ ++ -- + - * & (type) sizeof | Unary operators | 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 (ternary) | Right to left |
14 | = += -= *= /= %= &= ^= |= <<= >>= | Assignment | Right to left |
15 | , | Comma | Left to right |
Example of Operator Precedence
#include <stdio.h>
int main() {
int a = 5, b = 3, c = 10;
int result;
// Operator precedence in action
result = a + b * c; // Multiplication has higher precedence than addition
printf("a + b * c = %d\n", result); // Equivalent to a + (b * c)
result = (a + b) * c; // Parentheses change precedence
printf("(a + b) * c = %d\n", result);
// Complex example
result = a + b * c / (a - 2) + b;
printf("a + b * c / (a - 2) + b = %d\n", result);
// Evaluated as: a + ((b * c) / (a - 2)) + b
return 0;
}
Output:
a + b * c = 35
(a + b) * c = 80
a + b * c / (a - 2) + b = 18
Summary
- Arithmetic operators perform mathematical operations like addition (
+
), subtraction (-
), multiplication (*
), division (/
), and modulus (%
). - Relational operators compare values and return either true (1) or false (0).
- Logical operators combine conditional statements using AND (
&&
), OR (||
), and NOT (!
). - Bitwise operators perform operations on individual bits of integers.
- Assignment operators assign values to variables, sometimes combining assignment with another operation.
- Miscellaneous operators include
sizeof
, address operator (&
), dereference operator (*
), and the ternary conditional operator (?:
). - Operator precedence determines the order in which operations are performed in an expression.
Understanding operators is essential for writing efficient and effective C programs. They form the basic building blocks for manipulating data and controlling program flow.
If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)