samedi 7 novembre 2020

How to fix "error: control reaches end of non-void function [-Werror=return-type]" at HackerRank?

Okay, many answers are already provided, but I couldn't really seem to find the solution.

Our task is to find the greatest of four integers.

https://www.hackerrank.com/challenges/functions-in-c/problem

When I run the following code:

#include <stdio.h>

/*
int max_of_four(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 max_of_four(int a, int b, int c, int d)
{
    if(a>b)
    {
        if(a>c)
        {
            if(a>d)
            return a;
        }
    }
    
    else if(b>c)
    {
        if(b>d)
        return b;
    }
    
    else if(c>d)
    return c;
    
    else
    return d;
}


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

I am returned with this error at HackerRank:

Solution.c: In function ‘max_of_four’:
Solution.c:45:1: error: control reaches end of non-void function [-Werror=return-type]
 }
 ^
cc1: some warnings being treated as errors

But the above code does it's job when run on Dev-C++ 5.11.

If we use the function inside the comments max_of_four, and comment out the latter one, the job gets done as well.

If somebody could get me to know, how max_of_four can be tweaked to get the code to work aptly, I'd be grateful.

And what's causing the error?

Is it an error or a warning? If so, why is a warning being treated as error?

Aucun commentaire:

Enregistrer un commentaire