【问题标题】:My gcc compiler giving me warning for implicit declaration of function even though the declaration is clearly given in the code即使在代码中明确给出了声明,我的 gcc 编译器也会警告我隐式声明函数
【发布时间】:2021-05-30 14:28:43
【问题描述】:

我的 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;
}

【问题讨论】:

    标签: c gcc


    【解决方案1】:

    您需要在调用之前转发声明您的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 之前。

    【讨论】:

    • 谢谢,实际上我没有看到,问了一个非常愚蠢的问题。大声笑
    • 定义始终也是声明。不是定义的声明通常称为前向声明,其中区别很重要。大多数时候,一个声明就足以编译代码,有时(推断返回类型)需要定义。
    猜你喜欢
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    • 2011-02-21
    相关资源
    最近更新 更多