Clarification of why certain program in C works and not another

I’m on HackerRank, working on C programs. I recently passed an algorithm to determine the maximum number of four digits.

This is what passed:

#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 if(d > b && d > c && d > a){
       return d;
   }
    return 0;
}
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;
}

However, an earlier script where all of the if cases were the following:

if (a > b&&c&&d)
{
return a;
}
...

Why wouldn’t the above form pass? Is it just the platform of HackerRank or is there a syntactic reason why this doesn’t work?

While a > b&&c&&d might look like it should be equivalent of a > b && a > c && a > d, it is not. It means literally a > b and c and d. Or to write it differently:

(a > b) && (c) && (d)

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.