请教snprintf问题?

__Silent 2012-07-24 11:18:21
#include <stdio.h>
#include <string.h>

int main()
{
char cmd[256] = "hello %d\n";

snprintf(cmd, sizeof(cmd), cmd, 10);

printf(cmd);

return 0;
}

这段代码
在Linux 下执行cmd一直是空。
在Windows下执行cmd是hello 10.
请问这是为什么呢,有什么问题吗?
如果写法有问题,在Linux下我要怎么做呢?
...全文
144 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
__Silent 2012-07-25
  • 打赏
  • 举报
回复
Redhat gcc 4.1.2
不过配置了交叉编译环境
Linux-Torvalds 2012-07-25
  • 打赏
  • 举报
回复
format当中的格式控制必须是string literal,不能是runtime string
sundayX 2012-07-25
  • 打赏
  • 举报
回复
unix下gcc编译没问题。
用什么编译器编译的?
int snprintf(char *str, size_t size, const char *format, ...);
以format的格式将字符串的size个字节复制到str中。格式化后字符串长度<size时,全部复制;>size时,只复制size-1个,并在最后加'\0'。

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧