How to find the max and min from three numbers with short-circuit boolean operators in C

1 Answer

0 votes
#include <stdio.h>

int max_3(int a, int b, int c) {
	int max = a;
	 
	(max < b) && (max = b); 
	 
	(max < c) && (max = c); 
 
	return max;
}
 
int min_3(int a, int b, int c) {
	int min = a;
	 
	(min > b) && (min = b);
	 
	(min > c) && (min = c);
	 
	return min;
}
 
int main()
{
	printf("%d\n", max_3(8, 9, 3));
	printf("%d\n", min_3(9, 7, 4));
	 
	return 0;
}



/*
run:

9
4

*/

 



answered Mar 25, 2019 by avibootz

Related questions

3 answers 316 views
1 answer 250 views
1 answer 243 views
1 answer 154 views
1 answer 224 views
...