Input/Ouput (i/o) in C
Download
Report
Transcript Input/Ouput (i/o) in C
CS1061 C Programming
Lecture 16: Formatted I/0
A. O’Riordan, 2004
Input/Ouput (i/o) in C
C has extensive facilities for input and output (i/o) processing.
unlike most programming languages the i/o facilities are not part of the core
language but part of the standard library.
The i/o facilities are
character and line i/o
formatted i/o (and string versions)
formatted file i/o
low-level file i/o
First we focus on formatted i/o and the library functions printf() and scanf().
You will need to include the header file stdio.h.
printf()
printf() (print formatted) generates output under the control of a format string
(its first argument) which consists of literal characters to be printed and also
special character sequences called format specifiers (conversions).
Conversions request that other arguments be inserted into the string.
Format
printf(format-control-string, other-arguments);
format-control-string describes output format
other-arguments correspond to each conversion specification in formatcontrol-string
Each specification begins with a percent sign %.
Format Specifiers for printf()
Here are format specifiers for printf():
%d print an int argument in decimal
%ld print a long int argument in decimal
%c print a character
%s print a string
%f print a float or double argument
%e same as %f, but use exponential notation
%g use %e or %f, whichever is better
%o print an int argument in octal (base 8)
%x or %X
print an int argument in hexadecimal (base 16)
%% print a single %
Format Specifiers for printf() (2)
%g prints with no trailing zeros (1.2300 becomes 1.23).
%E and %G are similar to %e and %g respectively but they display a capital E
when printing in exponential format
Here are some other format specifiers that are part of the standatd but they are
rarely used:
%n corresponding argument must be of type *int, into which gets stored the
number of characters read so far. Nothing is actually printed.
%p corresponding argument must be of type *void, allows you to display a
memory address (in hexadecimal format).
Field Width and Precision
specify the width and precision of numbers as they are inserted
Field width- size of field in which data is printed. (Minus sign uses one character
position).
If width larger than data, data is right-justified by default; if field width too small
it increases to fit data.
Width inserted between % and conversion specifier. Example: %4d.
Floating point precision- number of digits to appear after decimal (e and f
specifier).
Format: A dot . followed by precision after the % character, e.g. %.3f specifies a
precision of 3.
Field width and precision can both be specified, %width.precision, e.g. %5.3f
Flags
Flags supplement formatting capabilities
Place flag immediately to the right of percent sign
Here are two commonly used flag:
0
converted argument left-justified (default is right justification)
zeros used instead of spaces to pad a field
Example:
int i = 123;
printf("%05d %-5d", i, i) will display "00123 123
"
scanf()
Formatting input with scanf() function is used to read information from the
keyboard.
Format:
scanf(format-control-string, other-arguments);
format-control-string describes formats of inputs
other-arguments - list of pointers to variables where input will be stored. The
scanf() needs to know the address of the variables it is using.
A scanf() does not display anything, the control string is only for the conversion
character representing the variable you want to store.
Format Specifiers for scanf()
Conversions (format specifiers) similar to printf(), notice though that double is
different.
%c
character
%d
integer
%o
octal integer
%x or %X hexadecimal integer
%f
floating point number
%lf
double floating point number
%s
string of characters
NOTE: Users must press enter key after they type their input.
scanf() continued.
The control string may also contain
white space - matches white space in input stream. Note if several consecutive
whitespace characters appear in the control string, the effect is the same as if
only one had appeared.
ordinary characters (other than % or format specifier) - must match input
stream exactly.
Return values:
printf() returns the number of characters printed. scanf() returns the number of
things read, e.g.:
count = scanf("%d%d",&x,&y);
The scanf() function is notoriously problematic and some C programmers don't
use it at all, preferring character and line input (see later)!