Sams Teach Yourself C in 21 Days

(singke) #1
Enter your age.
29 and never older!
Enter your first name.
Bradley
Your age is 29.
Your name is Bradley.
As you can see in line 15, the fflush()function is being used. The prototype for
thefflush()function is as follows:
int fflush( FILE *stream);
Thestreamis the stream to be flushed. In Listing 14.8, the standard input stream,stdin,
is being passed for stream.

scanf()Examples
The best way to become familiar with the operation of the scanf()function is to use it.
It’s a powerful function, but it can be a bit confusing at times. Try it and see what hap-
pens. Listing 14.9 demonstrates some of the unusual ways to use scanf(). You should
compile and run this program and then experiment by making changes to the scanf()
format strings.

LISTING14.9 scanf.c. Some ways to use scanf()for keyboard input
1: /* Demonstrates some uses of scanf(). */
2:
3: #include <stdio.h>
4:
5: int main( void )
6: {
7: int i1;
8: int i2;
9: long l1;
10:
11: double d1;
12: char buf1[80]
13: char buf2[80];
14:
15: /* Using the l modifier to enter long integers and doubles.*/
16:
17: puts(“Enter an integer and a floating point number.”);
18: scanf(“%ld %lf”, &l1, &d1);
19: printf(“\nYou entered %ld and %lf.\n”,l1, d1);
20: puts(“The scanf() format string used the l modifier to store”);
21: puts(“your input in a type long and a type double.\n”);
22:
23: fflush(stdin);
24:

356 Day 14

INPUT/
OUTPUT

ANALYSIS

22 448201x-CH14 8/13/02 11:12 AM Page 356

Free download pdf