即使代码中明确给出了声明,我的 gcc 编译器也会警告我隐式声明函数

时间:2021-05-30 14:28:43

标签: c gcc

我的 GCC 编译器给了我警告: power.c:在函数“main”中: power.c:9:36: 警告:函数‘power’的隐式声明[-Wimplicit-function-declaration] 9 | printf("%d \t %d \t %d \n", i+1, power(2,i), power(-6,i)); |

而我已经明确声明了幂函数是隐式的,我在下面的代码中给出了

#include <stdio.h>

int main(){
        int i;

        printf("%s \t %s \t %s \n", "Powers", "of 2", "of -6");
        for (i = 0; i < 10; ++i)
                printf("%d \t %d \t %d \n", i+1, power(2,i), power(-6,i));
        return 0;

}


int power(int base, int n){
        int i, p;

        p = 1;
        for (i=0; i <= n; ++i)
                p = p * base;
        return p;
}

1 个答案:

答案 0 :(得分:3)

您需要在调用之前转发声明您的 power 函数:

int power(int base, int n);

int main(){
        int i;

        printf("%s \t %s \t %s \n", "Powers", "of 2", "of -6");
        for (i = 0; i < 10; ++i)
                printf("%d \t %d \t %d \n", i+1, power(2,i), power(-6,i));
        return 0;

}

或者,您可以将 power 的定义移到 main 之前。