Chapter 9 - Formatted Input/Output: Outline
Chapter 9 - Formatted Input/Output: Outline
Outline
9.1 Introduction
9.2 Streams
9.3 Formatting Output with printf
9.4 Printing Integers
9.5 Printing Floating-Point Numbers
9.6 Printing Strings and Characters
9.7 Other Conversion Specifiers
9.8 Printing with Field Widths and Precisions
9.9 Using Flags in the printf Format-Control String
9.10 Printing Literals and Escape Sequences
9.11 Formatting Input with scanf
• In this chapter
– Presentation of results
– scanf and printf
– Streams (input and output)
• gets, puts, getchar, putchar (in <stdio.h>
• printf
– precise output formatting
• Conversion specifications: flags, field widths, precisions, etc.
– Can perform rounding, aligning columns, right/left justification,
inserting literal characters, exponential format, hexadecimal
format, and fixed width and precision
• Format
printf( format-control-string, other-arguments );
– format control string: describes output format
– other-arguments: correspond to each conversion specification in
format-control-string
• each specification begins with a percent sign, ends with conversion
specifier
• Integer
– Whole number (no decimal point): 25, 0, -9
– Positive, negative, or zero
– Only minus sign prints by default (later we shall change this)
455
455 Program Output
455
-455
32000
2000000000
707
455
65081
1c7
1C7 2000 Prentice Hall, Inc. All rights reserved.
9.5 Printing Floating-Point Numbers
• Floating Point Numbers
– Have a decimal point (33.5)
– Exponential notation (computer's version of scientific notation)
• 150.3 is 1.503 x 10² in scientific
• 150.3 is 1.503E+02 in exponential (E stands for exponent)
• use e or E
– f - print floating point with at least one digit to left of decimal
– g (or G) - prints in f or e(E) with no trailing zeros (1.2300
becomes 1.23)
• Use exponential if exponent less than -4, or greater than or equal to
precision (6 digits by default)
A
This is a string Program Output
This is a string
This is also a string
• Field width
– Size of field in which data is printed
– If width larger than data, default right justified
• If field width too small, increases to fit data
• Minus sign uses one character position in field
– Integer width inserted between % and conversion specifier
– %4d - field width of 4
1427
1427.00
• Printing Literals
– Most characters can be printed
– Certain "problem" characters, such as the quotation
mark "
– Must be represented by escape sequences
• Represented by a backslash \ followed by an escape character
• Format
scanf(format-control-string, other-arguments);
– format-control-string - describes formats of inputs
– other-arguments - pointers to variables where input will be stored
– can include field widths to read a specific number of characters
from the stream
Integers
d Read an optionally signed decimal integer. The corresponding argument is a pointer to integer.
i Read an optionally signed decimal, octal, or hexadecimal integer. The corresponding argument is a pointer to integer.
o Read an octal integer. The corresponding argument is a pointer to unsigned integer.
u Read an unsigned decimal integer. The corresponding argument is a pointer to unsigned integer.
x or X Read a hexadecimal integer. The corresponding argument is a pointer to unsigned integer.
h or l Place before any of the integer conversion specifiers to indicate that a short or long integer is to be input.
Floating-point numbers
e, E, f, g or G Read a floating-point value. The corresponding argument is a pointer to a floating-point variable.
l or L Place before any of the floating-point conversion specifiers to indicate that a double or long double value is to be input.
Characters and strings
c Read a character. The corresponding argument is a pointer to char, no null ('\0') is added.
s Read a string. The corresponding argument is a pointer to an array of type char that is large enough to hold the string and a
terminating null ('\0') character—which is automatically added.
Scan set
[scan characters Scan a string for a set of characters that are stored in an array.
Miscellaneous
p Read an address of the same form produced when an address is output with %p in a printf statement.
n Store the number of characters input so far in this scanf. The corresponding argument is a pointer to integer
% Skip a percent sign (%) in the input.