我正在使用我的前任使用的 C 代码库:
#ifdef _MSC_VER
// Map to equivalent function
#define snprintf sprintf_s
#endif
代码库需要在 Linux (gcc/clang)、OSX (gcc/clang) 和 Windows (VS) 上编译。我第一次尝试在 Visual Studio 2015 中编译代码。以前使用的是 Visual Studio 2010。我遇到了描述的错误 here并且能够使用已接受答案中的线索进行编译:
#ifdef _MSC_VER
#if _MSC_VER<1900
#define snprintf sprintf_s
#endif
#endif
该项目现在使用 Visual Studio 2015 和 clang 在 OSX 上进行编译。但是,我担心 sprintf_s 中的声明文档:
与 snprintf 不同,sprintf_s 保证缓冲区将以 null 终止(除非缓冲区大小为零)。
如果 Visual Studio 包含 snprintf
的 C99 兼容版本,是否应该保证缓冲区以 null 终止?
我写了一个小程序来评估行为。
#include <stdio.h>
#include <string.h>
int main(int argc, char** argv) {
char buf[5];
snprintf(buf, sizeof(buf), "abcdef");
printf("buffer: %s\n", buf);
printf("size of buffer: %lu\n", strlen(buf));
printf("last character a null terminator?: %s\n",
(strcmp(&buf[4], "\0") == 0) ? "yes" : "no");
return 0;
}
我使用 Visual Studio 2015 在 OSX 和 Windows 上构建并运行了该程序。
在 OSX 上,输出是:
~$c99 main.c
~$./a.out
buffer: abcd
size of buffer: 4
last character a null terminator?: yes
在带有 Visual Studio 2015 的 Windows 7 上,输出是
> printf_evaluation.exe
buffer: abcd
size of buffer: 4
last character a null terminator?: yes
这不是表明输出实际上是空终止的,并暗示 MSDN 文档不正确吗?我的例子太琐碎了吗?在 Visual Studio 2015 中,snprintf
的输出可能不会以 NULL 终止吗?
最佳答案
snprintf()
只要它的第二个参数大于零,它就总是 nul 终止缓冲区。所以是的,MSDN 文档是错误的。
From C11 standard, snprintf():
The snprintf function is equivalent to fprintf, except that the output is written into an array (specified by argument s) rather than to a stream. If n is zero, nothing is written, and s may be a null pointer. Otherwise, output characters beyond the n-1st are discarded rather than being written to the array, and a null character is written at the end of the characters actually written into the array. If copying takes place between objects that overlap, the behavior is undefined.
(强调我的)。
关于c - snprintf 的输出是否保证在 Visual Studio 2015 中以 null 终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34912145/