Pages

Wednesday, October 18, 2017

Easy tutorial for C / C ++ - macro constant and macro function

Easy tutorial for C / C ++ - macro constant and macro function

- You may need to use and change the same constant (or function) multiple times within a program. It is inefficient to find and fix the constants (or functions) each time.

- One way to compensate for this is to use the macro constant (function). Where the macro constants (functions) are executed in the process of pre-processing.

- For example, if you use several constants (functions) as a single macro constant (function), you only need to change the constant (function) in the definition part of the macro constant (function) if needed.

Macro constants :

Usage

#define '(1) Macro constant name' '(2) Constant or string to replace'

- Here, '(2)' means to replace the constant or string to be replaced with '(1) macro constant name'.

- Let's look at a simple example.

Example Code

#include <iostream>
#define MacC 3
using namespace std;

int main() {
     cout << " MacC = " << MacC << endl ;
     cout << " MacC + 7  = " << MacC + 7 << endl ;
          return 0;
}

- The macro constant named 'MacC' is defined and the 'MacC' is substituted for 3. In the main function, we print the result of adding '7' to 'MacC' and 'MacC'.

results :

 MacC = 3
 MacC + 7  = 10

- You can see that the result of operation 3 that is substituted by the macro constant is printed.

Macro function :

- Macro function acts like a macro constant but function. The difference with regular functions is that you do not call the macro function but rather replace it in your program.

- Let's look at this part in the example below.

Usage

#define '(1) Macro function name (argument)' '(2) Operation to replace or string'

Note)
- There must be no space between the macro function name and the trailing parenthesis '('.

TprM(x) cout<<x<<endl; (O)
TprM   (x) cout<<x<<endl; (X)

Example Code

#include <iostream>
#define MacC 3

#define TprM(x) cout<<x<<endl;
#define TsumM1(x) x + x
#define TsumM2(x) (x + x)

using namespace std;

int main() {
     TprM(MacC);
     cout << " 2*TsumM1(" << MacC << ") = " << 2 * TsumM1(MacC) << endl ;
     cout << " 2*TsumM2(" << MacC << ") = " << 2 
* TsumM2(MacC) << endl ;
          return 0;
}

(1) TprM(x) cout << x << endl; : This is a macro function that prints 'x' as an argument.

(2) TsumM1(x) x + x: Macro function that takes the argument 'x' and replaces it with the operation of 'x + x'.

(3) TsumM2(x) (x + x): A macro function that takes the argument 'x' and replaces it with an operation of 'x + x'. We have added parentheses here.

- Let's compare the output of "2 * TsumM1(MacC)" with "2 * TsumM2(MacC)".

results :

3
 2*TsumM1(3) = 9
 2*TsumM2(3) = 12

(1) As a result of the macro function 'TprM', 3 is printed. Where the 3 was substituted for the macro constant 'MacC' in the main function.

(2) The result is "2 * TsumM1(3) = 9". The reasons are as follows. Since the macro function is substituted, so if it expresses the above expression, it becomes 2 * 3 + 3 and gives the results of 9.

(3) The result of "2 * TsumM2(3) = 12" is obtained where the expression is 2 * (3 + 3). Parentheses can be used as a way to get right results.

No comments:

Post a Comment