-3
int max_of_four(int a,int b,int c,int d){
    int max = a > b ? a : b;
    max = c > max ? c : max;
    max = d > max ? d : max;
    return max;
}

so this is the code I found on net I was gonna write the greatest number what does this code mean or '?' and ':' mean?

xDevsMC
  • 5
  • 2
  • 1
    https://en.wikipedia.org/wiki/Ternary_conditional_operator – Iłya Bursov Dec 13 '22 at 20:35
  • IMO, that's obscurely written code. Using the ternary operator like that is not good. The first line in the body of the function is OK. The other two could be written `if (c > max) max = c; if (d > max) max = d;`. And the generalization to the maximum of an array of values is probably written like `int max = a[0]; for (int i = 1; i < size; i++) { if (a[i] > max) max = a[i]; }`. – Jonathan Leffler Dec 13 '22 at 21:03

1 Answers1

0

Youre looking at "ternary" operators. They typically work of the form

condition ? value_if_true : value_if_false

So to translate c > max ? c : max; to a traditional if:

if (c > max) { return c } else return max;

Kevin Crum
  • 195
  • 2
  • 16
  • 6
    In [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section _Answer Well-Asked Questions_, and therein the bullet point regarding questions that "have been asked and answered many times before". – Charles Duffy Dec 13 '22 at 20:41