Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>scanf() read formatted data from stdin</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 scanf()                 Read Formatted Data from Stdin

 #include   <stdio.h>

 int        scanf(format-string[,argument...]);
 const char *format-string;              Format control

    scanf() reads one character at a time from 'stdin' (the standard
    input stream) and stores the data in the locations given by
    'argument'. 'format-string' is a character string that contains
    whitespace and non-whitespace characters and format specifications;
    it governs the way input fields are interpreted.  Each argument must
    point to a variable of a type corresponding to a type specifier in
    'format-string'.  Here is a description of the parameters to scanf.

 format-string: The format string is read from left to right.  The first
                format specification determines how the value of the
                first input field is converted before being stored in the
                location specified by the first argument.  The second
                format specification determines how the value of the
                second input field is converted before being stored in
                the second location, and so on.  Characters outside the
                format string--whitespace characters and non-whitespace
                characters, described below--should match the sequence of
                characters being read from the input stream.

  Whitespace characters:
                Are the blank (' '), tab ('\t'), and newline ('\n')
                characters.  The scanf() functions read, without storing,
                all whitespace characters up to the next input character
                that is not a whitespace character. Any number and
                combination of whitespace characters in the input stream
                are matched by one whitespace character in the
                format-string.

  Non-whitespace characters:
                Are all other ASCII characters other than the percent
                sign (%).  The scanf() functions read a matching
                non-whitespace character, but don't store it. If the next
                character scanned does not match, the function
                terminates.

  Format specifications:
                Are introduced by the percent sign (%). Format
                specifications cause the scanf() functions to read and
                convert characters from the input field into specific
                types of values.  These values are assigned to arguments
                in the argument list.  A format specification has the
                following form:

                        %[*][width][{F|N}][{h|l|L}]type

       Type:    The type character, which appears after the last optional
                format field, determines whether the input field is
                interpreted as a character, a string, or a number.  The
                simplest format specification contains only the percent
                sign and a type character (%s, for example).  The various
                type specifications are:

       Character  Input Expected                     Type of Argument
       -----------------------------
       d          decimal integer                    Pointer to int
       D        * decimal integer                    Pointer to long
       o          octal integer                      Pointer to int
       O        * octal integer                      Pointer to long
       x          hexadecimal integer                Pointer to int
       X        * Hexadecimal integer                Pointer to long
       i          decimal, hex, or octal integer     Pointer to int
       I        * decimal, hex, or octal integer     Pointer to long
       u          unsigned decimal integer           Pointer to unsigned int
       U        * unsigned decimal integer           Pointer to unsigned long
       f          floating-point value               Pointer to float
       c          character                          Pointer to char
       s          string                             Pointer to char array
       n          no input read                      Pointer to int
       p          value in the form of xxxx:yyyy,    Pointer to far data item
                  where x and y are uppercase
                           hexadecimal digits

                * Note that ANSI does not support these pointers to long
                arguments.

                e   Floating-point.  Signed value of the form
                           [-]d.dddde[sign]ddd where:
                         d     is a single decimal digit
                         dddd  is one or more decimal digits
                         ddd   is exactly three decimal digits,
                         sign  is + or -

                E  Floating-point.  Identical to the "e" format above except
                that the lower case "e" (preceding the optional sign) is
                replaced with a capital "E".

                g  Floating-point.  Signed value printed in "f" or "e"
                format, whichever is more compact for the given value and
                precision. (See precision chart)

                G  Floating-point.  Identical to "g" format, except that "E"
                introduces the exponent (where appropriate) instead of "e".




    A set of characters in brackets ([]) can be substituted for the 's'
    string type to read strings not delimited by space characters.  The
    specification '%nc' (where 'n'is a decimal integer) can be used to
    store strings without storing a terminating null character ('\0').

   Asterisk:    The asterisk (*) character following the percent sign
                suppresses assignment of the next input field. The field
                is scanned but not stored, and the suppressed input data
                is assumed to be of the type specified by the character
                type that follows the *.

      Width:    A positive decimal integer, width controls the maximum
                number of characters to be read from the current input
                field.  No more than 'width' characters are converted and
                stored at the corresponding argument. Fewer than 'width'
                characters will be converted and stored if a delimiting
                or illegal character is read before 'width' is reached.

    F and N:    Are prefixes that override the default or declared size
                of 'argument'. Use 'F' with an argument pointing to a
                'far' object; use 'N' with an argument pointing to a
                'near' object.

 h,l and L: Indicate which version of the following 'type' is to be used.

                The prefix 'l' indicates the 'long' version, and the
                corresponding argument should point to a 'long' or
                'double' object.  The 'l' modifier can be used with the
                d, i, o, u, x, e, g, and f type characters.

                The prefix 'h' indicates the short version, and the
                corresponding argument should point to a 'short' object.
                The 'h' modifier can be used with d, i, o, u, and x type
                characters.

                'l' and 'h' modifiers are ignored if used with any other
                type.

                'L' is used as a prefix with 'e,E,f,g,G' type to specify
                a long double.


    Returns:    The number of fields successfully converted and assigned.
                A return value of EOF means an attempt was made to read
                at end-of-file. A return value of 0 means no fields were
                assigned.

      Notes:    scanf() may stop reading a particular input field before
                it reaches a space character because:

                o   the specified width has been reached;

                o   the next character cannot be converted as specified;

                o   the next character conflicts with a character in the
                    control string;

                o   the next character fails to appear, or does appear,
                    in a given character set;

                When any of these situations occur, the next input field
                is considered to begin at the first unread character.

 Compatibility: The "F" and "N" prefixes are Microsoft extensions and
                should not be used if ANSI compatibility is required.

   -------------------------------- Example ---------------------------------

    The following statements write a prompt to the screen, then read the
    data entered from the keyboard.

           #include <stdio.h>

           int num;
           char name[15];

           main()
           {
               do
                 {
                  printf("Enter employee number and last name (0 to exit):");
                     scanf("%4d%15s",&num,name);
                     printf("%s (employee number: %d) entered.\n",name,num);
                  } while (num != 0);
           }


See Also: fscanf() printf()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson