sscanf, swscanf
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

sscanf, swscanf

Read formatted data from a string.

int sscanf(
   const char *buffer,
   const char *format [,
   argument ] ... 
int swscanf(
   const wchar_t *buffer,
   const wchar_t *format [,
   argument ] ... 


Stored data
Format-control string. For more information, see Format Specifications.
Optional arguments

Return Value

Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.


The sscanf function reads data from buffer into the location given by each argument. Every argument must be a pointer to a variable with a type that corresponds to a type specifier in format. The format argument controls the interpretation of the input fields and has the same form and function as the format argument for the scanf function. If copying takes place between strings that overlap, the behavior is undefined.

Security Note   When reading a string with sscanf, always specify a width for the %s format (for example, "32%s" instead of "%s"); otherwise, improperly formatted input can easily cause a buffer overrun.

swscanf is a wide-character version of sscanf; the arguments to swscanf are wide-character strings. sscanf does not handle multibyte hexadecimal characters. swscanf does not handle Unicode fullwidth hexadecimal or "compatibility zone" characters. Otherwise, swscanf and sscanf behave identically.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_stscanf sscanf sscanf swscanf


Routine Required header Compatibility
sscanf <stdio.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP
swscanf <stdio.h> or <wchar.h> ANSI, Win 98, Win Me, Win NT, Win 2000, Win XP

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_sscanf.c
/* This program uses sscanf to read data items
 * from a string named tokenstring, then displays them.

#include <stdio.h>

int main( void )
   char  tokenstring[] = "15 12 14...";
   char  s[81];
   char  c;
   int   i;
   float fp;

   /* Input various data from tokenstring: */
   sscanf( tokenstring, "%80s", s ); // max 80 character string
   sscanf( tokenstring, "%c", &c );
   sscanf( tokenstring, "%d", &i );
   sscanf( tokenstring, "%f", &fp );

   /* Output the data read */
   printf( "String    = %s\n", s );
   printf( "Character = %c\n", c );
   printf( "Integer:  = %d\n", i );
   printf( "Real:     = %f\n", fp );


String    = 15
Character = 1
Integer:  = 15
Real:     = 15.000000

See Also

Stream I/O Routines | fscanf | scanf | sprintf | _snprintf | Run-Time Routines and .NET Framework Equivalents

© 2015 Microsoft