C Tutorial
C Output
printf sends text to the console. Format specifiers print numbers and strings. This is how every first program talks back.
printf writes to the console
printf is the standard way to print in C. You pass a format string, then any values that string refers to. Text goes in double quotes. After the program runs, you see those characters in the console — on StudyGrid, that is the output pane under the editor.
Example
#include <stdio.h>
int main(void) {
printf("Hello from printf\n");
return 0;
}Open examples with Try it in C. That is /c/try: a gcc compile-and-run editor.
Format specifiers
Placeholders in the format string tell printf how to print each argument. %d is a signed int. %s is a string. %f is a floating-point value (use it fordouble as well). The arguments after the string must match those placeholders, in order.
Example
#include <stdio.h>
int main(void) {
int score = 12;
double price = 2.5;
printf("Score: %d points\n", score);
printf("%d plus %d is %d\n", 3, 4, 3 + 4);
printf("price %f\n", price);
printf("%s\n", "done");
return 0;
}| Specifier | Typical type |
|---|---|
%d | int |
%f | double (or float) |
%s | string (a char array) |
%c | char |
Newlines with \n
\n is the newline character inside a string. Without it, the next printf continues on the same line. Two messages then glue together: HelloWorld instead of two words.
Example
#include <stdio.h>
int main(void) {
printf("first line\n");
printf("second line\n");
printf("third line\nfourth line\n");
return 0;
}\n is two characters in the source and one newline in the output. Do not put a real line break inside the quotes unless you want the source to span lines.
Spaces are your job
C does not insert spaces between printed values. If you need a gap, put it in the format string. A missing space is a missing character, not a compiler error.
| Code | What you see |
|---|---|
printf("%d%d", 2, 5); | 25 |
printf("%d %d", 2, 5); | 2 5 |
printf("n=%d", 3); | n=3 |
You need stdio.h
printf lives in #include <stdio.h>. Include that header at the top. Forgetting it is a common first error: gcc may warn, or refuse to compile, depending on flags.
Example
#include <stdio.h>
int main(void) {
char grade = 'A';
printf("%d\n", 7);
printf("%c\n", grade);
printf("%s\n", "same header, several specifiers");
return 0;
}Next: comments, so you can leave notes next to the lines that print.
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
Cells in series
Identical cells in series add their emfs. Four 1.5 V cells give 6.0 V, ignoring internal resistance. The count of cells is an int. The voltage is not.
%d prints the count. %.1f prints the voltage to one decimal place, which matches how those cells are labelled on the packet.
V_total = n × V_cell
Real batteries sag under load. This is the open-circuit sum, not what a motor would actually see.
Example
#include <stdio.h>
int main(void) {
int cells = 4;
double volts = 1.5 * cells;
printf("%d cells -> %.1f V\n", cells, volts);
return 0;
}Astronomy
A Mars year
Mars takes about 687 Earth days to go once around the Sun. That is a sidereal year, measured against the stars, not a calendar date on Mars.
printf can mix a name and an integer on one line. Mission logs look like this: an object, then a number, then a unit implied by the sentence.
Example
#include <stdio.h>
int main(void) {
printf("Mars year ~ %d Earth days\n", 687);
return 0;
}