gpt4 book ai didi

c - 宏编译

转载 作者:行者123 更新时间:2023-11-30 18:18:41 25 4
gpt4 key购买 nike

我在编译代码时收到这些错误消息。我很困惑。我使用 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; )

#define SWAP(t,x,y) \
do { t type = x; x = y; y = type; } while (0)

并更改调用:

swap(int, x, y);

SWAP(int, x, y);

宏定义中的一些注意事项:\ 之后不能有空行,而且 () 只能在表达式之间使用,不能在语句之间使用。

关于c - 宏编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32258794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com