Macros
A macro is a label defined in the source code that is replaced by its value by the preprocessor before compilation. Macros are initialized with the #define
preprocessor command and can be undefined with the #undef
command.
There are two types of macros: object-like macros and function-like macros.
Object-Like Macros
These macros are replaced by their value in the source code before compilation. Their primary purpose is to define constants to be used in the code.
Note: Macro definitions are not followed by a semicolon
;
.
Example
In the following example, PI
is defined as an object-like macro:
#include <iostream>using namespace std;#define PI 3.1416int main() {float radius = 3;float area;area = PI * radius * radius;cout << "Area is " << area;return 0;}
This results in the output:
Area is 28.2744
Function-Like Macros
These macros behave like functions, in that they take arguments that are used in the replaced code.
Note: When defining a function-like macro, there cannot be a space between the macro name and the opening parenthesis.
Example
In the following example, AREA
is defined as a function-like macro. Note that other macros can be used in defining a subsequent macro. The inner macro is replaced by its value before the outer macro is replaced.
#include <iostream>using namespace std;#define PI 3.1416#define AREA(r) r * r * PIint main() {float radius = 5;float result;result = AREA(radius);cout << "Area is " << result;return 0;}
This results in the output:
Area is 78.54
Predefined Macros
C++ has a number of predefined macros, including the following:
__DATE__
: Current date formatted asMMM DD YYYY
.__TIME__
: Current time formatted asHH:MM:SS
.__FILE__
: Current filename.__LINE__
: Current line number.
Example
The following example uses the predefined macros mentioned above:
#include <iostream>using namespace std;int main() {char file[] = __FILE__;char date[] = __DATE__;char time[] = __TIME__;int line = __LINE__;cout << "File name: " << file << "\n";cout << "Date: " << date << "\n";cout << "Time: " << time << "\n";cout << "Line number: " << line << "\n";}
This results in the output:
File name: main.cppDate: Jun 25 2022Time: 14:17:50Line number: 9
Undefining a Macro
Once defined, a macro can be undefined with the #undef
command. Using the macro after that point will result in a compile error.
Example
#include <iostream>using namespace std;#define TEST 1int main() {#ifdef TESTcout << "TEST defined\n";#elsecout << "TEST undefined\n";#endif#undef TEST#ifdef TESTcout << "TEST defined\n";#elsecout << "TEST undefined\n";#endif}
This results in the output:
TEST definedTEST undefined
All contributors
- Christine_Yang271 total contributions
- garanews222 total contributions
- StevenSwiniarski474 total contributions
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.