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.
/*
* C++ Program to Find Maximum Value using Macros
*/
#include <iostream>
#define MAX(a, b) (a > b ? a : b)
int main()
{
int a = 10, b = 20, c = -30;
std::cout << "MAX(10, 20) = " << MAX(a, b)
<< std::endl;
std::cout << "MAX(-30, 20) = " << MAX(c, b)
<< std::endl;
}
$ a.out Maximum of 10 and 20 = 20 Maximum of -30 and 20 = 20
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
Next Steps:
- Get Free Certificate of Merit in C++ Programming
- Participate in C++ Programming Certification Contest
- Become a Top Ranker in C++ Programming
- Take C++ Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Practice Computer Science MCQs
- Apply for C++ Internship
- Practice Programming MCQs
- Apply for Computer Science Internship
- Buy Programming Books