C++ Program to Find the Maximum Value using Macros

This C++ program computes the maximum value using macros. A macro is a #define preprocessor directive which give instruction to the compiler to preprocess the information before actual compilation starts. All subsequent occurrences of macro in the program will be replaced by replacement-text before the program is compiled. The macro-text should be written carefully using parentheses as several complications may occur while compiling.

Here is the source code of the C++ program computes the maximum value using macros. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C++ Program to Find Maximum Value using Macros
  3.  */
  4. #include <iostream>
  5. #define MAX(a, b) (a > b ? a : b)
  7. int main()
  8. {
  9.     int a = 10, b = 20, c = -30;
  11.     std::cout << "MAX(10, 20) = " << MAX(a, b)
  12.               << std::endl;
  13.     std::cout << "MAX(-30, 20) = " << MAX(c, b)
  14.               << std::endl;
  15. }

$ a.out
Maximum of 10 and 20 = 20
Maximum of -30 and 20 = 20

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

If you wish to look at all C++ Programming examples, go to C++ Programs.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.