C Operators — Arithmetic, Relational, Logical, Assignment
Learning Objectives
- Apply arithmetic operators to compute values
- Compare values with relational operators
- Combine conditions with logical operators
- Use assignment operators and understand precedence
What Is an Operator?
An operator is a symbol that tells the compiler to perform a calculation or comparison. Operators work on values called operands. The result of an operation is either a new value or, in the case of assignment, an updated variable. C groups operators into several families that you will use constantly.
Arithmetic Operators
Arithmetic operators perform basic math. Most behave as you would expect from a calculator, with a few important differences to remember.
| Operator | Name | Example | Result |
|---|---|---|---|
| plus | Addition | 7 plus 3 | 10 |
| minus | Subtraction | 7 minus 3 | 4 |
| star | Multiplication | 7 times 3 | 21 |
| slash | Division | 7 divided by 3 | 2 integer |
| percent | Modulus | 7 mod 3 | 1 remainder |
int a = 17;
int b = 5;
printf("%d\n", a + b);
printf("%d\n", a - b);
printf("%d\n", a * b);
printf("%d\n", a / b);
printf("%d\n", a % b);
Integer division truncates toward zero, so 17 divided by 5 gives 3, not 3.4. The modulus operator returns the remainder after division and is extremely useful for cycling values or checking even and odd numbers.
Division by zero is undefined behavior in C. Always guard divisors before performing a division that could receive a zero.
Relational Operators
Relational operators compare two operands and produce an integer result of one when the condition is true and zero when it is false. You will use them inside conditionals constantly.
| Operator | Meaning |
|---|---|
| double equals | Equal to |
| exclamation equals | Not equal to |
| greater than | Larger value |
| less than | Smaller value |
| greater equals | Greater or equal |
| less equals | Less or equal |
int x = 10;
int y = 20;
printf("%d\n", x == y);
printf("%d\n", x != y);
printf("%d\n", x < y);
A classic beginner error is writing a single equals sign when you meant to compare. A single equals assigns a value, while double equals tests equality. Using assignment inside a condition often causes hard-to-spot bugs.
Logical Operators
Logical operators combine or invert conditions. C treats any non-zero value as true and zero as false. Short-circuit evaluation means the right side may be skipped entirely when the result is already decided.
int age = 25;
int hasTicket = 1;
if (age >= 18 && hasTicket == 1) {
printf("You may enter.\n");
}
if (age < 13 || age > 65) {
printf("Discount applies.\n");
}
if (!(age == 0)) {
printf("Age is not zero.\n");
}
The AND operator requires both sides to be true. The OR operator needs at least one true side. The NOT operator flips a condition. Together they let you express complex rules clearly.
Assignment Operators
Assignment stores a value in a variable. C also provides compound assignment operators that combine a calculation and a store into one step, keeping code compact and readable.
int score = 100;
score += 5;
score -= 10;
score *= 2;
score /= 4;
score %= 7;
Each compound form is shorthand for the longer expression. The plus-equals form adds then assigns, and the pattern continues for the other operators. Increment and decrement operators, written as two plus signs or two minus signs, add or subtract one from a variable.
Operator Precedence
When operators appear together, precedence decides which runs first. Multiplication and division bind tighter than addition and subtraction. Comparisons run after arithmetic, and logical AND binds tighter than logical OR. Parentheses override the default order and make intent obvious.
int result = 2 + 3 * 4;
printf("%d\n", result);
int clearer = (2 + 3) * 4;
printf("%d\n", clearer);
The first expression prints 14 because multiplication happens first. The second prints 20 because parentheses force addition first. When precedence feels unclear, add parentheses rather than guessing.
📝Precedence Tip
Read complex expressions from the inside out. If you cannot explain the evaluation order quickly, rewrite the line with explicit parentheses or split it into intermediate variables.
Key Takeaways
💡
- Arithmetic operators include addition, subtraction, multiplication, division, and modulus
- Integer division truncates and division by zero is undefined
- Relational operators return one for true and zero for false
- Logical AND, OR, and NOT combine conditions with short-circuit behavior
- Parentheses beat memorizing precedence tables for readable code
Practice Questions
- What is the result of 17 divided by 5 using integer division, and what does 17 mod 5 return?
- Why is using a single equals sign inside an if condition dangerous?
- How do compound assignment operators such as plus-equals improve readability?
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type — Always start by categorizing the problem
- Incorrect formula application — Verify the formula applies to your specific case
- Unit errors — Always check units are consistent throughout
- Sign errors — Pay careful attention to positive and negative values
- Rounding too early — Keep full precision until the final answer
- Forgetting constraints — Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic — Build on the foundation laid here
- Next Topic — See how this concept extends to more advanced material
- Practice Problems — Additional exercises with solutions
- Video Explanation — Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions