Take a look at our sample ``joints.c'' C program again. Notice that
the lines in the main function all start in column 3--every line is
preceded by two spaces. Why do you think that we've put spaces in front of all
those lines?
Click here for the answer.
In C, curly braces (i.e., the characters { and }) are used to
group together other program statements. In our program, the curly braces
{} are used to group together the statements in the main function.
The lines between the braces are indented so that it's easier to see where the
main function starts and stops.
You might think that indentation isn't important. But as your programs get
more complicated and you use more {}'s to group together other lines,
it will become more obvious why it is important to use indentation to show how
different parts of the program are grouped together.
Every C statement ends with a `;'--a semicolon. Notice that we said
statement, not line. You can compare a C statement to an English
sentence:
- Just as a single English sentence can be spread over several lines in
a paragraph, a C statement can be divided between two or more lines in a
program.
- Every English sentence ends with a period. Similarly, every C
statement ends with a semicolon.
Take a look at the ``joints.c'' example program. Does every
non-comment line in the main function have a `;' at the end? What
about lines outside the main function?
You might wonder why some of the other lines in the program don't end with a
semicolon.
Click here for the reasons.
Eric N. Eide
Hamlet Project
Department of Computer Science
University of Utah