gpt4 book ai didi

c - c 标准库中函数的 "Implict declaration"? (头文件已经包含)

转载 作者:太空宇宙 更新时间:2023-11-04 00:29:05 25 4
gpt4 key购买 nike

函数 gcvt 包含在 stdlib.h 中。当我尝试编译它时:

    gcc -std=c99 problem.c 2.c -o problem

我得到了信息:

     warning: implicit declaration of function ‘gcvt’ [-Wimplicit-function-declaration]
fstr = gcvt(datad,10,buff);

并且无法正确执行。

但我可以通过在功能测试之前在 2.c 中添加一行来解决这个问题:

   char *gcvt(double number, int ndigit, char *buf);

有人知道为什么吗?

这是我的代码:

2.c

    #include "2.h"
//char *gcvt(double number, int ndigit, char *buf);--must add this line
void test() {

double datad = 2.3;

char buff[100];
char *fstr;
fstr = gcvt(datad,10,buff);


printf("%s",fstr);
}

2.h

    #ifndef XXX 
#define XXX
#include<stdlib.h>
#include<stdio.h>
void test();
#endif

问题.c

    #include "2.h"

int main()
{
test();
return 0;
}

最佳答案

您要调用的函数已在 15 年前标记为过时。阻止人们使用这些函数(实际上不会破坏程序)的正常方法是将函数的实现保留在标准库中,但从头文件中删除声明(或至少使其难以启用)。

使用 snprintf 来完成同样的事情。

关于c - c 标准库中函数的 "Implict declaration"? (头文件已经包含),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36696402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com