__line__C++ macro

The preprocessors are those programs that have our source code before the compilation process starts. These preprocessors contain some directives like #define, #include, etc. There are a total of 4 main types of preprocessor directives. One of them is Macros. Macros contain a piece of code in a program that is given a specific name. When the compiler encounters the name, the compiler replaces the name with the real/actual code. The ‘#define’ directive defines a macro.

Macro is an abbreviation that stands for some piece of code in a programming language. These Macros help to simplify and reduce the code to avoid redundancy and repetitive coding. So that errors are also reduced that mostly occur because of repeated coding. The main advantage regarding Macros is that it converts the code and makes a more readable assembly program.

Macro expansion

A macro contains a name, a set of formal parameters, and the body of code. The macro name is used with the set of parameters and replaced by some piece of code that is generated by its body. This forms a macro expansion. This allows the programmer to create and define pseudo operations, those operations that are desirable and those that are not implemented as the processor instructions. Macro has a specific quality in which it writes the program automatically. The name macro is written as it is as a keyword in the code. It acts as a call to that specific macro, just like a function call. Then this name is replaced by some other code of macro body as we have described above. This replacement of the call with the processed copy is the expansion of the macro call. And it is used in both C and C ++ as well.

Once a macro is defined, we cannot redefine it to a different value without removing the first original definition. But it is possible to redefine the macro with the same definition of already defined macros. The #undef directive is responsible for removing the definition of a macro. Once the definition is removed, we can now redefine the macro with a different value.

Macros in C++

There are two types:

  1. Object-like macros
  2. Function-like macros

Object-like macros

Object-like macros don’t take any argument. These are identifiers that are replaced by the value. These are mostly used to represent numeric values and constants.

#define Pl 3.17

Pl is the name of the macro that will be replaced by the value 3.17.

Function-like macros

These types of macros can accept arguments. These macros look like a function call. For instance, take an example where Max is the name of the macro.

#define Max(a,b) ((A)<(b)?(a): (b))

Macros usually don't generate the function calls.in C++, inline functions are preferable.

Predefined Macros

Microsoft C/C++ compiler that has MSVC predefined macros that depend on the language C++, the target of compilation, and the compiler options. MSVC supports predefined preprocessor macros that are required by ANSI/ISO C99 standards. These predefined macros use no arguments and can neither be redefined.

Many predefined macros can be used in C/C++ source code/programs. Some of them are described here that are most commonly used.

The compilation date of the program in the file is represented by this function macro. The date is a string for the month-day-year format and is a constant value. This type of macro is always defined.

It depicts the time for the translation of the unit that is preprocessed. Similar to date, it has a specific format in which it is written hours: minutes: seconds, the same way as the time is returned.

This type of macro is also always defined.

This represents the name of the current file. It expands to a literal character string. That ensures that the path that leads to the file is displayed. It is an always-defined macro.

It represents the line number in the integer format of a current source. This macro is defined like others, and its value is changed with the help of the #line.

Now we will highlight some examples of __line__C++ macro function.

To get an idea of the working of this macro, here we have quoted an example. Firstly a library for the input and output stream is used. Then we have used a function error log that maintains the log of errors. This has a parameter to accept the line in the form of a string and a message. This function will display the line number with the message that is passed from the main program to the macro.

#define LOG( msg ) errorLog( __LINE__, msg )

After that, we have defined a macro. This will accept the message line string from the main function that has passed as an argument when this macro is called. And similarly, the error log function is called with line macro and the message as parameters. The file will be saved with an extension of the .c source code file. As we have to see the Linux platform results, go to the terminal and apply the commands to compile and execute the file. Use a g++ compiler for C++.

$ g++ -o f f.c
$ ./f

You can see that even with the spaces, the line number is identified through the line macro. From this example, you have seen the functionality of a __line__ macro. But in the next example, all the macros are collectively in the program. We have used ‘cout’ to print lines. Each line contains a different macro in it, and that value is displayed. For instance, for the date we have used:

Std:: cout << ("Date :%s\n" , __Date) << std ::endl;

Now we will see the output. You can see that the file name, date of compilation, time of compilation, and the current line number are displayed very effectively through a single piece of code for every macro.

Uses/Advantages of macros

  • Less space is occupied as it converts the declaration of long variables into short ones.
  • When a macro is used with a name that provides the current statement a meaning, it is easier to understand the macro than the long codes.
  • Macros are not able to clean up or remove the trash, etc., by themselves. It depends on the programmer, and he decides when the macro is designed to exit and required to be clean so that the second file can use the macro easily.
  • The execution speed of a program is increased, which is a major advantage of using a macro in the code.


‘__line__C++ macro’ is an article written to elaborate on the basic functionalities of macro by describing the types and also the predefined macros and further their types. All these functions are explained with the help of an example. Macros are easily implemented and require less time in the compilation of the codes. These features are designed by those programmers who want to create innovations and conditions using a small piece of code in the base source program. For that purpose, in the end, the article is accompanied by the uses of macro in source code and describes how they are beneficial in some aspects.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.