gpt4 book ai didi

c - 为什么我不能创建一个名为 pow() 的函数?

转载 作者:行者123 更新时间:2023-12-04 05:48:00 25 4
gpt4 key购买 nike

这是我的 C 程序:

#include <stdio.h> 

main() {
int a,b;
int pow(int,int);
printf("Enter the values of a and b");
scanf("%d %d",&a,&b);
printf("Value of ab is %d",pow(a,b));
}

pow(int c,int d) {
return c*d;
}

我的程序中没有包含 math.h。我正在使用 gcc 编译器。我收到以下错误

ex22.c: In function `main': 
ex22.c:6: error: conflicting types for `pow'

经过搜索我才知道在math.h中有一个pow函数。我不包括 math.h,但我仍然收到错误。怎么会?

最佳答案

您不应该为您自己的函数使用同时也是 C 标准库函数名称的标识符,无论您是否包含该标准函数的头文件。 C 标准明确禁止这样做,除非函数声明为 static,并且编译器可能会特殊处理此类函数(例如 pow(x, 2),通过发出代码对于 x*x 而不是函数调用)。

关于c - 为什么我不能创建一个名为 pow() 的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23631832/

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