Microsoft KB Archive/67879

= INFO: scanf Format Specifications and Syntax =

Article ID: 67879

Article Last Modified on 12/12/2003

-

APPLIES TO

 The C Run-Time (CRT), when used with:  Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 2.0 Professional Edition</li></ul>

 Microsoft Visual C++ 4.0 Standard Edition</li></ul>

 Microsoft Visual C++ 5.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q67879

<div class="notice_section">

<div class="summary_section">

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

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

<div class="moreinformation_section">

MORE INFORMATION
The table below presents some examples of different uses for the string type. <pre class="fixed_text">  Format Specifier  Usage --

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

%[^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 &quot;*&quot; 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 &quot;a&quot;, or               a character between the lowercase &quot;f&quot; and lowercase &quot;k&quot; 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 (-).

Keywords: kbinfo kbcrt KB67879

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.