C Tutorial
C Comments
Comments are for people. The compiler skips // lines and /* blocks */.
The compiler ignores comments
A comment is text you leave in the source so a human can follow the program. gcc does not compile it. It does not change output. Use comments to record why a line exists, not to repeat what the code already shows.
Example
#include <stdio.h>
int main(void) {
// This program greets the user once.
printf("Hello\n");
return 0;
}Run this in /c/try. The C editor compiles with gcc. Comments never appear in the output pane.
Line comments with //
Two slashes start a comment that runs to the end of that line. You can put // on its own line or after a statement. Everything after // on that line is skipped. C17 allows //; older C89 code used only block comments.
Example
#include <stdio.h>
int main(void) {
int n = 10; // starting inventory
printf("%d\n", n);
// printf("skip this line while testing\n");
printf("still running\n");
return 0;
}Commenting out a statement is a common way to disable it while you test. Uncomment it by deleting the// when you want that line back.
Block comments with /* */
/* starts a comment that can span several lines. */ ends it. Everything between those markers is skipped, including what would otherwise be code.
Example
#include <stdio.h>
int main(void) {
/*
Print a short header, then a number.
Block comments can cover more than one line.
*/
printf("total\n");
printf("%d\n", 42);
return 0;
}Blocks do not nest
You cannot put one /* ... */ inside another. The first */ ends the comment. The rest of the inner comment becomes ordinary code, and gcc reports a mess of errors.
If you wrap a region in /* */ and that region already contains */, the comment stops too early. Prefer // on each line when you disable a chunk of code.
What to write
Name the intent, the unit, or the rule a reader would miss. Do not narrate n = n + 1 as “add one.” Stale comments are worse than none: when you change the code, change the comment in the same edit.
| Weak | Useful |
|---|---|
count = count + 1; // increment count | count = count + 1; // skip the header row |
price = price * 0.9; // multiply | price = price * 0.9; // 10 percent loyalty discount |
Comments are not strings
Text in quotes is data. The program can print it. Text after // is not data. If you need the user to see a message, put it in printf, not in a comment.
Next: variables, so those printed numbers have names.
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
Newton’s second law, labelled
Force is mass times acceleration when mass is constant. In SI, mass is kilograms, acceleration is metres per second squared, and force comes out in newtons. 2 kg at 3.5 m/s² is 7 N.
The comment is for the next person who opens the file. Six months later, 3.5 without a unit is a guess. The compiler never reads those notes.
F = m a
Example
#include <stdio.h>
int main(void) {
/* F = m a force in newtons, mass in kg, acceleration in m/s^2 */
double mass = 2.0; // kg
double accel = 3.5; // m/s^2
double force = mass * accel;
printf("F = %.1f N\n", force);
return 0;
}Chemistry
Molar mass of water
H2O is two hydrogens and one oxygen. Using 1.0 and 16.0 g/mol you get 18.0 g/mol. School tables sometimes print 18.02; the extra 0.02 is the more precise hydrogen mass. For a titration write-up, 18 is the figure people use.
Put the assumption in a comment: approximate molar mass, not a measured value from this run.
M(H2O) ≈ 18 g/mol
Example
#include <stdio.h>
int main(void) {
// Water: H2O. Approximate molar mass used in school labs.
double molar_mass = 18.0; /* g/mol */
printf("H2O ~ %.1f g/mol\n", molar_mass);
return 0;
}