Write a C program to seek out most and minimal of two numbers utilizing macro. Tips on how to discover most or minimal between two numbers utilizing macro in C program. Logic to seek out most and minimal utilizing macro in C.
In final put up we realized including situations to our macro. We realized to verify even or odd quantity utilizing macro.
On this put up we’ll proceed the train additional. I’ll clarify how simply you’ll be able to remodel our most or minimal verify perform to macro.
Required information
Fundamental C programming, Macros, Conditional operator
There are a number of methods to verify most or minimal between two numbers. In case you missed I’ve listed down the hyperlinks beneath.
Tips on how to discover most or minimal utilizing macro?
I assume that you’re already conscious with macro syntax, the best way to outline and use. Therefore, with out losing a lot time allow us to get began.
Lets outline two macro that accepts two arguments say MAX(x, y)
and MIN(x, y)
. It is going to return most or minimal quantity respectively. For this train we’ll use conditional (ternary) operator to seek out most or minimal.
Instance:
#outline MAX(x, y) (x > y ? x : y)
#outline MIN(x, y) (x < y ? x : y)
Program to seek out most or minimal utilizing macro
/**
* C program to verify most/minimal utilizing macro
*/
#embody <stdio.h>
// Outline macro to verify most and minimal
#outline MAX(x, y) (x > y ? x : y)
#outline MIN(x, y) (x < y ? x : y)
int essential()
{
int num1, num2;
// Enter numbers from person
printf("Enter any two quantity to verify max and min: ");
scanf("%dpercentd", &num1, &num2);
printf("MAX(%d, %d) = %dn", num1, num2, MAX(num1, num2));
printf("MIN(%d, %d) = %dn", num1, num2, MIN(num1, num2));
return 0;
}
Tips on how to discover most or minimal between three numbers utilizing ternary operator?
Enter any two quantity to verify max and min: 10 20 MAX(10, 20) = 20 MIN(10, 20) = 10
Completely happy coding 😉