14

i am running a program on win 7 via visual studio 2008

and i am getting this error:

Error 4 error C3861: 'snprintf': identifier not found

i have included stdio header...

i don't know what else it can be missing

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
user690936
  • 985
  • 5
  • 13
  • 23

1 Answers1

22

Looks like on Windows, the function is prefixed with a _. Also, the function is deprecated in favour of a safer one:

http://msdn.microsoft.com/en-us/library/2ts7cx93(v=vs.80).aspx

Corbin
  • 33,060
  • 6
  • 68
  • 78
  • 6
    `snprintf_s` is rather stupid, `snprintf` already takes the size of the buffer, what benefit does asking for it twice have? If you don't want to fill the entire buffer, just give a smaller size! – dreamlax Jan 29 '12 at 09:50
  • 29
    `snprintf` is a standard C function (new in C99). Microsoft's "deprecation" of it in favor of non-standard functions is highly questionable. – Keith Thompson Jan 29 '12 at 09:51
  • 1
    It's standard in a standard that MSVC++ does not conform to. As for the deprecation's merit, I can't really speak for that. The last time I did any kind of C coding in Visual Studio was quite a long time ago. It does seem rather odd that their version of it has a size and a bufferSize. Edit: A post that outlines MS' attitude towards C99 quite well: http://stackoverflow.com/a/146419/567864 – Corbin Jan 29 '12 at 10:05
  • 5
    I wouldn't say that the function is prefixed with a `_` on Windows. It's not the same function; `_snprintf` is not compliant with the C99 requirements for `snprintf`. In particular, `_snprintf` does not guarantee NUL-termination and does not return the necessary buffer size if a larger buffer is required. – jamesdlin Jan 29 '12 at 10:34
  • @dreamlax: It's silly, but Microsoft provides `_s` versions of pretty much all of the string functions. You don't actually need to pass the buffer size twice; you can pass `_TRUNCATE` for one of them. Arguably there is some benefit in that you can be explicit whether you want to fail outright or whether truncation is acceptable. In some cases, truncation will prevent a buffer overflow but could lead to other (potentially security-related) problems later. (It is admittedly redundant since callers could check the return value of the C99 `snprintf`, however.) – jamesdlin Jan 29 '12 at 10:38
  • @jamesdlin Did not check it against standard and did not realise that it's not the same. Next time I shall check much more thoroughly... lol. – Corbin Jan 29 '12 at 19:10
  • 1
    @KeithThompson: Microsoft deprecated `_snprintf`, not `snprintf` (which they do not provide). I don't have any issue with deprecating a function that isn't compliant with the standard version and (worse) can be easily mistaken for the standard version (as evidenced here). – jamesdlin Jan 30 '12 at 12:25
  • 3
    `snprintf` and `vsnprintf` are added with C99 standard support in VS2014. See http://blogs.msdn.com/b/vcblog/archive/2014/06/18/crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1.aspx. – vulcan raven Oct 29 '14 at 23:10