C Tutorial
C Data Types
int, double, char, and _Bool store different kinds of values. Pick the type that matches the data.
The type is a contract
Every variable has a type. The type says what kind of value fits and how printf and operators treat it. Choose int for whole counts, double for measurements, char for one glyph, and _Bool (or bool from #include <stdbool.h>) for yes or no.
Example
#include <stdio.h>
#include <stdbool.h>
int main(void) {
int seats = 24;
double length_m = 1.75;
char row = 'C';
bool open = true;
printf("%d\n", seats);
printf("%f\n", length_m);
printf("%c\n", row);
printf("%d\n", open);
return 0;
}bool prints with %d as 1 or 0. That is normal forprintf.
Compile this at /c/try with gcc. That editor is only for C. Python lives at /try. HTML lives at /html/try. C++ lives at /cpp/try.
A table of common types
| Type | Holds | Example | Header |
|---|---|---|---|
int | Whole numbers | 42, -3 | built in |
double | Floating-point numbers | 3.14, -0.5 | built in |
char | One character | 'A', '7' | built in |
_Bool / bool | 0 or 1, false or true | 0, true | stdbool.h for bool |
char[] | Text ending in a null byte | "hello" | built in; string.h to measure |
C has no string type like C++. Text is a char array with a '\0' at the end. The strings chapter covers that in full.
sizeof reports bytes
sizeof tells you how many bytes a type (or a variable) uses. The number can differ between compilers and machines. On the StudyGrid gcc setup you will typically see int as 4 bytes anddouble as 8.
Example
#include <stdio.h>
int main(void) {
printf("char %zu\n", sizeof(char));
printf("int %zu\n", sizeof(int));
printf("double %zu\n", sizeof(double));
printf("_Bool %zu\n", sizeof(_Bool));
return 0;
}You almost never pick a type because of sizeof at this stage. You pick it because of the kind of value. sizeof is here so you know the tool exists. %zu is the specifier forsizeof’s result.
int versus double
Integer division truncates toward zero: 7 / 2 is 3. If either side is adouble, you get a fractional result: 7.0 / 2 is 3.5. Useint for counts. Use double for measurements in this tutorial.
Example
#include <stdio.h>
int main(void) {
printf("%d\n", 7 / 2);
printf("%f\n", 7.0 / 2);
return 0;
}char is not a string
A char holds one character in single quotes: 'A'. A string literal in double quotes — "ABC" — is an array of characters plus a null terminator. Mixing them up is a type error.
C also has float, long, and unsigned. This track stays withint, double, char, and booleans until you have a reason to switch. Prefer double over float unless you are told otherwise.
Next: constants, so some of these values can be named and locked.
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.
Chemistry
Atomic number versus pH
Atomic number is a count of protons. Oxygen has 8. That value is an integer; you never have 8.3 protons in a nucleus.
pH is −log10 of hydrogen-ion activity. Rainwater around 5.6 is weakly acidic because of dissolved CO2, not because the rain is “polluted” by default. It needs a double.
pH = −log₁₀[H⁺]
Example
#include <stdio.h>
int main(void) {
int protons = 8; /* oxygen */
double ph = 5.6; /* rainwater, roughly */
printf("O has %d protons\n", protons);
printf("pH = %.1f\n", ph);
return 0;
}Physics
A kelvin reading and its unit
Absolute temperature is kelvin. 293.15 K is 20 °C. The number and the scale are different kinds of data: one is a measurement, the other is a label.
char holds a single character. Using K instead of a string is enough when the scale cannot be “kelvin” spelled out.
T(K) = t(°C) + 273.15
Example
#include <stdio.h>
int main(void) {
double temp = 293.15;
char scale = 'K';
printf("%.2f %c\n", temp, scale);
return 0;
}