'snprintf' : undeclared identifier怎么解决

#include <iostream>
/*#include <stdio.h>*/
#include "stdio.h"
#include <string.h>
#include <stdlib.h>

void free_buf(char *ptr)
{
free(ptr);
ptr=NULL;
return;
}

int main(int argc, char *argv[])
{
char *p=(char*)malloc(32);
char *q=p;
snprintf(p,32,"%s", "end of world, 2012");
free_buf(p);
q=NULL;
printf("%S\n",p==q?"p=q":"p!=q");
return 0;
}
总是出现这样的错误E:\c++ workspace\test.cpp(18) : error C2065: 'snprintf' : undeclared identifier
Error executing cl.exe.

snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。

VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;
gcc中的snprintf函数的count参数表示,向buff中写入count个字符,包括'\0'字符,并且,返回实际的字符串长度

看你程序的意思,其实有没有这句没有关系,都能达到你想要的效果!
改成_snprintf或者注释掉这一行。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-06-22
1、含义
这个错误的意思是,在程序中未定义snprintf这个变量。
2、解决方案
a.首先定位第一次使用snprintf的语句
b.逐句前查,看是否真正未定义此变量,还是出现了拼写错误
c.若真正未定义,则为其增加定义
d.若拼写错误,则改正拼写出现的问题
第2个回答  2011-10-10
/*#include <stdio.h>*/
#include "stdio.h"
这两行,为什么注释掉上面那种?有什么原因吗?snprintf 就位于stdio.h中,不应该出现这个问题啊。
第3个回答  2011-10-10
'snprintf' 这个函数没有,应该是_snprintf,前面有个'_'
第4个回答  2015-11-24
snprintf函数并不是标准c/c++中规定的函数,但是在许多编译器中,厂商提供了其实现的版本。
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;
gcc中的snprintf函数的count参数表示,向buff中写入count个字符,包括'\0'字符,并且,返回实际的字符串长度
改成_snprintf或者注释掉这一行
相似回答