next up previous contents index
Next: Reading and Writing: disk Up: Some hilights of the Previous: String functions   Contents   Index


Reading and Writing: the screen and keyboard

The C library provides three streams: stdin, stdout and stderr for input and output. stdin is an input stream, used to receive input from the user. stdout is the normal output stream, and stderr is a second output stream, traditionally used to report error messages (or other unexpected output).

Usually stdin collects characters typed at the keyboard, while characters sent to stdout are echoed to the screen. On some systems, stdin and stdout can be made to read to and write from files: running the command my_prog <input.txt runs my_prog, but empties the contents of the file input.txt into stdin rather than waiting for keyboard input. Such systems usually also let you redirect stdout and stderr, which is useful if you want to save or printout your program's output: my_prog ›input.txt >output.txt 2>errors.txt would redirect all three streams to appropriate files.

It is possible that output to the screen produced by one line does not appear before the next line of the program executes. If this is going to be a problem, (for example if you're writing out a prompt and then reading input from the user, you need the prompt to appear before waiting for input) use fflush (see below).


printf

printf is the usual method for writing information to the screen (similar to Pascal's Write and Writeln). (Declared in stdio.h.)

A simple use of printf might look like

  
printf("Some message.");   /*  argument is a string  */
This copies its argument to the standard output stream, stdout (usually characters sent to stdout will be sent to the screen). To write the value of a variable, use a % sign followed by a letter indicating the type of the variable

  
int i;
/* .. */
printf("Iteration %d", i);  /*  is the newline character
                             (see section on the char type for more info)  */
To write the value of variables of other types, use %f for doubles and floats, %c for chars, and %s for strings. (There are conversions for other types, and for each type you can request that the variable be displayed in a number of different formats, but we will not describe them all here.)

You can write the value of several variables at once:

  
int i;
char names[2][80] = { "test", "main run" };
double val;
int iter;
/* .. */
printf("Problem %s: iteration %d, value=%f", names[i], iter, val);
or specify a field with and precision (see a reference manual for more detailed information):

  
int iter;
double val;
/* .. */
printf("Iteration %10i", iter);  /*  use a field-width of 10 characters  */
printf("value %10.5f", val);  /*  field-width of 10,  5 decimal places  */


scanf

scanf reads and interprets characters from the standard input stream stdin, which usually receives what the user types at the keyboard. (Declared in stdio.h.)

Simple uses of scanf might look like

  
int i, j, k;
/* .. */
scanf("%d", &i);  /*  read an integer into i  */
/* .. */
scanf("%d %d %d", &i, &j, &k);  /*  read in 3 integers, 
                                    separated by whitespace */
To read in other types, use %f for floats, %lf for doubles (it is a common error to use %f for doubles - unlike printf, scanf will read the wrong number), and %s for strings. When reading in strings, the input stream will be split up into `words' (sequences of non-whitespace characters), and each one will be stored in a separate string argument.

  
double r;
char word1[100],word2[100];
/* .. */
scanf("%lf", &r);  /*  read in a double  */
scanf("%s %s", word1, word2);  /*  read in two words (both must be
                                   shorter than 100 characters  */


fflush

fflush flushes a stream (waits until all pending output is written). (Declared in stdio.h.) The most common use is:

  
double sigma;
/* .. */
printf("Enter the value for sigma: ");
fflush(stdout);  /*  wait for the prompt to be written on the screen  */
scanf("%lf",&sigma);
fflush can be used with any stream open for writing.


next up previous contents index
Next: Reading and Writing: disk Up: Some hilights of the Previous: String functions   Contents   Index
CATAM admin 2010-02-23