INPUT/OUTPUT PART ONE

TOPICS

CREDITS

Authored by: John Even

Faculty Supervisor: Al Day

Some material is derived from a lesson by: Dr. James Hilliard

HTML Documentation by: Michelle Roberts

Last Updated 7/24/96

COMPUTER INPUT/OUTPUT: NUMERICAL

Input
Terminal and File Input
    i) Formatted input
    ii) Default input

Output
Monitor and File Output
    i) Formatted output
    ii) Default output

FORMAT

CONVERSION SPECIFICATION

FORMATTED OUTPUT

Function
Instructs the computer to take variables and values from storage and output them according to an accompanying format statement.
Form

fp : File pointer variable
cs : Conversion Specification for corresponding variable
var : Variable names
Mesg : Any string of characters before or after the conversion specification

FORMAT FOR DIFFERENT VARIABLE TYPES

# is replaced by any whole number (e.g. 5)

# and #.# are completely optional and one (and/or the other for #.#) may be omitted for default output.

*Discussed in a Later Lesson.

HOW TO USE #'S FOR FORMATTED OUTPUT

For int & double:
The first # is the placeholder (or field width--the minimum number of columns reserved for the number. Numbers of smaller width than the placeholder are right-justified within the field reserved for the number.

For double:
The second # is the number of decimal places desired in the output.

Extra decimal places are rounded off. If the field width is too small, the number is printed with no blanks preceding it, and the number of columns used varies with the size of the number in its printed form.

EXAMPLE 1

printf("%4d     %10.3f", item, value);

The first 4 positions (more if the value is large) will contain the variable item right-justified. The next 5 positions will be left blank because of the string

"     ",
containing 5 spaces which was placed between the format statements. Finally, the next 10 positions will contai the variable value also right-justified.

b123bbbbbbb-429.775

EXAMPLE 2

fprintf(outp, "%10.5f%10.5f%4d%4d%4d", a, b, i, k, l);

If

The printed line would be:

EXAMPLE 3

fprintf(outp,  "%8.6f", a);

If a = 56.0

The printed line would be:

                         56.000000 

Notice that the line has "spilled" over its designated area.

%e SPECIFICATION

The %e conversion specification outputs a number in scientific notation.

EXAMPLE 1

printf("%14.6e", a); (a=-12.3456)

Computer Interpretation:
Outputs variable on current line.
Outputs variable as a double number, with exponent.
The mantissa is between 1 and 10 and the exponent is the appropriate power of 10.

LINE prints as: -1.234560e+001

EXAMPLE 2

printf("%9.2e %12.7e", c, d);

Let c = .0055 and d = 7654.321

The printed line would be:

	5.5e-.003 7.6543210e+003 

The numbers meshed together because no string was put between the numbers in the printf statement and the second value "spilled" over the reserved field width.

EXAMPLE 3

printf("%9.2e %16.7e", c, d);

Let c = .0055 and d = 7654.321

By formatting the field width at least 8 print positions greater than the desired number of decimal places, a value is guaranteed to fit in the reserved field.

Line prints as: 5.50e-003 bb7.6543210e+003