What does this #define syntax mean?

主宰稳场 提交于 2019-12-29 08:56:07

问题


I came across this one, don't understand.

#define IDEBUG(a...)  

What does the "(a...)" mean?


回答1:


That's a variadic macro.

Quoting verbatim from the linked page:

A macro can be declared to accept a variable number of arguments much as a function can. The syntax for defining the macro is similar to that of a function. Here is an example:

 #define eprintf(...) fprintf (stderr, __VA_ARGS__)

This kind of macro is called variadic. When the macro is invoked, all the tokens in its argument list after the last named argument (this macro has none), including any commas, become the variable argument. This sequence of tokens replaces the identifier VA_ARGS in the macro body wherever it appears. Thus, we have this expansion:

 eprintf ("%s:%d: ", input_file, lineno)
      ==>  fprintf (stderr, "%s:%d: ", input_file, lineno)

And for that specific form, quoting further down in the page:

If your macro is complicated, you may want a more descriptive name for the variable argument than __VA_ARGS__. CPP permits this, as an extension. You may write an argument name immediately before the `...'; that name is used for the variable argument. The eprintf macro above could be written

#define eprintf(args...) fprintf (stderr, args)



回答2:


Variable number of parameters. See variadic macros




回答3:


It is a variadic macro.

A variadic macro is a macro that accepts a variable number of arguments. The feature has been introduced in C99.

The form

#define IDEBUG(a...)  printf(a)

with the parameter a... is a GNU extension, a gives a name to the __VA_ARGS__ identifier.

The standard C99 form would be

#define IDEDBUG(...)  printf(__VA_ARGS__)


来源:https://stackoverflow.com/questions/9031728/what-does-this-define-syntax-mean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!