The reason why #define in the C language is wrong in the multi-line macro definition

The reason why #define in the C language is wrong in the multi-line macro definition

1. The first type of error

#include<stdio.h>
#define echange(a,b) {
    
    \/*宏定义中允许包含多行命令的情形,此时必须在最右边加上"\"*/
 int t;\
 t=a;\
 a=b;\
 b=t;\
}
main()
{
    
    
 int c, d;
 c = 2;
 d = 3;
 printf("%d %d\n", c, d);
 echange(c,d)
 printf("%d %d\n", c, d);
 return 0;
}

When using #define for multi-line macro definition, comments should be placed before "\"

2. The second mistake

#include<stdio.h>
#define echange(a,b) {
    
    /*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上"\"*/\
 int t;\
 t=a;\
 a=b;\
 b=t;\
}/*在最后一行多加了一个"\"*/\
main()
{
    
    
 int c, d;
 c = 2;
 d = 3;
 printf("%d %d\n", c, d);
 echange(c,d)
 printf("%d %d\n", c, d);
 return 0;
}

When using #define for multi-line macro definition, add "\" to the last line. When we use #define for multi-line definition, the line next to the last "\" by default also belongs to the scope of macro definition. Remove the "\" in the last line " just

The following is the correct form of the code

#include<stdio.h>
#define echange(a,b) {
    
    /*宏定义中允许包含两道衣裳命令的情形,此时必须在最右边加上"\"*/\
 int t;\
 t=a;\
 a=b;\
 b=t;\
}
main()
{
    
    
 int c, d;
 c = 2;
 d = 3;
 printf("%d %d\n", c, d);
 echange(c,d)
 printf("%d %d\n", c, d);
 return 0;
}

Guess you like

Origin blog.csdn.net/qq_45158026/article/details/104025617