【问题标题】:sprintf (convert int to char[])sprintf(将 int 转换为 char[])
【发布时间】:2013-02-07 14:26:45
【问题描述】:

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

我尝试通过以下方式做到这一点:

  char str[20];    
  sprintf(str, "%d", 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' 的未定义引用

【问题讨论】:

    标签: string int type-conversion printf


    【解决方案1】:

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

    【讨论】:

    • 是的,他是,而且我非常满意。我什至不能接受他的anwser jet,他太快了:P
    【解决方案2】:

    您要确保还添加对

    的引用
    stdio.h
    see this ref

    【讨论】:

      【解决方案3】:

      您可能需要将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
      }
      

      【讨论】:

      • 它在函数内部......所以这绝对不是它。 :)
      猜你喜欢
      • 2016-07-22
      • 2012-08-07
      • 1970-01-01
      • 1970-01-01
      • 2011-02-25
      • 2015-11-23
      • 2015-12-18
      • 2017-04-25
      相关资源
      最近更新 更多