KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q47026: Second sscanf Fails After sscanf Using %i Format

Article: Q47026
Product(s): See article
Version(s): 5.10   | 5.10
Operating System(s): MS-DOS | OS/2
Keyword(s): ENDUSER | | mspl13_c
Last Modified: 26-JUL-1989

The Microsoft C 5.10 run-time function sscanf fails to scan a string
if the following conditions are met:

1. An initial sscanf using the %i format specifier is carried out on a
   string with leading white space characters.

2. A second sscanf of a string with leading white space characters
   will fail.

To work around this problem, do one of the following:

1. Change the format string "%i" to "%d".

2. Change the format string for the second and all following sscanf's so
   that it causes all leading spaces to be removed. For example, use a
   format of " %d" instead of "%d".

The following program demonstrates this problem when compiled
with Microsoft C 5.10 using default compiler options:

#include <stdio.h>

void main(void)
{
  char  *str1= "  23  ";
  char  *str2= "  99  ";
  int   val0;
  int   val1;
  int   val2;

  sscanf(str2,"%d", &val1);     /* this line works correctly */
  sscanf(str1,"%i", &val0);     /* this line works correctly */
  sscanf(str2,"%d", &val2);     /* this line will fail       */

  if(val1 != val2)
        printf("sscanf test failed \n");
  else
        printf("sscanf passed test\n");
}

Microsoft is researching this problem and will post new information as
it becomes available.

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.