home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / ansi / stdio / scanf.txh < prev    next >
Encoding:
Text File  |  1996-05-20  |  3.3 KB  |  136 lines

  1. @node scanf, stdio
  2. @subheading Syntax
  3.  
  4. @example
  5. #include <stdio.h>
  6.  
  7. int scanf(const char *format, @dots{});
  8. @end example
  9.  
  10. @subheading Description
  11.  
  12. This function scans formatted text from @code{stdin} and stores it in
  13. the variables pointed to by the arguments.  @xref{scanf}.
  14.  
  15. The format string contains regular characters which much match the input
  16. exactly as well as a conversion specifiers, which begin with a percent
  17. symbol.  Any whitespace in the format string matches zero or more of any
  18. whitespace characters in the input.  Thus, a single space may match a
  19. newline and two tabs in the input.  All conversions except @code{c} and
  20. @code{[} also skip leading whitespace automatically.  Each conversion
  21. specifier contains the following fields:
  22.  
  23. @itemize @bullet
  24.  
  25. @item
  26.  
  27. An asterisk (@code{*}) which indicates that the input should be
  28. converted according to the conversion spec, but not stored anywhere. 
  29.  
  30. @item
  31.  
  32. A width specifier, which specifies the maximum number of input
  33. characters to use in the conversion. 
  34.  
  35. @item
  36.  
  37. An optional conversion qualifier, which may be @code{h} to specify
  38. @code{short}, @code{l} to specify long ints, or @code{L} to specify
  39. long doubles. Long long type can be specified by @code{L} or @code{ll}.
  40.  
  41. @item
  42.  
  43. The conversion type specifier:
  44.  
  45. @table @code
  46.  
  47. @item c
  48.  
  49. Copy the next character (or @var{width} characters) to the given buffer.
  50.  
  51. @item d
  52.  
  53. Convert the input to a signed integer.
  54.  
  55. @item e
  56. @item E
  57. @itemx f
  58. @itemx g
  59. @itemx G
  60.  
  61. Convert the input to a floating point number.
  62.  
  63. @item i
  64.  
  65. Convert the input, determining base automatically by the presence of
  66. @code{0x} or @code{0} prefixes.  @xref{strtol}.
  67.  
  68. @item n
  69.  
  70. Store the number of characters scanned so far into the integer pointed
  71. to. 
  72.  
  73. @item o
  74.  
  75. Convert the input to a signed integer, using base 8.
  76.  
  77. @item p
  78.  
  79. Convert the input to a pointer.  This is like using the @code{x} format. 
  80.  
  81. @item s
  82.  
  83. Copy the input to the given string, skipping leading whitespace and
  84. copying non-whitespace characters up to the next whitespace.  The string
  85. stored is then @code{NULL}-terminated. 
  86.  
  87. @item u
  88.  
  89. Convert the input to an unsigned integer.
  90.  
  91. @item x
  92. @itemx X
  93.  
  94. Convert the input to an unsigned integer, using base 16.
  95.  
  96. @item [@dots{}]
  97.  
  98. Like the @code{c} format, except only certain characters are copied. 
  99. The characters between the brackets determine which characters are
  100. allowed, and thus when the copying stops.  These characters may be
  101. regular characters (example: @code{[abcd]}) or a range of characters
  102. (example: @code{[a-d]}).  If the first character is a caret (@code{^}),
  103. then the set specifies the set of characters that do not get copied
  104. (i.e.  the set is negated).  To specify that the set contains a
  105. close-bracket (@code{]}), list that as the first regular character. 
  106.  
  107. @item %
  108.  
  109. This must match a percent character in the input. 
  110.  
  111. @end table
  112.  
  113. @end itemize
  114.  
  115. Most conversions make use of @code{strtol} or @code{strtoul} to perform
  116. the actual conversions. 
  117.  
  118. @subheading Return Value
  119.  
  120. The number of items successfully matched and assigned. If input ends
  121. before first item is assigned, EOF is returned.
  122.  
  123. @subheading Example
  124.  
  125. @example
  126. int x, y;
  127. char buf[100];
  128. scanf("%d %d %s", &x, &y, buf);
  129.  
  130. /* read to end-of-line */
  131. scanf("%d %[^\n]\n", &x, buf);
  132. /* read letters only */
  133. scanf("[a-zA-Z]", buf);
  134. @end example
  135.  
  136.