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.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Apply for Computer Science Internship
- Apply for C++ Internship
- Practice Programming MCQs
- Check Programming Books
- Check Computer Science Books