宏编译
Macro compilation
我在编译代码时收到这些错误消息。我很困惑。我使用 clang 编译器,代码是用 C 编写的。宏应该交换两个 t 类型的参数。
Documents/program7.c:5:5: error: expected ')'
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:1: note: to match this '('
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:3: warning: type specifier missing, defaults to 'int'
[-Wimplicit-int]
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:15: warning: type specifier missing, defaults to 'int'
[-Wimplicit-int]
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:19: error: use of undeclared identifier 'y'
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:22: warning: type specifier missing, defaults to 'int'
[-Wimplicit-int]
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:26: error: use of undeclared identifier 'type'
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:32: error: expected identifier or '('
( t type = x; x = y; y = type; )
代码:
#include <stdio.h>
#define SWAP(t,x,y) \
( t type = x; x = y; y = type; )
int main()
{
int x, y;
x = 2, y = 10;
swap(int, x, y);
printf("%d %d\n", x, y);
return 0;
}
您的宏需要括号:
#define SWAP(t,x,y) \
{ \
t type = x; x = y; y = type; \
}
并且区分大小写。所以你的宏调用应该是:
SWAP(int, x, y);
话虽如此,我会让 swap
成为内联函数而不是宏。
替换你的:
#define SWAP(t,x,y) \
( t type = x; x = y; y = type; )
和
#define SWAP(t,x,y) \
do { t type = x; x = y; y = type; } while (0)
并更改调用:
swap(int, x, y);
到
SWAP(int, x, y);
宏定义中的一些注意事项:\
后不能有空行,而且 ()
只能在表达式之间使用,不能在语句之间使用。
您的代码至少存在三个不同的问题:
- 您打算作为宏替换值的代码位于顶层,因为您在
#define
和替换文本之间插入了一个空行。这吸收了行连接反斜杠。
- 您在括号内有一个完整的以分号结束的语句(实际上是几个)。括号不能包含语句,只能包含表达式。
- 您尝试通过小写名称而不是您定义的大写名称来使用宏。
你想要更像这样的东西:
#include <stdio.h>
#define SWAP(t,x,y) \
do { t type = x; x = y; y = type; } while (0)
int main()
{
int x, y;
x = 2, y = 10;
SWAP(int, x, y);
printf("%d %d\n", x, y);
return 0;
}
#define SWAP(t,x,y) \
( t type = x; x = y; y = type; )
你多了一个空行。摆脱它。但是,这只会给您带来更多错误,以便稍后在您使用宏 SWAP
时修复,因为您不能那样使用括号。
do {} while(0)
习惯用法对于定义内联作用域很有用。
#define SWAP(t,x,y) do { t tmp = x; x = y; y = tmp; } while(0)
在您的代码中:
int x, y;
x = 2, y = 10;
swap(int, x, y);
您正在调用 swap
,没有为其定义宏。应该改为调用 SWAP
。
我在编译代码时收到这些错误消息。我很困惑。我使用 clang 编译器,代码是用 C 编写的。宏应该交换两个 t 类型的参数。
Documents/program7.c:5:5: error: expected ')'
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:1: note: to match this '('
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:3: warning: type specifier missing, defaults to 'int'
[-Wimplicit-int]
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:15: warning: type specifier missing, defaults to 'int'
[-Wimplicit-int]
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:19: error: use of undeclared identifier 'y'
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:22: warning: type specifier missing, defaults to 'int'
[-Wimplicit-int]
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:26: error: use of undeclared identifier 'type'
( t type = x; x = y; y = type; )
^
Documents/program7.c:5:32: error: expected identifier or '('
( t type = x; x = y; y = type; )
代码:
#include <stdio.h>
#define SWAP(t,x,y) \
( t type = x; x = y; y = type; )
int main()
{
int x, y;
x = 2, y = 10;
swap(int, x, y);
printf("%d %d\n", x, y);
return 0;
}
您的宏需要括号:
#define SWAP(t,x,y) \
{ \
t type = x; x = y; y = type; \
}
并且区分大小写。所以你的宏调用应该是:
SWAP(int, x, y);
话虽如此,我会让 swap
成为内联函数而不是宏。
替换你的:
#define SWAP(t,x,y) \
( t type = x; x = y; y = type; )
和
#define SWAP(t,x,y) \
do { t type = x; x = y; y = type; } while (0)
并更改调用:
swap(int, x, y);
到
SWAP(int, x, y);
宏定义中的一些注意事项:\
后不能有空行,而且 ()
只能在表达式之间使用,不能在语句之间使用。
您的代码至少存在三个不同的问题:
- 您打算作为宏替换值的代码位于顶层,因为您在
#define
和替换文本之间插入了一个空行。这吸收了行连接反斜杠。 - 您在括号内有一个完整的以分号结束的语句(实际上是几个)。括号不能包含语句,只能包含表达式。
- 您尝试通过小写名称而不是您定义的大写名称来使用宏。
你想要更像这样的东西:
#include <stdio.h>
#define SWAP(t,x,y) \
do { t type = x; x = y; y = type; } while (0)
int main()
{
int x, y;
x = 2, y = 10;
SWAP(int, x, y);
printf("%d %d\n", x, y);
return 0;
}
#define SWAP(t,x,y) \
( t type = x; x = y; y = type; )
你多了一个空行。摆脱它。但是,这只会给您带来更多错误,以便稍后在您使用宏 SWAP
时修复,因为您不能那样使用括号。
do {} while(0)
习惯用法对于定义内联作用域很有用。
#define SWAP(t,x,y) do { t tmp = x; x = y; y = tmp; } while(0)
在您的代码中:
int x, y;
x = 2, y = 10;
swap(int, x, y);
您正在调用 swap
,没有为其定义宏。应该改为调用 SWAP
。