Find the maximum of three integers using the ternary operator

Here is how you do it

max = ((a>b)?((a>c)?a:c):((b>c)?b:c));

Here is another way

max = ((a>b)?a:b)>c?((a>b)?a:b):c;

Here is some code to find the max of 4 numbers…
Method 1


#include <stdio.h>
#include <stdlib.h>

#define max2(x,y)  ((x)>(y)?(x):(y))
#define max4(a,b,c,d)  max2(max2((a),(b)),max2((c),(d)))

int main ( void )
{
     printf ( "Max: %d\n", max4(10,20,30,40));
     printf ( "Max: %d\n", max4(10,0,3,4));
     return 0;
}

Method 2

#include <stdio.h>
#include <stdlib.h>

int retMax(int i1, int i2, int i3, int i4)
{
    return(((i1>i2)?i1:i2) > ((i3>i4)?i3:i4)? ((i1>i2)?i1:i2):((i3>i4)?i3:i4));
}

int main()
{
    int val = 0 ;

    val = retMax(10, 200, 10, 530);
    val = retMax(9, 2, 5, 7);

    return 0;
}

One Thought on “Find the maximum of three integers using the ternary operator

  1. Santosh mahto on April 10, 2016 at 1:22 pm said:

    Well.. Did prgrm of finding max of 3 No’s many times…but it never clicked to me finding largest of 4 No’s using dis very approach…!! Liked ur code..!!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation