请教,printf()可以用宏来定义吗?

fushaobing2010 2014-09-29 06:03:14
请教一个问题。

对于参数固定的函数,我可以用如下的方法来定义。
#ifdef DEBUG
#define dprint(X,Y) debugPrint(X, Y)
#else
#define dprint(X,Y)
#endif

但是,对于printf(char *fmt, ...)这样参数不固定的函数,可以像上面那样用宏定义吗?
...全文
342 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
fushaobing2010 2014-11-22
  • 打赏
  • 举报
回复
多谢各位高人的热心指点! 我是做嵌入式开发的,在ARM Cortex-M0 的编译器上试验过了,是可行的。 我用下面的形式: #define LOGPRINT(...) printf(__VA_ARGS__)
lx3275852 2014-10-06
  • 打赏
  • 举报
回复
c99、c++11支持 __VA_ARGS__
#define dprint(X, ... ) printf( X, __VA_ARGS__ ) 
注意X和...之间有逗号 或者用 ##即可。。。
#define dprint(X,args... ) printf( X, ##args) 
注意args和...之间没逗号
fushaobing2010 2014-09-30
  • 打赏
  • 举报
回复
多谢回复!我学习一下。
赵4老师 2014-09-30
  • 打赏
  • 举报
回复
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.
「已注销」 2014-09-30
  • 打赏
  • 举报
回复
// 有的编译器支持 "##__VA_ARGS__","..." 可以为空:
#define LOGPRINT(fmt, ...) printf(fmt, ##__VA_ARGS__)
// 有的编译器不支持 "##__VA_ARGS__",只能写成这样:
#define LOGPRINT(...) printf(__VA_ARGS__)
mymtom 2014-09-30
  • 打赏
  • 举报
回复
引用 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
*/
wansbest 2014-09-30
  • 打赏
  • 举报
回复
可以! 比如

#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
mymtom 2014-09-30
  • 打赏
  • 举报
回复
可变参数宏,C99支持的
mujiok2003 2014-09-29
  • 打赏
  • 举报
回复
引用 1 楼 mujiok2003 的回复:
variadic maco
maco --> macro
mujiok2003 2014-09-29
  • 打赏
  • 举报
回复

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧