预处理-02-带参宏定义

C语言允许宏带有参数。在宏定义中的参数称为形式参数,在宏调用中的参数称为实际参数。

对带参数的宏,在调用中,不仅要宏展开,而且要用实参去代换形参。

带参宏定义的一般形式为:#define  宏名(形参表)  字符串

带参宏调用的一般形式为:宏名(实参表);  

例如:

#define M(y) y*y+3*y      /*宏定义*/
   ……
k=M(5);                   /*宏调用*/
   ……

在宏调用时,用实参5去代替形参y,经预处理宏展开后的语句为:
     k=5*5+3*5
例子:

#include <stdio.h>

#define MAX(a,b) (a>b)?a:b

void main()
{     
      int x, y, max;
      
      printf("input two numbers: ");
      
      scanf("%d %d", &x, &y);      
      max = MAX(x, y); // max = (x>y) ? x : y;
      
      printf("The max is %d

", max);
      
}
View Code

对于带参的宏定义有以下问题需要说明:

1. 带参宏定义中,宏名和形参表之间不能有空格出现。
例如把:
    #define MAX(a,b) (a>b)?a:b
写为:
       #define MAX  (a,b)  (a>b)?a:b

#include <stdio.h>

#define MAX (a,b) (a>b)?a:b

void main()
{
      
      int x, y, max;
      
      printf("input two numbers: ");
      
      scanf("%d %d", &x, &y);      
      max = MAX(x, y);
      
      printf("The max is %d

", max);
      
}
//程序错误,无法执行
View Code

2. 在带参宏定义中,形式参数不分配内存单元,因此不必作类型定义。而宏调用中的实参有具体的值。要用它们去代换形参,因此必须作类型说明。
这是与函数中的情况不同的。在函数中,形参和实参是两个不同的量,各有自己的作用域,调用时要把实参值赋予形参,进行“值传递”。而在带参宏中,只是符号代换,不存在值传递的问题。

  例子:

#include <stdio.h>

#define SAY(y) (y)

void main()
{      
      int i = 0;
      char say[] = {73, 32, 108, 111, 118, 101, 32, 102, 105, 115, 104, 99, 46, 99, 111, 109, 33, 0};

      while( say[i] )
      {
            say[i] = SAY(say[i]*1+1-1);
            i++;
      }

 
      printf("
	%s

", say);
      
      
}
View Code

3.在宏定义中的形参是标识符,而宏调用中的实参可以是表达式。例子同上

4. 在宏定义中,字符串内的形参通常要用括号括起来以避免出错。例如宏定义中(y)*(y)表达式的y都用括号括起来,因此结果是正确的。如果去掉括号,把程序改为以下形式:

#include <stdio.h>

#define SQ(y) (y)*(y)   // y*y试试

void main()
{
      int a, sq;
      
      printf("input a number: ");
      
      scanf("%d", &a);
      
      sq = SQ(a+1); // y*y试试:sq = a+1 * a+1
      
      printf("sq = %d
", sq);
      
}
View Code

将上述例子进行变态,我们现在要160 ÷ SQ(y):

#include <stdio.h>

#define SQ(y) ((y)*(y))  //必须包含外边的括号,不然sq =160/(a+1)*(a+1)

void main()
{
      int a, sq;
      
      printf("input a number: ");
      
      scanf("%d", &a);
      
      sq = 160 / SQ(a+1);  // sq = 160 / ((a+1) * (a+1));
      
      printf("sq = %d
", sq);
      
}
View Code

5. 带参的宏和带参函数很相似,但有本质上的不同,除上面已谈到的各点外,把同一表达式用函数处理与用宏处理两者的结果有可能是不同的。
课后题:比较比较

//example1
#include <stdio.h>

void main()
{      
      int i=1;
      int SQ(int y);
      
      while( i <= 5 )
      {
            printf("%d
", SQ(i++) );
      }
      
}

int SQ(int y)
{
      return( (y) * (y) );//1 4 9 16 25
}


//example2
#include <stdio.h>

#define SQ(y) ( (y)*(y) )

void main()
{
      int i = 1;
      
      while( i <= 5 ) 
      {
            printf("%d
", SQ(i++) );//1 9 15
      }
}
View Code

6. 宏定义也可用来定义多个语句,在宏调用时,把这些语句又代换到源程序内。看下面的例子:

#include <stdio.h>
#include <string.h>

#define STR(s1, s2, s3, sum)  strcat( strcat( strcat(sum, s1), s2 ), s3)
//strcat(a,b)函数:相当于python中的,a.append(b),不过这里只用于字符串

void main()
{
      
      char str1[] = "I ", str2[] = "love ", str3[] = "Fishc.com!", str[40] = "";
      
      STR(str1, str2, str3, str); // strcat( strcat( strcat(str, str1), str2 ), str3)
      
      printf("
	str1 = %s
	str2 = %s
	str3 = %s
	Str  = %s


", str1, str2, str3, str);

      str[0] = 0;//清空字符串 0相当于
      STR(str2, str1, str3, str);
      printf("
	str1 = %s
	str2 = %s
	str3 = %s
	Str  = %s


", str1, str2, str3, str);
}
View Code





原文地址:https://www.cnblogs.com/tianqizhi/p/10069843.html