After storing values with variables and types, we need to calculate with them and compare them. In C, the type of each value can change the result—even a simple division. This post pairs operators with type conversion so you can always explain why an expression produced its final value.
New Terms In This Post
- Expression: any chunk of code that evaluates to a value (
a + b,score >= 80, etc.) - Arithmetic operator: operators that perform math such as addition, subtraction, multiplication, division
- Comparison operator: operators that compare two values and return true/false
- Type conversion: interpreting a value as another type
- Casting: explicitly telling the compiler to convert a value, e.g.
(double)count
Core Ideas
Study Notes
- Time required: 45–60 minutes
- Prerequisites: declared variables, used
printf, and triedsizeof- Goal: understand how operator precedence and type conversion affect results
Operators do the math or comparisons. Type conversion decides which type rules apply during those operations. Learn them together so the output never feels mysterious.
We will focus on five skills:
- Use
+,-,*,/,%for arithmetic - Build comparisons with comparison operators
- Distinguish integer vs. floating-point division
- Separate implicit conversion from explicit casting
- Use compound assignment and increment/decrement for quick updates
Follow Along in Code
Compute with Arithmetic Operators
Start with the basics:
#include <stdio.h>
int main(void) {
int a = 12;
int b = 5;
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
return 0;
}
/ divides and % computes the remainder. Because both operands are int, a / b performs integer division and discards the fraction, so 12 / 5 becomes 2.
Compare Values
Comparison operators feed directly into the conditionals from the next post.
#include <stdio.h>
int main(void) {
int score = 85;
printf("score >= 80 -> %d\n", score >= 80);
printf("score == 100 -> %d\n", score == 100);
printf("score != 60 -> %d\n", score != 60);
return 0;
}
printf shows these results as 1 or 0 because we print them with %d. In C, comparisons often appear as integers—1 for true, 0 for false.
Integer vs. Floating-Point Division
This is the most common early pitfall, and it surprises almost everyone once.
#include <stdio.h>
int main(void) {
int total = 7;
int count = 2;
printf("int division = %d\n", total / count);
printf("float division = %.2f\n", (double)total / count);
return 0;
}
The first print uses integer division: 7 / 2 becomes 3. The second casts total to double, so the division runs in floating-point and produces 3.50.
Keep this rule in mind:
7 / 2 -> 3, 7.0 / 2 -> 3.5
If any operand is floating-point, the result typically becomes floating-point as well. More complex rules appear with signed/unsigned mixes. For example, if a signed integer mixes with an unsigned integer, the signed value can be converted to unsigned, which may produce unexpected results when the signed value is negative.
Implicit Conversion vs. Casting
Conversions happen automatically or because you demanded them.
#include <stdio.h>
int main(void) {
int count = 3;
double price = 2.5;
double total = count * price;
double average = (double)7 / 2;
printf("total = %.1f\n", total);
printf("average = %.2f\n", average);
return 0;
}
count * price triggers an implicit conversion because price is wider. (double)7 / 2 is an explicit cast—your code instructs the compiler to treat 7 as a double.
Remember:
- Implicit conversion: the compiler automatically widens or narrows values during an expression according to C's conversion rules, which can sometimes lose precision.
- Casting: you direct the conversion with syntax like
(double).
Compound Assignment and Increment/Decrement
Shorter notation helps when you change a value repeatedly.
#include <stdio.h>
int main(void) {
int level = 1;
level = level + 2;
printf("level = %d\n", level);
level += 3;
printf("level = %d\n", level);
level++;
printf("level = %d\n", level);
return 0;
}
level += 3 equals level = level + 3. level++ adds 1. Avoid code like value++ + ++value for now; stick to simple increments until the concept is solid.
Practical Example: Average Score
#include <stdio.h>
int main(void) {
int math = 80;
int english = 91;
int science = 87;
int total = math + english + science;
double average = (double)total / 3;
printf("total = %d\n", total);
printf("average = %.2f\n", average);
printf("average >= 85 -> %d\n", average >= 85.0);
return 0;
}
This combines addition, division, casting, and comparison. Real programs mix operators like this constantly, so practice reading each piece separately.
Why This Matters
Operators and conversions feed every conditional, loop, and function call. If you gloss over why a calculation changed, the next post’s conditions will also feel blurry. Keep these five points:
- Arithmetic operators compute values; comparison operators create true/false signals.
- Integer division drops the fractional part.
- Adding floating-point operands or casting changes how results are interpreted.
+=,++, and friends appear everywhere in loops and state updates.- Always ask “which type is this expression using?” while reading code.
Practice in CodeSandbox
The sandbox below uses CodeSandbox's Universal starter. For C, the key learning loop is still compile and run in the terminal, so recreate the lesson code as a source file and repeat that cycle directly.
💬 댓글
이 글에 대한 의견을 남겨주세요