One More Example

I l @ ve RuBoard

One More Example

Let's run one more printing example, one that makes use of some of C's special escape characters . In particular, the program in Listing 3.9 shows how backspace ( \b ), tab ( \t ), and carriage return ( \r ) work. These concepts date from when computers used teletype machines for output and they don't always translate successfully to contemporary graphical interfaces. For example, this listing doesn't work as described on some Macintosh implementations .

Listing 3.9 The escape.c program.
 /* escape.c--uses escape characters */ #include <stdio.h> int main(void) {     float salary;     printf("Enter your desired monthly salary:");  /* 1 */     printf(" $_______\b\b\b\b\b\b\b");             /* 2 */     scanf("%f", &salary);     printf("\n\t$%.2f a month is $%.2f a year.", salary,                salary * 12.0);                     /* 3 */     printf("\rGee!\n");                            /* 4 */     return 0; } 

What Happens

Let's walk through this program step by step as it would work under an ANSI C implementation. The first printf() statement (the one numbered 1 ) prints the following:

 Enter your desired monthly salary: 

Because there is no \n at the end of the string, the cursor is left positioned after the colon . The second printf() statement picks up where the first one stops, so after it is finished, the screen looks like this:

 Enter your desired monthly salary: $_______ 

The space between the colon and the dollar sign is there because the string in the second printf() statement starts with a space. The effect of the seven backspace characters is to move the cursor seven positions to the left. This backs the cursor over the seven underline characters, placing the cursor directly after the dollar sign. Usually, backspacing does not erase the characters that are backed over, but some implementations may use destructive backspacing, negating the point of this little exercise.

At this point, you type your response, say 2000.00 . Now the line looks like this:

 Enter your desired monthly salary: 00.00 

The characters you type replace the underline characters, and when you press Enter (or Return) to enter your response, the cursor moves to the beginning of the next line.

The third printf() statement output begins with \n\t . The newline character moves the cursor to the beginning of the next line. The tab character moves the cursor to the next tab stop on that line, typically to column 9. Then the rest of the string is printed. After this statement, the screen looks like this:

 Enter your desired monthly salary: 00.00         00.00 a month is 000.00 a year. 

Because the printf() statement doesn't use the newline character, the cursor remains just after the final period.

The fourth printf() statement begins with \r . This positions the cursor at the beginning of the current line. Then Gee! is displayed there, and the \n moves the cursor to the next line. The final appearance of the screen is this:

 Enter your desired monthly salary: 00.00 Gee!    00.00 a month is 000.00 a year. 

A Possible Problem

Some older C implementations do not work as we've just described. The problem lies in when printf() actually sends output to the screen. In general, printf() statements send output to an intermediate storage area called a buffer . Every now and then, the material in the buffer is sent to the screen. Under ANSI C, the rules for when output is sent from the buffer to the screen are clear. It is sent when the buffer gets full or when a newline character is encountered or when there is impending input. (This is called flushing the buffer .) For instance, the first two printf() statements don't fill the buffer and don't contain a newline, but they are immediately followed by a scanf() statement asking for input. That forces the printf() output to be sent to the screen.

Some older C implementations, however, do not invoke the third condition (impending input) for flushing the buffer. If you run Listing 3.9 with one of these compilers, the output of the first two printf() statements remains in the buffer. If you type a response anyway and then press Enter, the newline generated by the Enter key flushes the buffer. You have to type your answer before you see the question! One solution to this awkward situation is to use a newline at the end of the printf() statement preceding the input. That newline will flush the buffer. The code can be changed to look like this:

 printf("Enter your desired monthly salary:\n"); scanf("%f", &salary); 

This code works whether or not impending input flushes the buffer. However, it also puts the cursor on the next line, preventing you from entering data on the same line as the prompting string. A sample run would look like this:

 Enter your desired monthly salary: 2000.00 

To maintain greater portability, we'll follow this model (with the response on the line following the prompt) for the rest of this book. Another solution is to use the fflush () function described in Chapter 12, "File Input/Output."

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net