C调试宏:错误:预期表达式[重复]

时间:2022-12-03 22:43:03

This question already has an answer here:

这个问题在这里已有答案:

Every time, I invoke the below C debug macro, I have to pass some argument. Else compilation fails.

每次,我调用下面的C调试宏,我都要传递一些参数。否则编译失败。

#include <stdio.h>
#include <stdlib.h>

#define debug(fmt, ...)\
do{\
fprintf(stdout, "%s(%d) : " fmt, __FUNCTION__, __LINE__, __VA_ARGS__);\
}while(0)

int
main()
{
    debug("Debug 1");
}   

Here is the compilation error :

这是编译错误:

test.c:12:5: error: expected expression debug("Debug 1"); ^ test.c:6:70: note: expanded from macro 'debug' fprintf(stdout, "%s(%d) : " fmt, FUNCTION, LINE, VA_ARGS);\ ^ 1 error generated.

test.c:12:5:错误:预期表达式debug(“Debug 1”); ^ test.c:6:70:注意:从宏'debug'fprintf扩展(stdout,“%s(%d):”fmt,FUNCTION,LINE,VA_ARGS); \ ^ 1生成错误。

If I invoke the same macro with an argument :

如果我使用参数调用相同的宏:

debug("Debug 1 %s", "");

It compiles fine with no issues. Is is because of the compiler ? Does it work in latest compiler ?

它没有任何问题编译好。是因为编译器?它在最新的编译器中有效吗?

1 个解决方案

#1


2  

Variadic macros allow you to pass one or more arguments to a macro in one fell swoop. They do not allow you to pass zero arguments in its place.

Variadic宏允许您一次性将一个或多个参数传递给宏。它们不允许您在其位置传递零参数。

Write a second macro that you can use if you do not wish to pass arguments.

如果您不希望传递参数,请编写第二个可以使用的宏。

[C99: 6.10.3/4]: If the identifier-list in the macro definition does not end with an ellipsis, the number of arguments (including those arguments consisting of no preprocessing tokens) in an invocation of a function-like macro shall equal the number of parameters in the macro definition. Otherwise, there shall be more arguments in the invocation than there are parameters in the macro definition (excluding the ...). There shall exist a ) preprocessing token that terminates the invocation.

[C99:6.10.3 / 4]:如果宏定义中的标识符列表没有以省略号结尾,则在调用类函数宏时,参数的数量(包括那些不包含预处理标记的参数)等于宏定义中的参数数量。否则,调用中的参数应该多于宏定义中的参数(不包括...)。应存在a)终止调用的预处理令牌。

If a different compiler (such as one of those distributed with Microsoft Visual Studio) allows you to pass zero arguments in place of ..., it is non-compliant. As you can read above, this rule is not implementation-dependant.

如果不同的编译器(例如与Microsoft Visual Studio一起分发的编译器)允许您传递零参数来代替...,则它是不符合的。如上所述,此规则与实现无关。

#1


2  

Variadic macros allow you to pass one or more arguments to a macro in one fell swoop. They do not allow you to pass zero arguments in its place.

Variadic宏允许您一次性将一个或多个参数传递给宏。它们不允许您在其位置传递零参数。

Write a second macro that you can use if you do not wish to pass arguments.

如果您不希望传递参数,请编写第二个可以使用的宏。

[C99: 6.10.3/4]: If the identifier-list in the macro definition does not end with an ellipsis, the number of arguments (including those arguments consisting of no preprocessing tokens) in an invocation of a function-like macro shall equal the number of parameters in the macro definition. Otherwise, there shall be more arguments in the invocation than there are parameters in the macro definition (excluding the ...). There shall exist a ) preprocessing token that terminates the invocation.

[C99:6.10.3 / 4]:如果宏定义中的标识符列表没有以省略号结尾,则在调用类函数宏时,参数的数量(包括那些不包含预处理标记的参数)等于宏定义中的参数数量。否则,调用中的参数应该多于宏定义中的参数(不包括...)。应存在a)终止调用的预处理令牌。

If a different compiler (such as one of those distributed with Microsoft Visual Studio) allows you to pass zero arguments in place of ..., it is non-compliant. As you can read above, this rule is not implementation-dependant.

如果不同的编译器(例如与Microsoft Visual Studio一起分发的编译器)允许您传递零参数来代替...,则它是不符合的。如上所述,此规则与实现无关。