

scanf()                       STDIO                       scanf()




Accept and format input

#include <stdio.h>
iinntt ssccaannff(_f_o_r_m_a_t, _a_r_g_1, ... _a_r_g_N)
cchhaarr *_f_o_r_m_a_t; [ddaattaa ttyyppee] *_a_r_g_1, ... *_a_r_g_N;

scanf reads  the standard  input, and  uses the string  format to
specify a  format for each arg1 through argN,  each of which must
be a pointer.

scanf  reads one  character at  a time  from format;  white space
characters are ignored.  The percent sign character `%' marks the
beginning of a  conversion specification.  `%' may be followed by
characters that  indicate the  width of  the input field  and the
type of conversion to be done.

scanf reads  the standard input until the  return key is pressed.
Inappropriate characters  are thrown away; e.g.,  it will not try
to write an alphabetic character into an iinntt.

The following modifiers can be used within the conversion string:

11. The  asterisk `*', which  indicates that the  next input field
   should be skipped rather than assigned to the next arg.

22. A  string of decimal  digits, which specifies  a maximum field
   width.

33. An l,  which specifies that the next input  item is a long ob-
   ject rather  than an int object.   Capitalizing the conversion
   character has the same effect.

The following conversion specifiers are recognized:

cc  Assign the next input  character to the next arg, which should
   be of type cchhaarr *.

dd  Assign  the decimal integer  from the next input  field to the
   next arg, which should be of type iinntt *.

DD  Assign  the decimal integer  from the next input  field to the
   next arg, which should be of type lloonngg *.

ee  Assign the floating point  number from the next input field to
   the next  arg, which should be of type ffllooaatt *.

EE  Assign the floating point  number from the next input field to
   the next  arg, which should be of type ddoouubbllee *.

ff  Same as ee.

FF  Same as EE.




COHERENT Lexicon                                           Page 1




scanf()                       STDIO                       scanf()



oo  Assign the octal integer from the next input field to the next
   arg, which should be of type iinntt *.

OO  Assign the octal integer from the next input field to the next
   arg, which should be of type lloonngg *.

ss  Assign the  string from the next input field  to the next arg,
   which should be of type cchhaarr *.  The array to which the char *
   points should  be long enough to accept the  string and a ter-
   minating null character.

xx  Assign  the hexadecimal integer  from the next  input field to
   the next arg, which should be of type iinntt *.

XX  Assign  the hexadecimal integer  from the next  input field to
   the next arg, which should be of type lloonngg *.

It is important to remember that ssccaannff reads up, but not through,
the newline character:  the newline remains in the standard input
device's  buffer until  you dispose  of it  somehow.  Programmers
have  been known  to forget  to empty  the buffer  before calling
ssccaannff a second time, which leads to unexpected results.

***** Example *****

The following  example uses  ssccaannff in  a brief dialogue  with the
user.


#include <stdio.h>

main()
{
        int left, right;



        printf("No. of fingers on your left hand:  ");
        /* force message to appear on screen */
        fflush(stdout);
        scanf("%d", &left);



        /* eat newline char */
        while(getchar() != '\n')
                ;



        printf("No. of fingers on your right hand:  ");
        fflush(stdout);
        scanf("%d", &right);




COHERENT Lexicon                                           Page 2




scanf()                       STDIO                       scanf()





        /* again, eat newline */
        while(getchar() != '\n')
                ;



        printf("You've %d left fingers, %d right, & %d total\n",
                left, right, left+right);
}


***** See Also *****

fscanf(), sscanf(), STDIO

***** Diagnostics *****

scanf returns the number  of arguments filled.  It returns EOF if
no arguments can be filled or if an error occurs.

***** Notes *****

Because C does not perform type checking, it is essential that an
argument match its specification.  For that reason, scanf is best
used to process only data that you are certain are in the correct
data format.  The  use of upper-case format characters to specify
long arguments  is not  standard; use  the `l' modifier  for por-
tability.

scanf is  difficult to use  correctly, and its misuse  can be as-
sociated with  intermittent and dangerous bugs.   Rather than use
scanf to  obtain a  string from  the keyboard: it  is recommended
that you use gets to obtain  the string, and use strtok or sscanf
to parse it.





















COHERENT Lexicon                                           Page 3


