Partager via


_snprintf, _snwprintf (Windows CE 5.0)

Send Feedback

Developing an Application > Microsoft C Run-time Library for Windows CE > Run-time Library Reference

Write formatted data to a string.

int _snprintf(
    char *buffer,
    size_tcount, 
    const char *format [,
      argument] ... );int _snwprintf(    wchar_t *buffer,
    size_tcount, 
    const wchar_t *format [,
      argument] ... );

Parameters

  • buffer
    Storage location for output.
  • count
    Maximum number of characters to store.
  • format
    Format-control string.
  • argument
    Optional arguments.

Return Values

_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.

_snwprintf returns the number of wide characters stored in buffer, not counting the terminating null wide character. If the storage required to store the data exceeds count wide characters, then count wide characters are stored in buffer and a negative value is returned.

Remarks

These functions are supported by all versions of the C run-time libraries.

The _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) in buffer.

Each argument (if any) is converted and output according to the corresponding format specification in format. The format consists of ordinary characters and has the same form and function as the format argument for printf. If copying occurs between strings that overlap, the behavior is undefined.

_snwprintf is a wide-character version of _snprintf; the pointer arguments to _snwprintf are wide-character strings. Detection of encoding errors in _snwprintf may differ from that in _snprintf. _snwprintf, like swprintf, writes output to a string rather than to a destination of type FILE.

The following table shows generic-text routine mappings for this function.

TCHAR.H Routine
_sntprintf

For more information about TCHAR.H routines, see Generic Text Mappings.

Example

Description

The following example writes formatted output to a string.

Code

#include <stdlib.h>

#if !defined(__cplusplus)
typedef int bool;
const bool true = 1;
const bool false = 0;
#endif

int main(void)
{
  char buffer[200];
  const static char s[] = "computer"
#if 0 
"computercomputercomputercomputercomputercomputercomputercomputer"
"computercomputercomputercomputercomputercomputercomputercomputer"
"computercomputercomputercomputercomputercomputercomputercomputer"
"computercomputercomputercomputercomputercomputercomputercomputer"
#endif
  ;
  const char c = 'l'; 
  const int i = 35;
#if 1
  const double fp = 1.7320534;
#else
  const double fp = 1e300; // also doesn't fit in the buffer
#endif
  /* !subtract one to prevent "squeezing out" the terminal nul! */
  const int bufferSize = sizeof(buffer)/sizeof(buffer[0]) - 1;
  int bufferUsed = 0;
  int bufferLeft = bufferSize - bufferUsed;
  bool bSuccess = true;
  buffer[0] = 0;

  /* Format and print various data: */

  if (bufferLeft > 0)
  {
    int perElementBufferUsed = _snprintf(&buffer[bufferUsed], bufferLeft, "\tString: %s\n", s );
    if (bSuccess = (perElementBufferUsed >= 0))
    {
      bufferUsed += perElementBufferUsed;
      bufferLeft -= perElementBufferUsed;
      if (bufferLeft > 0)
      {
        int perElementBufferUsed = _snprintf(&buffer[bufferUsed], bufferLeft, "\tCharacter: %c\n", c );
        if (bSuccess = (perElementBufferUsed >= 0))
        {
          bufferUsed += perElementBufferUsed;
          bufferLeft -= perElementBufferUsed;
          if (bufferLeft > 0)
          {
            int perElementBufferUsed = _snprintf(&buffer[bufferUsed], bufferLeft, "\tInteger: %d\n", i );
            if (bSuccess = (perElementBufferUsed >= 0))
            {
              bufferUsed += perElementBufferUsed;
              bufferLeft -= perElementBufferUsed;
              if (bufferLeft > 0)
              {
                int perElementBufferUsed = _snprintf(&buffer[bufferUsed], bufferLeft, "\tReal: %f\n", fp );
                if (bSuccess = (perElementBufferUsed >= 0))
                {
                  bufferUsed += perElementBufferUsed;
                }
              }
            }
          }
        }
      }
    }
  }

  if (!bSuccess)
  {
    printf("%s\n", "failure");
  }
  else
  {
    /* !store nul because _snprintf doesn't necessarily (if the string fits without the
     * the terminal nul, but not with it)!
     * bufferUsed might be as large as bufferSize, which normally is like going
     * one element beyond a buffer, but in this case subtracted one from bufferSize,
     * so we're ok.
     */
    buffer[bufferUsed] = 0;
    printf( "Output:\n%s\ncharacter count = %d\n", buffer, bufferUsed );
  }
  return EXIT_SUCCESS;
}
// Output
Output:
  String: computer
  Character: l
  Integer: 35
  Real: 1.732053

character count = 61

Requirements

OS Versions: Windows CE 2.0 and later.

Header: (_snprintf) stdio.h, stdlib.h, (_snwprintf) wchar.h, stdio.h, stdlib.h,

Link Library: coredll.dll.

See Also

sprintf | fprintf | printf | scanf | sscanf

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.