簡體   English   中英

我的 gcc 編譯器警告我函數的隱式聲明,即使聲明在代碼中明確給出

[英]My gcc compiler giving me warning for implicit declaration of function even though the declaration is clearly given in the code

我的 GCC 編譯器給我警告:

power.c: 在函數“main”中:power.c:9:36: 警告:函數“power”的隱式聲明 [-Wimplicit-function-declaration] 9 | printf("%d \t %d \t %d \n", i+1, 冪(2,i), 冪(-6,i));

而我已經明確地聲明了 power 函數的隱式,我在下面的代碼中給出了它:

#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;
}

您需要在調用之前轉發聲明您的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之前。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM