109

I'm unfortunate enough to be stuck using VS 2010 for a project, and noticed the following code still doesn't build using the non-standards compliant compiler:

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
    char buffer[512];

    snprintf(buffer, sizeof(buffer), "SomeString");

    return 0;
}

(fails compilation with the error: C3861: 'snprintf': identifier not found)

I remember this being the case way back with VS 2005 and am shocked to see it still hasn't been fixed.

Does any one know if Microsoft has any plans to move their standard C libraries into the year 2010?

3
  • 1
    ... or you can just do "#define snprintf _snprintf" Feb 25, 2015 at 1:16
  • 5
    ...you could, but unfortunately _snprintf() isn't the same as snprintf() as it doesn't guarantee null termination. Oct 16, 2015 at 8:46
  • Ok so you will need to memset it to zero before using _snprintf(). Also I agree with you. Developing under MSVC is awful. The errors are confusing as hell too.
    – Owl
    Aug 17, 2016 at 13:37

6 Answers 6

96

Short story: Microsoft has finally implemented snprintf in Visual Studio 2015. On earlier versions you can simulate it as below.


Long version:

Here is the expected behavior for snprintf:

int snprintf( char* buffer, std::size_t buf_size, const char* format, ... );

Writes at most buf_size - 1 characters to a buffer. The resulting character string will be terminated with a null character, unless buf_size is zero. If buf_size is zero, nothing is written and buffer may be a null pointer. The return value is the number of characters that would have been written assuming unlimited buf_size, not counting the terminating null character.

Releases prior to Visual Studio 2015 didn't have a conformant implementation. There are instead non-standard extensions such as _snprintf() (which doesn't write null-terminator on overflow) and _snprintf_s() (which can enforce null-termination, but returns -1 on overflow instead of the number of characters that would have been written).

Suggested fallback for VS 2005 and up:

#if defined(_MSC_VER) && _MSC_VER < 1900

#define snprintf c99_snprintf
#define vsnprintf c99_vsnprintf

__inline int c99_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap)
{
    int count = -1;

    if (size != 0)
        count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
    if (count == -1)
        count = _vscprintf(format, ap);

    return count;
}

__inline int c99_snprintf(char *outBuf, size_t size, const char *format, ...)
{
    int count;
    va_list ap;

    va_start(ap, format);
    count = c99_vsnprintf(outBuf, size, format, ap);
    va_end(ap);

    return count;
}

#endif
11
  • This will not always terminate the string with a 0 which is required on a overflow. The second if in c99_vsnprintf must be: if (count == -1) { if (size > 0) str[size-1] = 0; count = _vscprintf(format, ap); }
    – Lothar
    Feb 11, 2014 at 2:09
  • 1
    @Lothar: The buffer is always null-terminated. According to MSDN: "if string truncation is enabled by passing _TRUNCATE, these functions will copy only as much of the string as will fit, leaving the destination buffer null-terminated, and return successfully". Feb 12, 2014 at 9:18
  • 3
    As of Jun 2014, there is still no "full" C99 support in Visual Studio, even with Update 2. This blog gives the C99 support brief for MSVC 2013. As snprintf() family functions are now a part of C++11 standard, MSVC lags behind clang and gcc in C++11 implementation!
    – fnisi
    Jun 3, 2014 at 23:24
  • 2
    With VS2014, C99 standards with snprintf and vsnprintf are added. See blogs.msdn.com/b/vcblog/archive/2014/06/18/…. Oct 29, 2014 at 23:12
  • 1
    Mikael Lepistö: Really? For me _snprintf only works if I enable _CRT_SECURE_NO_WARNINGS. This work-around works fine without that step.
    – FvD
    Sep 10, 2015 at 13:46
34

snprintf is not part of C89. It's standard only in C99. Microsoft has no plan supporting C99.

(But it's also standard in C++0x...!)

See other answers below for a workaround.

10
  • 5
    It's not a good workaround, however...as there are differences in the snprintf and _snprintf behavior. _snprintf handles the null terminator retardedly when dealing with insufficient buffer space.
    – Andrew
    May 26, 2010 at 18:37
  • 7
    @DeadMG - wrong. cl.exe supports the /Tc option, which instructs the compiler to compile a file as C code. Furthermore, MSVC ships with a version of standard C libraries.
    – Andrew
    May 26, 2010 at 20:04
  • 3
    @DeadMG - it does, however, support the C90 standard as well as a few bits of C99, making it a C compiler.
    – Andrew
    May 27, 2010 at 14:26
  • 15
    Only if you live between 1990 and 1999.
    – Puppy
    May 27, 2010 at 17:41
  • 6
    -1, Microsoft's _snprintf is an unsafe function which behaves differently from snprintf (it doesn't necessarily add a null terminator), so the advice given in this answer is misleading and dangerous.
    – interjay
    Jun 4, 2013 at 9:31
8

If you don't need the return value, you could also just define snprintf as _snprintf_s

#define snprintf(buf,len, format,...) _snprintf_s(buf, len,len, format, __VA_ARGS__)
3

I believe the Windows equivalent is sprintf_s

3
  • 7
    sprintf_s behaves differently from snprintf.
    – interjay
    Jun 4, 2013 at 9:32
  • Specifically sprintf_s docs say, "If the buffer is too small for the text being printed then the buffer is set to an empty string". In contrast snprintf writes a truncated string to the output. Oct 27, 2014 at 20:04
  • 2
    @AndrewBainbridge -- you truncated the documentation. The full sentence is "If the buffer is too small for the text being printed then the buffer is set to an empty string and the invalid parameter handler is invoked." The default behavior for the invalid parameter handle is to terminate your program. If you want truncation with the _s family then you need to use snprintf_s and the _TRUNCATE flag. Yes, it is unfortunate that the _s functions do not give a convenient way of truncating. On the other hand, the _s functions do use template magic to infer buffer sizes, and that is excellent. Nov 22, 2014 at 0:23
2

Another safe replacement of snprintf() and vsnprintf() is provided by ffmpeg. You can checkout the source here (suggested).

1

I tried @Valentin Milea's code but I've got access violation errors. The only thing that worked for me was Insane Coding's implementation: http://asprintf.insanecoding.org/

Specifically, I was working with VC++2008 legacy code. From Insane Coding's implementation (can be downloaded from the link above), I used three files: asprintf.c, asprintf.h and vasprintf-msvc.c. Other files were for other versions of MSVC.

[EDIT] For completeness, their contents are as follows:

asprintf.h:

#ifndef INSANE_ASPRINTF_H
#define INSANE_ASPRINTF_H

#ifndef __cplusplus
#include <stdarg.h>
#else
#include <cstdarg>
extern "C"
{
#endif

#define insane_free(ptr) { free(ptr); ptr = 0; }

int vasprintf(char **strp, const char *fmt, va_list ap);
int asprintf(char **strp, const char *fmt, ...);

#ifdef __cplusplus
}
#endif

#endif

asprintf.c:

#include "asprintf.h"

int asprintf(char **strp, const char *fmt, ...)
{
  int r;
  va_list ap;
  va_start(ap, fmt);
  r = vasprintf(strp, fmt, ap);
  va_end(ap);
  return(r);
}

vasprintf-msvc.c:

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include "asprintf.h"

int vasprintf(char **strp, const char *fmt, va_list ap)
{
  int r = -1, size = _vscprintf(fmt, ap);

  if ((size >= 0) && (size < INT_MAX))
  {
    *strp = (char *)malloc(size+1); //+1 for null
    if (*strp)
    {
      r = vsnprintf(*strp, size+1, fmt, ap);  //+1 for null
      if ((r < 0) || (r > size))
      {
        insane_free(*strp);
        r = -1;
      }
    }
  }
  else { *strp = 0; }

  return(r);
}

Usage (part of test.c provided by Insane Coding):

#include <stdio.h>
#include <stdlib.h>
#include "asprintf.h"

int main()
{
  char *s;
  if (asprintf(&s, "Hello, %d in hex padded to 8 digits is: %08x\n", 15, 15) != -1)
  {
    puts(s);
    insane_free(s);
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.