Programming in C - Try It Out: Exercising What You Know
(Page 8 of 9 )
Let’s now look at an example that puts into practice what you’ve learned so far. First, have a look at the following code and see whether you can understand what it does without running it. Then type it in; compile, link, and run it; and see what happens.
/* Program 1.7 A longer program */
#include <stdio.h> /* Include the header file for input and output */
void main()
{
printf("Hi there!\n\n\nThis program is a bit");
printf(" longer than the others.");
printf("\nBut really it's only more text.\n\n\n\a\a");
printf("Hey, wait a minute!! What was that???\n\n");
printf("\t1.\tA bird?\n");
printf("\t2.\tA plane?\n");
printf("\t3.\tA control character?\n");
printf("\n\t\t\b\bAnd how will this look when it prints out?\n\n");
}
The output will be as follows:
Hi there!
This program is a bit longer than the others.
But really it's only more text.
Hey, wait a minute!! What was that???
- A bird?
- A plane?
- A control character?
And how will this look when it prints out?
The program looks a little bit complicated, largely because the text strings between parentheses include a lot of escape sequences. Each text string is bounded by a pair of double quotation marks. However, the program is just a succession of calls to the printf() function, and it demonstrates that output to the screen is controlled by what you pass to the printf() function. Let’s look at this program in detail.
You include the stdio.h file through the preprocessing directive:
#include <stdio.h> /* Include the header file for input and output */
You can see that this is a preprocessing directive because it begins with #. The stdio.h file provides the definitions you need to be able to use the printf() function.
You then define the start of the function main() and specify that it doesn’t return a value with this line:
void main()
The opening brace on the next line indicates that the body of the function follows:
{
The next statement calls the standard library function printf() to output Hi there! to your display screen, followed by two blank lines and the phrase This program is a bit.
printf("Hi there!\n\n\nThis program is a bit");
The two blank lines are produced by the three \n escape sequences. Each of these starts a new line when the characters are written to the display. The first ends the line containing Hi there!, and the next two produce the two empty lines. The text This program is a bit appears on the fourth line of output. You can see that this one line of code produces a total of four lines of output on the screen.
The next line of output produced by the next printf() starts at the character position immediately following the last character in the previous output.
The next statement outputs the text longer than the others. with a space as the first character of the text:
printf(" longer than the others.");
This output will simply continue where the last line left off, following the t in bit. This means that you really do need the space at the beginning of the text, otherwise the computer will display This program is a bitlonger than the others, which isn’t what you want.
The next statement starts its output on a new line, immediately following the previous line, because of the \n at the beginning of the text string between double quotation marks:
printf("\nBut really it's only more text.\n\n\n\a\a");
It then displays the text and adds two empty lines (because of the three \n escape sequences) and beeps twice. The next output to the screen will start at the beginning of the line that follows the second empty line produced here.
The next output is produced by the following statement:
printf("Hey, wait a minute!! What was that???\n\n");
This outputs the text and then leaves one empty line. The next output will be on the line following the empty line.
Each of the next three statements inserts a tab, displays a number, inserts another tab followed by some text, and ends with a new line. This is useful for making your output easier to read:
printf("\t1.\tA bird?\n"); printf("\t2.\tA plane?\n"); printf("\t3.\tA control character?\n");
This produces three numbered lines of output.
The last statement that produces output adds a new line, so that there will be an empty line after the previous output. Two tabs are then sent to the display, followed by two backspaces, which moves your cursor back two spaces from the last tab position. Lastly, the text is displayed, and two newline characters are sent to the display:
printf("\n\t\t\b\bAnd how will this look when it prints out?\n\n");
The closing brace marks the end of the function body:
}
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: Common Mistakes >>
More Code Examples Articles
More By Apress Publishing