C Tutorial
C Operators
Arithmetic, comparison, and assignment operators work on values. Precedence decides what runs first.
Arithmetic
+, -, *, and / add, subtract, multiply, and divide.% is remainder after integer division. Both sides of % should be integers in this tutorial.
Example
#include <stdio.h>
int main(void) {
printf("%d\n", 10 + 3);
printf("%d\n", 10 - 3);
printf("%d\n", 10 * 3);
printf("%d\n", 10 / 3);
printf("%d\n", 10 % 3);
return 0;
}10 / 3 is 3 because both operands are int. The fractional part is dropped, not rounded. 10 % 3 is 1: three goes into ten three times, remainder one.
Run the listings in /c/try. gcc compiles them there. Do not use the Python editor at/try, the HTML preview at /html/try, or C++ at /cpp/try.
Increment and decrement
++ adds one. -- subtracts one. Written as a statement, n++ and++n both change n by one. This tutorial uses them on their own line so prefix and postfix do not matter yet.
After int n = 5;, the statement n++; leaves n equal to 6.n--; would take it back to 5.
Comparison
Comparisons produce 0 or 1. C treats 0 as false and any non-zero value as true. Use == to test equality. A single = assigns; it does not compare.
| Operator | Meaning |
|---|---|
== | equal |
!= | not equal |
< | less than |
> | greater than |
<= | less than or equal |
>= | greater than or equal |
Example
#include <stdio.h>
int main(void) {
int a = 7;
int b = 4;
printf("%d\n", a == b);
printf("%d\n", a != b);
printf("%d\n", a > b);
printf("%d\n", a < b);
return 0;
}Logic
&& is and: both sides must be true. || is or: at least one side is true.! is not: it flips true and false. Use parentheses when you mix them so the intended grouping is obvious.
Example
#include <stdio.h>
int main(void) {
int n = 8;
printf("%d\n", n > 0 && n < 10);
printf("%d\n", n < 0 || n > 100);
printf("%d\n", !(n == 8));
return 0;
}Compound assignment
+= adds and stores. -=, *=, /=, and %= work the same way. n += 2 means n = n + 2.
Example
#include <stdio.h>
int main(void) {
int n = 10;
n++;
n += 5;
n *= 2;
printf("%d\n", n);
return 0;
}Precedence
Multiplication and division run before addition and subtraction. 2 + 3 * 4 is 14, not20. Parentheses override the default: (2 + 3) * 4 is 20. When a line looks busy, add parentheses even if the default would match what you meant.
Next: C strings — char arrays that end with a null byte, not a C++ string.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.
Physics
Ohm’s law
For many metals at constant temperature, current is proportional to potential difference. V = I R. 20 mA through 220 Ω is 4.4 V.
Keep I in amps, not milliamps, unless you convert. 20 times 220 is 4400, which looks like a voltage until you remember the 20 was milliamps.
V = I R
Ohm’s law is a model for ohmic components. A filament lamp’s resistance rises as it heats.
Example
#include <stdio.h>
int main(void) {
double I = 0.020;
double R = 220.0;
double V = I * R;
printf("V = %.2f V\n", V);
return 0;
}Maths
Hours and leftover minutes
Integer division throws away the remainder. 125 / 60 is 2 hours. The leftover 5 minutes is 125 % 60. Together they reconstruct 125.
This is how you convert a total in a small unit into a mixed measurement. The same pair of operators splits a number of days into weeks and leftover days.
h = total / 60, min = total % 60
Example
#include <stdio.h>
int main(void) {
int total = 125;
printf("%d h %d min\n", total / 60, total % 60);
return 0;
}Biology
Body mass index
BMI is mass in kilograms divided by height in metres squared. 68 kg at 1.72 m is about 23, which tables call a typical adult range. The index does not know muscle from fat.
Comparisons in C produce 0 or 1. Printing bmi > 25.0 is a crude flag, not a diagnosis.
BMI = m / h²
BMI was built for populations, not for one athlete. Treat the cutoff as a teaching threshold.
Example
#include <stdio.h>
int main(void) {
double mass = 68.0;
double height = 1.72;
double bmi = mass / (height * height);
printf("BMI = %.1f\n", bmi);
printf("over 25? %d\n", bmi > 25.0);
return 0;
}