






















Preview text:
1
Chapter 9 - Formatted Input/Output Outline 9.1 Introduction 9.2 Streams 9.3
Formatting Output with pr  r i  i nt  t f 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 p  r  i n  t  f Format-Control String 9.10
Printing Literals and Escape Sequences 9.11
Formatting Input with s c anf
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 Objectives
• In this chapter, you will learn:
– To understand input and output streams.
– To be able to use all print formatting capabilities.
– To be able to use all input formatting capabilities.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 3 9.1 Introduction • In this chapter – Presentation of results – scanf and pr i nt f – Streams (input and output)
• get s , put s , get char , put c har (in )
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 9.2 Streams • Streams
– Sequences of characters organized into lines
• Each line consists of zero or more characters and ends with  newline character
• ANSI C must support lines of at least 254 characters
– Performs all input and output – Can often be redirected
• Standard input – keyboard • Standard output – screen • Standard error – screen • More in Chapter 11
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 2 5 9.3
Formatting Output with p  r  i n  t f • pr i nt f – 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
– pr i nt f ( 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
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 9.4 Printing Integers Conversion Specifier  Description  d 
Display a signed decimal integer.  i 
Display a signed decimal integer. (Note: The i and d specifiers 
are different when used with scanf .)  o 
Display an unsigned octal integer.  u 
Display an unsigned decimal integer.  x or X 
Display an unsigned hexadecimal integer. X causes the digits 0- 9 
and the letters A- F to be displayed and x causes the digits 0- 9  and a- f to be displayed.  h or l (letter l  ) 
Place before any integer conversion specifier to indicate that a 
shor t or l ong integer is displayed respectively. Letters h and l 
are more precisely called length modifiers. 
Fig. 9.1 Integer conversion specifiers.     
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 3 7 9.4 Printing Integers • Integer
– Whole number (no decimal point): 25, 0, - 9
– Positive, negative, or zero
– Only minus sign prints by default (later we shall change this)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1   /  *   F i F g   9  . 2  :    f ig09_02  .  c    *  /  8 2   /  *   Us i ng    g  t he h   i n  t n ege  r   con o   ve  r  s i on o    spec i  f i e  r e   s    *  /  Ou O t u l t iln i e n 3 # i nc l ude   <  s  t d io  . h>  > 4  fig09_02.c  5  in  t   ma in  (  )  6   {    7  p  r p i n  t  f  (    " %d  \ n  "  ,   455    5  )  ;  8  p  r p i n  t  f  (    "   %i  \ n  "  ,   455    )  ;      /  *   i    same   as   d   i n   p  r i n  t  f    *  /  9  p  r p i n  t  f  (    " %d  \ n  "  ,   +4 + 55    )  ;  10        p  r i n  t  f  (  " %d  \ n  "  ,    - 455    5  )  ;  11        p  r i n  t  f  (  " %hd  \ d n  "  ,   32000 0    )  ;  12        p  r i n  t  f  (  "   %l d  \ d n  "  ,   20000 0 00000    )  ;  13        p  r i n  t  f  (  " %o  \ n  "  ,   455 5    )  ;  14        p  r i n  t  f  (  " %u  \ n  " ,   455    )  ;  15        p  r i n  t  f  (  " %u  \ n  "  ,    - 45 4 5    )  ;  16        p  r i n  t  f  (  " %  x  \  n  "  ,   455 5    )  ;  17        p  r i n  t  f  (  " %X  \ X n  "  ,   455 5    )  ;  18     19         r e  t e u  r n 0  ;    /  *   i nd i d   ca  t es    su  c  ce  s e   s  f u l    t e  r e   mi na  t i on  *  /  20  21   }    /  *   end    d ma i n  *  /   
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 9 455 5 Ou O t u l t iln i e n 455 5 455 5   - 45 4 5 Program Ouptut 320 2 00 2000000000 707 0 455 5 4294966841 1c7 1C7
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 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 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)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 5 11 9.5
Printing Floating-Point Numbers Conversion specifier  Description  e or E 
Display a floating-point value in exponential notation.  f 
Display floating-point values.  g or G 
Display a floating-point value in either the floating-point form f or the  exponential form e (or E).  L 
Place before any floating-point conversion specifier to indicate that a 
l ong doubl e floating-point value is displayed.  Fig. 9.3 
Floating-point conversion specifiers.     
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1   /  *   Fi F  i F g  g   9  . 4  :    f  i g09_04 0  . c    *  /  12 2   /  *   Pr P  r P  i n  t  i ng  f  l oa  t  i ng n  - po  i n  t   numbe  r  s w  i  t h      Ou O t u l t iln i e n 3         f  l oa  t  i ng  - po  i n  t    c onv e  r s  i on    s pe  c  i  f  i er e  r e s    *  /  4  fig09_04.c  5 #i #  i nc l  l ud u e   t d  i o  . h  > 6  7 i  i nt  t   mai  i n( n  ( )  )  8   {    9        p  r  i n  t  f  (  " %e  \ n  "  ,   1234 3 567. 7  . 89    )  ;  10        p  r  i n  t  f  (    " %e  \ n  "  ,   +12 1 34567  . 89    )  ;  11        p  r  i n  t  f  (    " %e  \ n  "  ,    - 1234567 6  . 89    )  ;  12        p  r  i n  t  f  (    " %E\ E  \ E n  "  ,   1234567  . 89    )  ;  13        p  r  i n  t  f  (    "   %f  \ n  "  ,   1234567  . 89    )  ;  14        p  r  i n  t  f  (    " %g  \ n  "  ,   1234567  . 89 8    )  ;  15        p  r  i n  t  f  (    " %G  \ n  "  ,   1234567  . 89    )  ;  16  17   r e  t u  r n 0  ;    /  *    i nd  i ca  t e  s    s u  c  c es s  f u  l    t e  r   mi na n  t  i on o    *  /  18  19   }    /  *   end   ma  i n    *  /  Program Output  1.234568e+006 1.234568e+006 -1.234568e+006 1.234568E+006 1234567.890000 1.23457e+006 1.23457E+006 
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 13 9.6
Printing Strings and Characters • c – Prints char argument
– Cannot be used to print the first character of a string • s
– Requires a pointer to c har as an argument
– Prints characters until NULL (' \ 0' ) encountered
– Cannot print a char argument • Remember
– Single quotes for character constants (' z' )
– Double quotes for strings " z" (which actually contains two  characters, ' z' and ' \ 0' )
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 /  / *  *   Fi F  i g   9.  . 5:  :   f  f i  i g09_05 0 c  c   *  * /  /  14 2 /  / *  *   Pr P  r i  i nt  t i  i ng   s  s t  t r  r i  i ng n s   and   c  c har  r ac a  c t  t er  r s   *  * /  /  Ou O t u l t iln i e n 3 #i #  i nc  c l  l ude d <  s t  t di  i o. o  . h>  4  fig09_05.c 5 i  i nt  t   mai  i n(  ( )  )  6 {  {  7        c har a  r   c  c har  r act  t er  r   =  =   ' ' A' A ' ;  ;   /  / *  *   i  i ni  i t  t i  i al  l i  i z  z e   c  c har  r   *  * /  /  8        c har a  r   s  s t  t r  r i  i ng n [  [ ]  ]   =  =   "  " Th T i h  i s   i  i s   a   s  s t  t r  r i  i ng n "  " ; /  / *  *   i  i ni  i t  t i  i al a  l i  i ze   c ha h r  r   ar  r r  r ay a  y   *  * /  /      9        c ons n  s t  t c  c har   *  * s t  t r  r i  i ng n Pt P  t r  r   =  =   "  " Th T i h  i s   i  i s   al  l s o  o   a   s  s t  t r  r i  i ng"  " ;  ;   /  / *  *   c har a  r   poi  i nt n  t er  r   *  * /  /  10  11        pr  r i  i nt  t f  f (  (   "  " %c\  \ n"  " ,  ,   c  c har  r ac a  c t  t er  r   )  ) ;  ;  12        pr  r i  i nt  t f  f (  (   "  " %s\  \ n"  " ,  ,   "  " Th T i h  i s   i  i s   a   s  s t  t r  r i  i ng n "  " )  ) ;  ;  13        pr  r i  i nt  t f  f (  (   "  " %s\  \ n"  " ,  ,   s  s t  t r  r i  i ng n   )  ) ;  ;  14        pr  r i  i nt  t f  f (  (   "  " %s\  \ n"  " ,  ,   s  s t  t r  r i  i ng n Pt  t r  r   )  ) ;  ;  15  16        r  r et  t ur  r n 0;  ;   /  / *  *   i  i ndi  i cat  t es   s  s uc c es e  s s  s f  f ul  l   t  t er  r mi  i na n t  t i  i on o   *  * /  /  17  18 }  }   /  / *  *   end  d   mai  i n   *  * /  /   A Th T  i s  i s   a    s  t  r  i ng n Th T  i s  i s   a    s  t  r  i ng n Th T  i s  i s   a  l  s o   a   s  t  r  i ng n
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 15 9.7
Other Conversion Specifiers • p
– Displays pointer value (address) • n
– Stores number of characters already output by current  pr i nt f statement
– Takes a pointer to an integer as an argument
– Nothing printed by a %n specification
– Every pr i nt f call returns a value
• Number of characters output
• Negative number if error occurs • % – Prints a percent sign  – %%
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 16 9.7
Other Conversion Specifiers Conversion specifier  Description  p 
Display a pointer value in an implementation-defined manner.  n 
Store the number of characters already output in the current 
pr i nt f statement. A pointer to an integer is supplied as the 
corresponding argument. Nothing is displayed.  % 
Display the percent character.  Fig. 9.6  Other conversion specifiers.     
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 8 1   /  *   Fi F  i F g  g    g 9  . 7  :    f  i g0 g 9_ 9 07  .  c    *  /  17 2   /  *   U  s  i ng n    t he  e    e p  ,   n  ,   and      % co nv n  v n e  r s  i on    s pec  i  f  i er e  r e   s    *  /  Ou O t u l t iln i e n 3 #i #  i # n  c  l ud u e   <  s <  t d  i o. o  . o h>  > 4  fig09_07.c (1 of 2) 5   i n  t   mai a  i a n  (  )  6   {    7         i n  t    * pt p  t p  r  ;              /  *   de  f  i ne n   po  i nt n  t n e  r    t o    i nt n  t n    *  /  8         i n  t    x   =  =    = 12345  ;    /  *    i ni n  i n  t  i a  l  i  z e    i n  t    x    *  /  9         i n  t y;  /  *   de  f  i ne n    i n  t    y    *  /  10         11        p  t  r   =   &x &  ;              /  *   a  s s  i gn g   add  r es s   o  f    x    t o   p  t  r    *  /  12        p  r  i n  t  f  (  " Th T e h    v a  l u e  e    e o  f   pt p  t p  r    i s   %p  \ n  " ,   p  t r    )  ;  13        p  r  i n  t  f  (  " Th T e h   add  r es e  s e   s   o  f    x    i s   %p  \ n  \ n  " , &x  )  ;  14  15        p  r  i n  t  f  (  " To T t o  t a  l    c ha  r ac  t e  r  s   p  r  i n  t ed   o n  n    n  t h  i s    l  i ne  : %n  "  ,   &y &    )  ;  16        p  r  i n  t  f  ( "   %d\  \ n\  \ n"  "  ,    y    )  ;  17  18         y   =  =    = p  r  i nt n  t n  f  (    " Th T  i  s    l  i ne n   has a  s a   28    c ha  r ac  t e  r  s  \ n  "    )  ;  19        p  r  i n  t  f  (  " %d    c ha  r ac  t e  r  s   wer e  r e e   p  r  i nt n  t n ed  \ n  \ n  "  ,   y    )  ;  20         21        p  r  i n  t  f  (  " P  r  i n  t  i ng   a   %  %  i n  n    n a    f o  r mat a  t a    c on  t  r o  l    s  t  r  i ng n  \ n  "    )  ;  22  23         r e  t u  r n 0  ;    /  *    i nd  i ca  t es    s uc c es e  s e s  f u  l    t e  r   mi na n  t  i on o    *  /  24  25   }    /  *   end   mai a  i a n    *  /   
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 18
The value of ptr is 0012FF78 Ou O t u l t iln i e n
The address of x is 0012FF78
Total characters printed on this line: 38 Program Output This line has 28 characters 28 characters were printed
Printing a % in a format control string 
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 9 19 9.8
Printing with Field Widths and  Precisions • 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
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 20 9.8
Printing with Field Widths and  Precisions • Precision
– Meaning varies depending on data type – Integers (default 1)
• Minimum number of digits to print
– If data too small, prefixed with zeros – Floating point
• Number of digits to appear after decimal (e and f )
– For g – maximum number of significant digits – Strings
• Maximum number of characters to be written from string – Format
• Use a dot (. ) then precision number after % %. 3f
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 21 9.8
Printing with Field Widths and  Precisions • Field width and precision – Can both be specified • %wi dt h. pr ec i s i on %5. 3f
– Negative field width – left justified
– Positive field width – right justified – Precision must be positive
– Can use integer expressions to determine field width and  precision values
• Place an asterisk (* ) in place of the field width or precision
– Matched to an i nt argument in argument list • Example:
pr i nt f ( " %* . * f " , 7, 2, 98. 736 ) ;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 /  / *  *   Fi F  i g  g   9.  . 8:  :   f  f i  i g0 g 9_08.  . c  c   *  * /  /  22 2 /  / *  *   Pr P  r i  i nt  t i  i ng  g   i  i nt  t eger  r s  s   r  r i  i ght  t -  - j  j us u  s t  t i  i f  f i  i ed e   *  * /  /  Ou O t u l t iln i e n 3 #i  i nc n  c l  l ude <  s t  t di  i o. o  . h>  4  fig09_08.c 5 i  i nt n  t   mai  i n( n  ( )  )  6 {  {  7        pr  r i  i nt n  t f  f (  (   "  " %4d\  \ n"  " ,  ,   1   ) ;  8        pr  r i  i nt n  t f  f (  (   "  " %4d\  \ n"  " ,  ,   12 )  ) ;  ;  9        pr  r i  i nt n  t f  f (  (   "  " %4d\  \ n"  " ,  ,   123   )  ) ;  ;  10        pr  r i  i nt n  t f  f (  (   "  " %4d\  \ n"  " ,  ,   1234   )  ) ;  ;  11        pr  r i  i nt n  t f  f (  (   "  " %4d\  \ n\  \ n"  " ,  ,   12345 4   )  ) ;  ;  12  13        pr  r i  i nt n  t f  f (  (   "  " %4d\  \ n"  " ,  ,   -  - 1 )  ) ;  ;  14        pr  r i  i nt n  t f  f (  (   "  " %4d\  \ n"  " ,  ,   -  - 12   )  ) ;  ;  15        pr  r i  i nt n  t f  f (  (   "  " %4d\  \ n"  " ,  ,   -  - 123   )  ) ;  ;  16        pr  r i  i nt n  t f  f (  (   "  " %4d\  \ n"  " ,  ,   -  - 1234   ) ;  17        pr  r i  i nt n  t f  f (  (   "  " %4d\  \ n"  " ,  ,   -  - 12345 )  ) ;  ;  18  19        r  r et  t ur  r n 0;  ;   /  / *  *   i  i nd n i  i c at a  t es  s   s uc u  c c  c es  s s f  f ul  l   t  t er  r m  mi na n t  t i  i on o   *  * /  /  20  21 }  }   /  / *  * end mai  i n *  * /  /   
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 23 1 Ou O t u l t iln i e n 12 123 123 2 4 1 Program Output 123 2 45   - 1   - 12   - 12 1 3   - 12 1 34   - 12 1 345 4 
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.
1 / * Fi g 9 . 9 : f i g 0 9 _ 0 9 . c * /  24
2 / * Us i ng p r e c i s i on wh i l e p r i n t i n g i nt e g e r s ,  Ou O t u l t iln i e n
3  f l o at i n g- po i nt n u mb e r s , a nd s t r i n g s * / 
4 #i n c l u de < s t d i o. h >  fig09_09.c 5  6 i n t ma i n ( )  7 { 
8  i n t i = 87 3; / * i ni t i a l i z e i nt i * / 
9  d o u b l e f = 12 3 . 9 4 5 3 6 ; / * i n i t i a l i z e d o u bl e f * / 
10  c h a r s [ ] = " Hap p y Bi r t hd a y " ; / * i ni t i a l i z e c h a r a r r a y s * /  11 
12  pr i n t f ( " Us i ng p r ec i s i on f o r i n t eg e r s \ n " ) ; 
13  pr i n t f ( " \ t %. 4d \ n \ t %. 9 d \ n \ n " , i , i ) ;  14 
15  pr i n t f ( " Us i ng p r ec i s i on f o r f l o at i n g- po i nt n u mb e r s \ n " ) ; 
16  pr i n t f ( " \ t %. 3f \ n \ t %. 3 e \ n \ t %. 3 g \ n \ n " , f , f , f ) ;  17  
18  pr i n t f ( " Us i ng p r ec i s i on f o r s t r i n g s \ n" ) ; 
19  pr i n t f ( " \ t %. 11 s \ n" , s ) ;  20 
21  r et u r n 0 ; / * i n d i c a t e s s uc c e s s f u l t er mi n at i o n * /  22 
23 } / * en d ma i n * /   
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 12