Using macros

One useful feature of preprocessor symbols is macros. A macro has parameters and the preprocessor will ensure that the search and replace will replace a symbol in the macro with the symbol used as a parameter to the macro.

Edit the main function to look as follows:

    #include <iostream>      #define MESSAGE(c, v)      for(int i = 1; i < c; ++i) std::cout << v[i] << std::endl;      int main(int argc, char *argv[])     {         MESSAGE(argc, argv);         std::cout << "invoked with " << argv[0] << std::endl;     }

The main function calls a macro called MESSAGE and passes the command line parameters to it. The function then prints the first command line parameters (the invocation command) to the console. MESSAGE is not a function, it is a macro, which means ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.