Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How to call macro definition function in C language?
How to call macro definition function in C language?
Just call directly. The following example:

# include & ltstdio.h & gt#define sum(b,c){ int d = 0; d = b+ c; Printf ("sum of the two: %d\n", d); }int main(){ int x=0,y = 0; scanf("%d%d ",& ampx & amp; y); sum (x,y); Returns 0; } When using macro functions or macro functions, pay attention to the following points:

The parameters of (1) macro function have no types, and the preprocessor is only responsible for form substitution, not parameter type checking, so be extra careful when passing parameters.

(2) macro function definition should pay attention to the format, especially the brackets.

If the macro function above is written as # definemax (a, b) (a >; b? A:b), remove the brackets, and the macro expansion becomes k =(I &;; 0x0f & gt Johnson & Johnson Company. 0x0f? Me & 0x0f: J & 0x0f), the priority of the operation is wrong. Similarly, the outer brackets of this macro definition cannot be omitted. If the macro in the function is replaced by ++MAX(a, b), the macro expansion becomes++(a) >; (b)? (a):(b), the operation priority is also wrong.

(3) Macro functions often lead to inefficient code execution.

int a[]={9,3,5,2, 1,0,8,7,6,4 }; int max(n) { return n==0? a[0]:MAX(a[n],MAX(n- 1)); } int main(){ max(9); Returns 0; If it is an ordinary function, the maximum value can be obtained by recursion, and the time complexity is O(n). But if it is a macro function, the macro expansion is (a [n] >; max(n- 1)? A[n]:max(n- 1)), in which max(n- 1) has been called twice, so if recursion goes on, the time complexity will be high.