C Language: C program to define Macro to find maximum/minimum of two numbers
C – Macro to find maximum/minimum of two numbers.
In computer programming, a macro is a rule or pattern that specifies how a certain input should be mapped to a replacement output. Applying a macro to an input is known as macro expansion.
Here are the macros for maximum and minimum:
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
Example:
Code:
# include <stdio.h>
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
int main() {
int a = 45, b = 34;
printf("The Maximum of %d and %d is %d\n", a, b, MAX(a, b));
printf("The Minimum of %d and %d is %d\n", a, b, MIN(a, b));
return 0;
}
Output:
The Maximum of 45 and 34 is 45 The Minimum of 45 and 34 is 34
In the above code, the MAX and MIN macros are defined using the ternary operator to check which of the two numbers is larger or smaller. The macros take two arguments x and y, representing the two numbers to be compared.
The main() function calls the macros with two integer values a and b, and the results are printed to the console.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/c-programming-exercises/c-snippets/c-program-to-define-macro-to-find-maximum-minimum-of-two-numbers.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics