The * Modifier with printf() and scanf()

来源:互联网 发布:医疗器械 软件确认 编辑:程序博客网 时间:2024/06/07 08:03

Both   printf()   and   scanf() can   use   the   *   modifier   to   modify   the   meaning   of   a   specifier,   but   they   do   so   in   dissimilar   fashions.   First,   let 's   see   what   the   *   modifier   can   do   for   printf(). 

1. Suppose   that   you   don 't   want   to   commit   yourself   to   a   field   width   in   advance,   but   that   you   want   the   program   to   specify   it.   You   can   do   this   by   using   *   instead   of   a   number   for   the   field   width,   but   you   also   have   to   use   an   argument   to   tell   what   the   field   width   should   be.   That   is,   if   you   have   the   conversion   specifier   %*d,   the   argument   list   should   include   a   value   for   *   and   a   value   for   d.   The   technique   also   can   be   used   with   floating-point   values   to   specify   the   precision   as   well   as   the   field   width.   Listing   4.16   is   a   short   example   showing   how   this   works. 

Listing   4.16   The   varwid.c   Program 
/*   varwid.c   --   uses   variable-width   output   field   */ 
#include   <stdio.h> 
int   main(void) 

    unsigned   width,   precision; 
    int   number   =   256; 
    double   weight   =   242.5; 

    printf( "What   field   width?\n "); 
    scanf( "%d ",   &width); 
    printf( "The   number   is   :%*d:\n ",   width,   number); 
    printf( "Now   enter   a   width   and   a   precision:\n "); 
    scanf( "%d   %d ",   &width,   &precision); 
    printf( "Weight   =   %*.*f\n ",   width,   precision,   weight); 
    return   0; 


The   variable   width   provides   the   field   width,   and   number   is   the   number   to   be   printed.   Because   the   *   precedes   the   d   in   the   specifier,   width   comes   before   number   in   printf() 's   argument   list.   Similarly,   width   and   precision   provide   the   formatting   information   for   printing   weight.   Here   is   a   sample   run: 

What   field   width? 

The   number   is   :       256: 
Now   enter   a   width   and   a   precision: 
8   3 
Weight   =     242.500 

Here,   the   reply   to   the   first   question   was   6,   so   6   was   the   field   width   used.   Similarly,   the   second   reply   produced   a   width   of   8   with   3   digits   to   the   right   of   the   decimal.   More   generally,   a   program   could   decide   on   values   for   these   variables   after   looking   at   the   value   of   weight. 

   2.   The   *   serves   quite   a   different   purpose   for   scanf().   When   placed   between   the   %   and   the   specifier   letter,   it   causes   that   function   to   skip   over   corresponding   input.   Listing   4.17   provides   an   example. 

Listing   4.17   The   skip2.c   Program 
/*   skip2.c   --   skips   over   first   two   integers   of   input   */ 
#include   <stdio.h> 
int   main(void) 

      int   n; 

      printf( "Please   enter   three   integers:\n "); 
      scanf( "%*d   %*d   %d ",   &n); 
      printf( "The   last   integer   was   %d\n ",   n); 
      return   0; 


The   scanf()   instruction   in   Listing   4.17   says,   "Skip   two   integers   and   copy   the   third   into   n. "   Here   is   a   sample   run: 

Please   enter   three   integers 
1976   1992   1996 
The   last   integer   was   1996 

This   skipping   facility   is   useful   if,   for   example,   a   program   needs   to   read   a   particular   column   of   a   file   that   has   data   arranged   in   uniform   columns.