C Operators
Essential symbols for performing operations in C
🔢 What are C Operators?
C operators are special symbols that perform operations on variables and values. They help you do math, compare values, and make logical decisions in your programs.
// Simple operator example
int a = 10, b = 5;
int sum = a + b; // Addition operator
printf("Sum: %d", sum); // Output: 15
Types of C Operators
Arithmetic
Basic math operations
Relational
Compare two values
Logical
Combine conditions
Assignment
Assign values to variables
🔹 Arithmetic Operators
Arithmetic operators in C perform basic mathematical operations on numeric values and variables. The five fundamental operators are addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). For instance, int result = (10 + 5) * 2 - 8 / 4; demonstrates operator precedence where multiplication and division execute before addition and subtraction. The modulus operator returns the remainder of division, making it useful for checking even/odd numbers, implementing circular buffers, or creating cyclic patterns. Understanding operator precedence and using parentheses ensures calculations produce expected results in complex expressions.
#include
int main() {
int a = 15, b = 4;
printf("Addition: %d + %d = %d\n", a, b, a + b); // 19
printf("Subtraction: %d - %d = %d\n", a, b, a - b); // 11
printf("Multiplication: %d * %d = %d\n", a, b, a * b); // 60
printf("Division: %d / %d = %d\n", a, b, a / b); // 3
printf("Modulus: %d %% %d = %d\n", a, b, a % b); // 3
return 0;
}
Output:
Subtraction: 15 - 4 = 11
Multiplication: 15 * 4 = 60
Division: 15 / 4 = 3
Modulus: 15 % 4 = 3
🔹 Relational Operators
Relational operators compare two values and return either true (1) or false (0) based on the comparison result. C provides six relational operators: equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). For example, if (age >= 18) checks if someone is an adult, returning 1 for true or 0 for false. These operators are fundamental in conditional statements, loop conditions, and decision-making logic throughout C programs, enabling comparisons between integers, floating-point numbers, and character values.
#include
int main() {
int x = 10, y = 20;
printf("%d == %d is %d\n", x, y, x == y); // 0 (false)
printf("%d != %d is %d\n", x, y, x != y); // 1 (true)
printf("%d < %d is %d\n", x, y, x < y); // 1 (true)
printf("%d > %d is %d\n", x, y, x > y); // 0 (false)
printf("%d <= %d is %d\n", x, y, x <= y); // 1 (true)
printf("%d >= %d is %d\n", x, y, x >= y); // 0 (false)
return 0;
}
Output:
10 != 20 is 1
10 < 20 is 1
10 > 20 is 0
10 <= 20 is 1
10 >= 20 is 0
🔹 Logical Operators
Logical operators combine multiple conditions to create complex boolean expressions in C programming. The three logical operators are AND (&&), OR (||), and NOT (!). The AND operator returns true only when both conditions are true, OR returns true when at least one condition is true, and NOT inverts the boolean value. For example, if (age >= 18 && hasLicense == 1) checks if someone can drive by verifying both conditions simultaneously. These operators support short-circuit evaluation, meaning && stops evaluating if the first condition is false, optimizing performance in conditional statements and complex decision-making scenarios.
#include
int main() {
int a = 5, b = 10, c = 15;
// AND operator (&&)
if (a < b && b < c) {
printf("Both conditions are true\n");
}
// OR operator (||)
if (a > 20 || b < 15) {
printf("At least one condition is true\n");
}
// NOT operator (!)
if (!(a > b)) {
printf("a is NOT greater than b\n");
}
return 0;
}
Output:
At least one condition is true
a is NOT greater than b
🔹 Assignment Operators
Assignment operators in C assign values to variables and provide shorthand notation for common operations. The basic assignment operator (=) stores a value in a variable, while compound assignment operators like +=, -=, *=, /=, and %= combine arithmetic operations with assignment. For instance, x += 5; is equivalent to x = x + 5; but more concise and readable. These operators reduce code verbosity and make modifications clearer. Using compound operators also helps compilers optimize code execution, making them valuable in loops, counters, and accumulator patterns throughout C programming applications.
#include
int main() {
int num = 10;
printf("Initial value: %d\n", num);
num += 5; // Same as: num = num + 5
printf("After += 5: %d\n", num);
num -= 3; // Same as: num = num - 3
printf("After -= 3: %d\n", num);
num *= 2; // Same as: num = num * 2
printf("After *= 2: %d\n", num);
num /= 4; // Same as: num = num / 4
printf("After /= 4: %d\n", num);
return 0;
}
Output:
After += 5: 15
After -= 3: 12
After *= 2: 24
After /= 4: 6