The scanf function reads data from standard input (usually, the terminal). A call to this function takes the general form
scanf(control_string, arg1, arg2, arg3, ...)where control
_
string refers to a character string containing certain required
formatting information, and arg1, arg2, etc., are arguments
that represent the individual input data items.
The control string consists of individual groups of characters, with one character
group for each data input item. In its simplest form, each character group
consists of a
percent sign (%
), followed by a set of conversion characters which
indicate the type of the corresponding data item. The two most
useful sets of conversion characters are as follows:
Character Type --------- ---- d int lf doubleThe arguments are a set of variables whose types match the corresponding character groups in the control string. For reasons which will become apparent later on, each variable name must be preceded by an ampersand (
&
).
Below is a typical application of the scanf function:
#include <stdio.h> . . . int k; double x, y; . . . scanf("%d %lf %lf", &k, &x, &y); . . .In this example, the scanf function reads an integer value and two floating-point values, from standard input, into the integer variable k and the two floating-point variables x and y, respectively.
The scanf function returns an integer equal to the number of data values successfully read from standard input, which can be fewer than expected, or even zero, in the event of a matching failure. The special value EOF (which on most systems corresponds to ) is returned if an end-of-file is reached before any attempted conversion occurs. The following code snippet gives an example of how the scanf function can be checked for error-free input:
#include <stdio.h> . . . int check_input; double x, y, z; . . . check_input = scanf("%lf %lf %lf", &x, &y, &z); if (check_input < 3) { printf("Error during data input\n"); . . . } . . .See later for an explanation of the if() construct.
The printf function writes data to standard output (usually, the terminal). A call to this function takes the general form
printf(control_string, arg1, arg2, arg3, ...)where control
_
string refers to a character string containing
formatting information, and arg1, arg2, etc., are arguments
that represent the individual output data items.
The control string consists of individual groups of characters, with
one character group for each output data item.
In
its simplest form, each character group
consists of a
percent sign (%
), followed by a conversion character which
controls the format of the corresponding data item. The most
useful conversion characters are as follows:
Character Meaning --------- ------- d Display data item as signed decimal integer f Display data item as floating-point number without exponent e Display data item as floating-point number with exponentThe arguments are a set of variables whose types match the corresponding character groups in the control string (i.e., type int for d format, and type double for f or e format). In contrast to the scanf function, the arguments are not preceded by ampersands. Below is a typical application of the scanf function:
#include <stdio.h> . . . int k = 3; double x = 5.4, y = -9.81; . . . printf("%d %f %f\n", k, x, y); . . .In this example, the program outputs the values of the integer variable k and the floating-point variables x and y to the terminal. Executing the program produces the following output:
3 5.400000 -9.810000 %Note that the purpose of the escape sequence
\n
in the control string is
to generate a line-feed after the three data items have been written to the
terminal.
Of course, the printf function can also be used to write a simple text string to the terminal: e.g.,
printf(text_string)Ordinary text can also be incorporated into the control string described above.
An example illustrating somewhat more advanced use of the printf function is given below:
#include <stdio.h> . . . int k = 3; double x = 5.4, y = -9.81; . . . printf("k = %3d x + y = %9.4f x*y = %11.3e\n", k, x + y, x*y); . . .Executing the program produces the following output:
k = 3 x + y = -4.4100 x*y = -5.297e+01 %Note that the final two arguments of the printf function are arithmetic expressions. Note, also, the incorporation of explanatory text into the control string.
The character sequence %3d
in the control
string indicates that the associated data item should be output as
a signed decimal integer occupying a field whose width is at least
3 characters. More generally, the character sequence %nd
indicates that the associated data item should be output as
a signed decimal integer occupying a field whose width is at least
n characters.
If the number of characters in the data item is less than
n characters, then the data item
is preceded by enough leading blanks to fill the specified field. On the
other hand, if the data item exceeds the specified field width then additional
space is allocated to the data item, such that the entire data item is displayed.
The character sequence %9.4f
in the control string indicates that
the associated data item should be output as a floating-point number, in
non-scientific format, which occupies a field of at least 9 characters, and
has 4 figures after the decimal point. More generally, the character
sequence %n.mf
indicates that the associated data item should be
output as a floating-point number, in non-scientific format, which occupies a field
of at least n characters, and
has m figures after the decimal point.
Finally, the character sequence %11.3e
in the control string indicates that
the associated data item should be output as a floating-point number, in
scientific format, which occupies a field of at least 11 characters, and
has 3 figures after the decimal point. More generally, the character
sequence %n.me
indicates that the associated data item should be
output as a floating-point number, in scientific format, which occupies a field
of at least n characters, and
has m figures after the decimal point.
The printf function returns an integer equal to the number of printed characters, or a negative value if there was an output error.
When working with a data file, the first step is to establish a buffer area, where information is temporarily stored whilst being transferred between the program and the file. This buffer area allows information to be read or written to the data file in question more rapidly than would otherwise be possible. A buffer area is established by writing
FILE *stream;where FILE (upper-case letters required) is a special structure type that establishes a buffer area, and stream is the identifier of the created buffer area. Note that a buffer area is often referred to as an input/output stream. The meaning of the asterisk (
*
) that precedes the identifier of the stream,
in the above statement,
will become clear later on. It is, of course, possible to establish
multiple input/output streams (provided that their identifiers are
distinct).
A data file must be opened and attached to a specific input/output stream before it can be created or processed. This operation is performed by the function fopen. A typical call to fopen takes the form
stream = fopen(file_name, file_type);where stream is the identifier of the input/output stream to which the file is to be attached, and
file_name
and file_type
are character strings that represent
the name of the data file and the manner in which the data file will be
utilized, respectively. The file_type
string must be one of the strings
listed below:
file_type Meaning --------- ------- "r" Open existing file for reading only "w" Open new file for writing only (Any existing file will be overwritten) "a" Open existing file in append mode. (Output will be appended to the file)The fopen function returns the integer value NULL (which on most systems corresponds to zero) in the event of an error.
A data file must also be closed at the end of the program. This operation is performed by the function fclose. The syntax for a call to fclose is simply
fclose(stream);where stream is the name of the input/output stream which is to be deattached from a data file. The fclose function returns the integer value 0 upon successful completion, otherwise it returns the special value EOF.
Data can be read from an open data file using the fscanf function, whose syntax is
fscanf(stream, control_string, arg1, arg2, arg3, ...)Here, stream is the identifier of the input/output stream to which the file is attached, and the remaining arguments have exactly the same format and meaning as the corresponding arguments for the scanf function. The return values of fscanf are similar to those of the scanf function.
Likewise, data can be written to an open data file using the fprintf function, whose syntax is
fprintf(stream, control_string, arg1, arg2, arg3, ...)Here, stream is the identifier of the input/output stream to which the file is attached, and the remaining arguments have exactly the same format and meaning as the corresponding arguments for the printf function. The return values of fprintf are similar to those of the printf function.
An example of a C program which outputs data to the file ``data.out'' is given below:
#include <stdio.h> . . . int k = 3; double x = 5.4, y = -9.81; FILE *output; . . . output = fopen("data.out", "w"); if (output == NULL) { printf("Error opening file data.out\n"); . . . } . . . fprintf(output, "k = %3d x + y = %9.4f x*y = %11.3e\n", k, x + y, x*y); . . . fclose(output); . . .On execution, the above program will write the line
k = 3 x + y = -4.4100 x*y = -5.297e+01to the data file ``data.out''.