snprintf跟_snprintf的区别
来源:互联网 发布:网络广告公司排名 编辑:程序博客网 时间:2024/04/30 09:01
在gcc中,该函数名称就snprintf,而在VC中称为_snprintf。
由于不是标准函数,没有一个统一的标准来规定该函数的行为,所以导致了各厂商间的实现版本可
能会有差异。今天也的的确确看到了差异,因为这个小小的差异是我的程序无法正常的处理数据。
这个小小的差异发生在count参数。在VC中,这个count就是要写入的总字符串字符数,例如:
//VC
int main(int argc, char * argv[])
...{
char buff[100];
printf("%d ",_snprintf(buff,10,"1234567890ab"));
printf("%s",buff);
return 0;
}
//Linxu:gcc/g++
#include <stdio.h>
int main(int argc, char * argv[])
...{
char buff[100];
printf("%d ",snprintf(buff,10,"1234567890ab"));
printf("%s",buff);
return 0;
}vc程序的输出是:
-1
1234567890@
gcc程序的输出是:
12
123456789
从输出结果可以知道:VC中的_snprintf的count参数表示,会向buff中写入count个字符,不包括'\0'字符,
并且不会在字符串末尾添加'\0'符,并且,如果字符串超过count,函数返回-1以标志可能导致的错误;gcc
中的snprintf函数的count参数表示,向buff中写入10个字符,包括'\0'字符,并且,返回实际的字符串长度,
例子中为12。 如果不了解这些函数在各平台间的差异,也许我们的程序在移植过程中会变得很脆弱。我们应该小心各种各样
的陷阱。
下面是MSDN对_snprintf函数的解释:
int _snprintf( char *buffer, size_t count, const char *format [, argument] ... ); Parameters- buffer
- Storage location for output
- count
- Maximum number of characters to store
- format
- Format-control string
- argument
- Optional arguments
All versions of the C run-time libraries.
Return Value_snprintf returns the number of bytes stored in buffer, not counting the terminating null character. If the number of bytes required to store the data exceeds count, then count bytes of data are stored in buffer and a negative value is returned.
RemarksThe _snprintf function formats and stores count or fewer characters and values (including a terminating null character that is always appended unless count is zero or the formatted string length is greater than or equal to count characters) in buffer. Each argument (if any) is converted and output according to the corresponding format specification in format. The format consists of ordinary characters and has the same form and function as the format argument for printf. If copying occurs between strings that overlap, the behavior is undefined.
- snprintf跟_snprintf的区别
- snprintf跟_snprintf的区别
- snprintf与_snprintf的区别
- strcpy/strncpy/sprintf/_snprintf/snprintf的区别
- _snprintf()与sprintf()的区别
- VC中 snprintf()应该为: _snprintf()
- snprintf vs _snprintf 和平台差异
- memcpy、strcpy、snprintf的区别
- snprintf和printf的区别
- memcpy、strcpy、snprintf的区别
- memcpy、strcpy、snprintf的区别
- memcpy、strcpy、snprintf的区别
- memcpy、strcpy、snprintf的区别
- strcpy,snprintf,memcpy的区别
- sprintf与snprintf的区别
- snprintf与sprintf的区别
- sprintf和snprintf的区别
- strcpy和strncpy sprintf和_snprintf的区别
- ecshop问题
- mysql拒绝访问(Error 1044/1…
- ECShop,phpcms v9,Discz服务器配置…
- 国人权力崇拜是滋生“北大淫…
- BBC 六分钟英语 (zz)
- snprintf跟_snprintf的区别
- 如何批量生成sql语句
- 微博营销常用操作方法总结
- mysql如何添加用户及1364错…
- Discuz X2.5 修改在线人数方…
- 如何将帝国php.ini中的short…
- Microsoft.XMLHttp组件的属性方法
- EmpireCMS系统学习-留言板
- 帝国cms二次开发应用