Is there any library function in c
for finding out the largest number between two numbers?
Asked
Active
Viewed 3.3k times
5

But I'm Not A Wrapper Class
- 13,614
- 6
- 43
- 65

RottieRango
- 105
- 1
- 1
- 5
-
5Can't you just use the > or < comparison operator for comparing two numbers? – Ahmed Salman Tahir Jan 24 '14 at 17:01
-
Oh damn!! max and min doesn't work with my c compiler... – RottieRango Jan 24 '14 at 17:08
3 Answers
13
You can do
#define new_max(x,y) (((x) >= (y)) ? (x) : (y))
#define new_min(x,y) (((x) <= (y)) ? (x) : (y))
valter

γηράσκω δ' αεί πολλά διδασκόμε
- 7,172
- 2
- 21
- 35
-
-
I think this is a bit buggy, since 2 - max(a,b) becomes (2 - ((a) >= (b))) ? (a) : (b), which is always just a. A safer define might be ((x) >= (y) ? (x) : (y)) – Alex Li Sep 25 '20 at 19:18
-
0
You can easily write your own function using comparison operators such as >
, <
, >=
, <=
, and ==.
Here is an example from this site:
#include <stdio.h>
int main()
{
int a, b;
scanf("%d %d", &a, &b);//get two int values from user
if (a < b)//is a less than b?
printf("%d is less than %d\n", a, b);
if (a == b)//is a equal to b?
printf("%d is equal to %d\n", a, b);
if (a > b)//is a greater than b?
printf("%d is greater than %d\n", a, b);
return 0;
}
You can use this knowledge to do a simple function that follows this pseudo code:
//get a and b int variable
//is a greater than or equal to b?
//return a
//else
//return b
Some other useful resources:
http://c.comsci.us/tutorial/icomp.html
http://www.cyberciti.biz/faq/if-else-statement-in-c-program/

But I'm Not A Wrapper Class
- 13,614
- 6
- 43
- 65
-1
Here is a example of using a IF statement to determine the largest of 3 numbers. Like Ahmed said.
/* C program to find largest number using if statement only */
#include <stdio.h>
int main(){
float a, b, c;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
if(a>=b && a>=c)
printf("Largest number = %.2f", a);
if(b>=a && b>=c)
printf("Largest number = %.2f", b);
if(c>=a && c>=b)
printf("Largest number = %.2f", c);
return 0;
}

Steve Church
- 344
- 1
- 3
- 12