Programming in C - Keywords
(Page 5 of 9 )
In C, a keyword is a word with special significance, so you shouldn’t use keywords for any other purpose in your program. For this reason, keywords are also referred to as reserved words. In the preceding example, void is a keyword. C has several keywords, and you’ll become familiar with more of them as you learn more of the language. You’ll find a complete list of C keywords in Appendix C.
The Body of a Function
The general structure of the function main() is illustrated in Figure 1-2.

Figure 1-2. Structure of the function main ()
The function body is the bit between the opening and closing braces that follow the line where the function name appears. The function body contains all the statements that define what the function does. The example’s function main() has a very simple function body consisting of just one statement:
| { | | /* This marks the beginning of main() */ |
|---|
| printf("Beware the Ides of March!"); | /* This line displays a quotation | */ |
| } | | /* This marks the end of main() | */ |
Every function must have a body, although the body can be empty and just consist of the two braces without any statements between them. In this case, the function will do nothing.
You may wonder where the use is for a function that does nothing. Actually, this can be very useful when you’re developing a program that will have many functions. You can declare the set of (empty) functions that you think you’ll need to write to solve the problem at hand, which should give you an idea of the programming that needs to
12 be done, and then gradually create the program code for each function. This technique helps you to build your program in a logical and gradual manner.
NOTE You can see that I’ve aligned the braces one below the other. I’ve done this to make it clear where the block of statements that the braces enclose starts and finishes. Statements between braces are usually indented by a fixed amount—usually two or more spaces so that the braces stand out. This is good programming style, as the statements within a block can be readily identified.
Outputting Information The body of the example’s function main() includes only one statement, which calls the printf() function:
printf("Beware the Ides of March!"); /* This line displays a quotation */
As I said, printf() is a standard library function, and it outputs information to the display screen based on what appears between the parentheses that immediately follow the function name. In this case, the call to the function displays a simple piece of Shakespearean advice that appears between the double quotes. Notice that this line does end with a semicolon.
Arguments Items enclosed between the parentheses following a function name, as with the printf() function, are called arguments. When there is more than one argument to a function, they must be separated by commas.
If you don’t like the quotation in this example’s argument, you could display something else by simply including your own choice of words, within double quotes, between the parentheses. For instance, you might prefer a line from Macbeth:
printf("Out, damned Spot! Out I say!");
Try using this in the example. When you’ve modified the source code, you need to compile and link the program again before executing it.
NOTE As with all executable statements in C (as opposed to defining or directive statements) the printf() line must have a semicolon at the end. As you’ve seen, a very common error, particularly when you first start programming in C, is to forget the semicolon.
Control Characters You could alter the program to display two sentences on separate lines. Try typing in the following code:
/* Program 1.4 Another Simple C Program - Displaying a Quotation */
#include <stdio.h>
void main() {
printf("\nMy formula for success?\nRise early, work late, strike oil."); }
The output from this program looks like this:
My formula for success? Rise early, work late, strike oil.
Look at the printf() statement. At the beginning of the text and after the first sentence, you insert the characters \n. The combination \n actually represents one character: a newline character.
The backslash (\) is of special significance in a text string. It indicates the start of an escape sequence. Escape sequences are used to insert characters in a string that would otherwise be impossible to specify, such as tab and newline, or would confuse the compiler, such as a double quote that you use to delimit a string. The character following the backslash indicates what character the escape sequence represents. In this case, it’s n for newline, but there are plenty of other possibilities. Obviously, if a backslash is of special significance, you need a way to specify a backslash in a text string. To do this, you simply use two backslashes: \\. Similarly, if you actually want to display a double quote character, you can use \".
Type in the following program:
/* Program 1.5 Another Simple C Program - Displaying Great Quotations */
#include <stdio.h>
void main() { printf("\n\"It is a wise father that knows his own child.\" Shakespeare"); }
The output displays the following text:
"It is a wise father that knows his own child." Shakespeare
You can use the \a escape sequence in an output string to sound a beep to signal something interesting or important. Enter and run the following program:
/* Program 1.6 A Simple C Program – Important */ #include <stdio.h>
void main() {
printf("\nBe careful!!\a"); }
The output of this program is sound and vision. Listen closely and you should hear a beep through the speaker in your computer.
Be careful!!
The \a sequence represents the “bell” character. Table 1-1 shows a summary of the escape sequences that you can use.
Table 1-1. Escape Sequences
Escape Sequence | Description |
\n | Represents a newline character |
\r | Represents a carriage return |
\b | Represents a backspace |
\f | Represents a form-feed character |
\t | Represents a horizontal tab |
\v | Represents a vertical tab |
\a | Inserts a bell (alert) character |
\" | Inserts a double quote (") |
\' | Inserts a single quote (') |
\\ | Inserts a backslash (\) |
Try displaying different lines of text on the screen and alter the spacing within that text. You can put words on different lines using \n, and you can use \t to space the text. You’ll get lot more practice with these as you progress through the book.
This article is excerpted from Beginning C by Ivor Horton (Apress, 2004; ISBN 1590592530). Check it out at your favorite bookstore today. Buy this book now. |
Next: Developing Programs in C >>
More Code Examples Articles
More By Apress Publishing