user-0 提问: 请教，printf()可以用宏来定义吗？

回复:
user: user-0
content: 多谢各位高人的热心指点！ 我是做嵌入式开发的，在ARM Cortex-M0 的编译器上试验过了，是可行的。  我用下面的形式： #define LOGPRINT(...) printf(__VA_ARGS__)

user: user-1
content: c99、c++11支持 __VA_ARGS__ #define dprint(X, ... ) printf( X, __VA_ARGS__ )  注意X和...之间有逗号  或者用 ##即可。。。  #define dprint(X,args... ) printf( X, ##args)  注意args和...之间没逗号

user: user-0
content: 多谢回复！我学习一下。

user: user-2
content: ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/dv_vclang/html/51e757dc-0134-4bb2-bb74-64ea5ad75134.htm  Collapse AllExpand All      Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: J# Code: JScript   Visual Basic C# Visual C++ J# JScript Visual C++ Language Reference  Variadic Macros  Example  See Also  Send Feedback     Variadic macros are function-like macros that contain a variable number of arguments.  Remarks To use variadic macros, the ellipsis may be specified as the final formal argument in a macro definition, and the replacement identifier __VA_ARGS__ may be used in the definition to insert the extra arguments. __VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them.  The C Standard specifies that at least one argument must be passed to the ellipsis, to ensure that the macro does not resolve to an expression with a trailing comma. The Visual C++ implementation will suppress a trailing comma if no arguments are passed to the ellipsis.  Support for variadic macros was introduced in Visual C++ 2005.  Example   Copy Code  // variadic_macros.cpp #include <stdio.h> #define EMPTY  #define CHECK1(x, ...) if (!(x)) { printf(__VA_ARGS__); } #define CHECK2(x, ...) if ((x)) { printf(__VA_ARGS__); } #define CHECK3(...) { printf(__VA_ARGS__); } #define MACRO(s, ...) printf(s, __VA_ARGS__)  int main() {    CHECK1(0, "here %s %s %s", "are", "some", "varargs1(1)\n");    CHECK1(1, "here %s %s %s", "are", "some", "varargs1(2)\n");   // won't print     CHECK2(0, "here %s %s %s", "are", "some", "varargs2(3)\n");   // won't print    CHECK2(1, "here %s %s %s", "are", "some", "varargs2(4)\n");     // always invokes printf in the macro    CHECK3("here %s %s %s", "are", "some", "varargs3(5)\n");     MACRO("hello, world\n");    // MACRO("error\n", EMPTY);   would cause C2059 }     Copy Code  here are some varargs1(1) here are some varargs2(4) here are some varargs3(5) hello, world    See Also Concepts Macros (C/C++) Send feedback on this topic to Microsoft.

user: user-3
content: // 有的编译器支持 "##__VA_ARGS__"，"..." 可以为空： #define LOGPRINT(fmt, ...) printf(fmt, ##__VA_ARGS__) // 有的编译器不支持 "##__VA_ARGS__"，只能写成这样： #define LOGPRINT(...) printf(__VA_ARGS__)

user: user-4
content: 引用 6 楼 mymtom 的回复:可变参数宏，C99支持的  #include <stdio.h>  #define debug(fmt, ...) \     printf("%s:%d " fmt "\n", __FILE__, __LINE__, __VA_ARGS__)  int main(int argc, char *argv[]) {      debug("argc=%d", argc);     return 0; }  /* output: hello.c:10 argc=1 */

user: user-5
content: 可以！  比如 #define LOG(format, ...) printf(format, ##__VA_ARGS__)   给你一个我用来调试代码用的  #ifdef DEBUG #define LOG_DEBUG(log_name, format, ...) \     fprintf(log_name, "\033[32m%s %s:%d %s()\033[0m DEBUG| "format"\n", __DATE__, __FILE__, __LINE__,  __func__, ##__VA_ARGS__) #else #define LOG_DEUGG(log_name, format, ...) #endif

user: user-4
content: 可变参数宏，C99支持的

user: user-6
content: 引用 1 楼 user-6 的回复:variadic  maco maco --> macro

user: user-6
content: variadic  maco