簡體   English   中英

為什么我收到此編譯 r 錯誤:控制到達非無效 function [-Werror=return-type] 的結尾

[英]Why I am getting this Compilation r error: control reaches end of non-void function [-Werror=return-type]

c 編程的新手,即使我在其他 IDE 中獲得了 output。 其中一個 IDE 顯示了這個問題。

#include <stdio.h>

int max(int a, int b, int c, int d) {
    if (a>b && a>c && a>d){
        printf("%d",a);
    }
    else if (b>a && b>c && b>d ) {
        printf("%d",b);
    }
    else if (c>a && c>b && c>d ) {
        printf("%d",c);
    }
    else {
        printf("%d",d);
    }
}

int main() {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    max(a,b,c,d);
    return 0;
}

The function int max(int a, int b, int c, int d) is declared to return int but does not return anything just change the declaration to void max(int a, int b, int c, int d) if您想返回答案而不是打印,您可以在主 function 中返回並打印。

int max(int a, int b, int c, int d) {
    if (a>b && a>c && a>d){
        return a;
    }
    else if (b>a && b>c && b>d ) {
        return b;
    }
    else if (c>a && c>b && c>d ) {
        return c;
    }
    else {
        return d;
    }
}

int main() {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    printf("%d",max(a,b,c,d));
    return 0;
}

暫無
暫無

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

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