[C language] Macro definition (macro definition without parameters and macro definition with parameters)

table of Contents

1. Macro definition without parameters

1. Definition

2. #undef instruction cancels macro definition

Two, macro definition with parameters

1. Definition

2. Precautions for substitution of macro definition parameters

Third, the difference between a parameterized macro definition and a parameterized function


1. Macro definition without parameters

1. Definition

In the program, some constants are often defined, such as Pi 3.1415, "ABC" and so on. If these constants are frequently used in the program, writing errors will inevitably occur. In order to avoid programming errors, you can use macro definitions without parameters to define these constants.

For example: #define PI 3.14159

Analysis: #define is used to identify a macro definition, the identifier PI is the defined macro name, 3.14159 is the macro body, it can be a constant or an expression, etc.

Generally, the macro definition needs to be placed at the beginning of the source program, outside of the function definition, its valid range is from the beginning of the macro definition statement to the end of the source file. Generally, the macro name is in uppercase letters, which is convenient to distinguish it from other operators.

Using a macro to define PI can replace the place where PI appears in the subsequent source code with 3.14159.

Case demonstration:

#include <stdio.h>
#define PI 3.141592

int main(){
	int r;
	scanf("%d",&r);
	double area=PI*r*r;
	double cicle=2*PI*r;
	printf("半径为%d的圆面积:area=%lf  周长cicle=%lf\n",r,area,cicle);
} 

 operation result:

2. #undef instruction cancels macro definition

The #undef instruction is used to cancel the macro definition. After defining a macro with #define, if the preprocessing sees the #undef instruction in the following source code, the macro after the #undef instruction will be invalid.


Two, macro definition with parameters

1. Definition

The syntax of the macro definition with parameters is as follows:

#define identifier (parameter list) string

The parameters in the formal parameter list are separated by commas. For macro definitions with parameters, it is also necessary to replace the macro name with a string and replace the formal parameters with actual parameters.

Since the macro definition is executed when the program is preprocessed, the overhead of the macro definition is smaller than that of the function.

[Note] The parameter replacement in the macro definition is "overall replacement".

Case Analysis:

#define ABS(x)((x)>=0?(X):-(X);

int a=12;

ABS(++a);

Our expected value is 12, but the actual result of a is 14. Obviously this is wrong to replace the absolute value function.

The reason is that "ABS(++a)" is replaced with "(++a)((++a)>=0?(++a):-(++a);" during preprocessing, so the result For 14

Case realization:

#include <stdio.h>
#define SWAP(a,b) {int temp;temp=a;a=b;b=temp;}

int main() {
	int a[5] = { 3,4,5,6,7 };
	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < 5 - i - 1; j++) {
			if (a[j] < a[j + 1])  
				SWAP(a[j], a[j + 1]);
		}
	}

	for (int i = 0; i < 5; i++) {
		printf("%d ", a[i]);
	}
	printf("\n");
}

operation result:

2. Precautions for substitution of macro definition parameters

  • If an operator appears in the string in the macro definition , you need to add parentheses at the appropriate position . If you do not add the parentheses, an error may occur.

#define S 3+4

a=S*c;//The sentence after the replacement of the macro definition is a=3+4*c, which does not meet expectations.

Modify to #define S (3+4)

Then replace the original sentence with a=(3+4)*c

  • There is no need to add a semicolon at the end of the macro definition . If a semicolon is added, it will be regarded as part of the string being replaced. The macro definition is just a simple string replacement without grammatical checking. Therefore, the macro replacement error will not be discovered until the system is compiled.
  • Macro definitions are allowed to be nested , and the defined macro name can be used in the string of the macro definition. It is replaced by the preprocessor nested during the replacement. But the macro definition does not support recursion .

#define PI 3.14159

#define P PI*x

double c=2*P; The statement after the macro replacement is c=2*3.14159*x;

Macro definition does not support recursion

So #define MAX MAX+5 is the wrong way to define


Third, the difference between a parameterized macro definition and a parameterized function

Basic operation Macro definition with parameters Function with parameters
Processing time During pretreatment When the program is running
Parameter Type no Need to define the type
Parameter passing No space is allocated and no value is passed (just string replacement) Allocate memory and pass actual parameters to formal parameters
Running speed fast Relatively slow, because the function call will involve the transfer of parameters, stacking and popping operations

 

Guess you like

Origin blog.csdn.net/Jacky_Feng/article/details/109139444