views:

383

answers:

3

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?

A: 

I believe the Windows equivalent is sprintf_s

Il-Bhima
+2  A: 

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...!)

You could still use

#if _MSC_VER
#define snprintf _snprintf
#endif

as a workaround.

KennyTM
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
Here's your citation -- http://connect.microsoft.com/VisualStudio/feedback/details/333273/request-for-c99-vla-in-visual-studio
jsumners
@jsumners: Thanks :) @Andrew: That's why they've invented `sprintf_s`. :(
KennyTM
An advantage of the C99 snprintf() over msvc _snprintf() and the snprintf() on older *nix platforms is that C99 snprintf() returns the number of characters that would have been written even if the buffer is too small. It's often useful if you don't know how much space to allocate in advance.
Alexandre Jasmin
Visual Studio is a C++ compiler, it's not a C compiler.
DeadMG
@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
Just because the compiler technically can compile C code doesn't make it an actual C compiler, as evidenced by the fact that it's not been updated for the standard that arrived last decade.
DeadMG
@DeadMG - it does, however, support the C90 standard as well as a few bits of C99, making it a C compiler.
Andrew
Only if you live between 1990 and 1999.
DeadMG
A: 

You should use

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

or better _snprintf_s function (see http://msdn.microsoft.com/de-de/library/f30dzcf6%28VS.80%29.aspx):

_snprintf_s(buffer, _countof(buffer), 100, "SomeString");

or

_snprintf_s(buffer, _countof(buffer), _countof(buffer), "SomeString");
Oleg