INFO: scanf() Format Specifications and Syntax (67879)



The information in this article applies to:

  • The C Run-Time (CRT), when used with:
    • Microsoft Visual C++ for Windows, 16-bit edition 1.0
    • Microsoft Visual C++ for Windows, 16-bit edition 1.5
    • Microsoft Visual C++, 32-bit Editions 1.0
    • Microsoft Visual C++, 32-bit Editions 2.0
    • Microsoft Visual C++, 32-bit Editions 4.0
    • Microsoft Visual C++, 32-bit Editions 5.0

This article was previously published under Q67879

SUMMARY

In Microsoft C, the scanf() format specifier for a string is generally specified with the "%s" specifier. However, "%s" is a shorthand notation for the specifier that the run-time library actually uses.

Because "%s" causes the library to read the input string until it finds some white space, the equivalent format specifier is "%[^\0x20\t\n]", which instructs the library to read the string until it encounters a space character (\0x20), a tab character (\t), or a newline character (\n).

MORE INFORMATION

The table below presents some examples of different uses for the string type.
   Format
   Specifier   Usage
   ------------------------------------------------------------------

   %[a-z]      Read the input string until the library encounters a
               character other than a lowercase letter ("abc...z").

   %[^a-z]     Read the input string until the library encounters a
               lowercase letter.

   %[]]        According to the ANSI specification, read a series of
               right bracket (]) characters from the input string.

   %[^]]       Read the input string until the library encounters a
               right bracket character.

   %*[^%]      Scan the input string (without storing it in a
               variable) until the library encounters a percent sign
               (%) character. (The asterisk "*" instructs the compiler
               to scan the string without storing it in a variable.)

   %[-af-k]    Read the input string until the library encounters a
               character other than a hyphen (-), a lowercase "a", or
               a character between the lowercase "f" and lowercase "k"
               characters (fghijk).

   %[]         Illegal -- unpredictable results. The compiler does not
               detect this error because it is in a parameter string.

   %[^]        Illegal -- unpredictable results. The compiler does not
               detect this error because it is in a parameter string.

   %40c        Read 40 characters from the input string. [The run-time
               library does not automatically append a null terminator
               to the string, nor does reading 40 characters
               automatically terminate the scanf() function. Because the
               library uses buffered input, you must press the ENTER key
               to terminate the string scan. If you press the ENTER before
               the scanf() reads 40 characters, it is displayed normally,
               and the library continues to prompt for additional input
               until it reads 40 characters.]'

   %[^.-]      Read the input string until the library encounters a
               period (.) or a hyphen (-).

Modification Type:MajorLast Reviewed:12/12/2003
Keywords:kbCRT kbinfo KB67879 kbAudDeveloper