sprintf(将int转换为char [])

时间:2013-02-07 14:26:45

标签: string int type-conversion printf

在我的程序中,我尝试将int转换为char [20];

我尝试以下列方式执行此操作:

  char str[20];    
  sprintf(str, "%d", timer);

其中timer是int。

但是当我构建此代码时,我收到以下警告。

Type implicit declaration of function 'sprintf' [-Wimplicit-function-declaration]   
incompatible implicit declaration of built-in function 'sprintf' [enabled by default]   
这是什么意思?

注意:(我已经包含了string.h和stdlib.h)。


很好,我在我的代码中添加了stdio.h,现在警告消失了只是为了给我一个更难的错误。

未定义对`_sbrk'的引用

3 个答案:

答案 0 :(得分:3)

您必须#include <stdio.h>才能使用sprintf()

答案 1 :(得分:2)

您要确保同时添加对

stdio.h
see this ref

的引用

答案 2 :(得分:0)

您可能需要将sprintf(str, "%d", timer)放在函数内(而不是源代码的全局部分)。

类似的东西:

#include <stdlib.h>
char str[20];
// SPOT #1
int f() {
    sprintf(str, "%d", timer); // this won't work if placed on SPOT #1
}