"too many arguments for format" 有宏
"too many arguments for format" with macro
我有一个调试宏,用于快速和脏输出。我一直在尝试确定如何在 gcc 5.4 上使代码编译干净。它不会在早期版本的 gcc (4.x) 或 clang (11.0.3) 上出现任何问题。错误是这样的:
main.c: In function ‘main’:
main.c:4:38: warning: too many arguments for format [-Wformat-extra-args]
do { if (1){ fprintf(stdout, "debug:%s:%04d:%s: " fmt, __FILE__, \
^
main.c:10:2: note: in expansion of macro ‘DEBUGPRINT’
DEBUGPRINT("How to have no arguments?\n", NULL);
我一直用来尝试确定如何解决此问题的代码是:
#include <stdio.h>
#define DEBUGPRINT(fmt, ...) \
do { if (1){ fprintf(stdout, "debug:%s:%04d:%s: " fmt, __FILE__, \
__LINE__, __func__, __VA_ARGS__);} } while (0)
int main(int argc, char *argv[])
{
DEBUGPRINT("nums: %04i, %04i\n", 0x1234,0x5678);
DEBUGPRINT("How to have no arguments?\n", NULL);
return(0);
}
正如大家所见,如果我有论据,那没有问题。仅当我收到一条没有参数的消息时。我想我可以用“%s”传递一个“\n”,但我只是好奇是否有办法处理 NULL。
需要省略 fmt
参数,并且您需要将打印的固定部分与传入的参数分开。
#include <stdio.h>
#define DEBUGPRINT(...) \
do { printf("debug:%s:%04d:%s: ", __FILE__, __LINE__, __func__);\
printf(__VA_ARGS__);\
} while (0)
int main(int argc, char *argv[])
{
DEBUGPRINT("nums: %04i, %04i\n", 0x1234,0x5678);
DEBUGPRINT("How to have no arguments?\n");
return(0);
}
输出:
debug:x1.c:0016:main: nums: 4660, 22136
debug:x1.c:0017:main: How to have no arguments?
为了没有参数,你可以这样做:
#include <stdio.h>
/* SEPARATE THE COMMA FROM THE __VA_ARGS__ macro with
* the ## token */
#define DEBUGPRINT(fmt, ...) \
do { if (1){ fprintf(stdout, "debug:%s:%04d:%s: " fmt, __FILE__, \
__LINE__, __func__,## __VA_ARGS__);} } while (0)
/* THIS TOKEN ELIMINATES THE COMMA ------------^^ */
int main(int argc, char *argv[])
{
DEBUGPRINT("nums: %04i, %04i\n", 0x1234,0x5678);
DEBUGPRINT("How to have no arguments?\n");
return(0);
}
恐怕它没有包含在标准中,但它是 CLANG 和 GCC 编译器共享的扩展。
这是我系统上的输出:
$ a.out
debug:pru3225.c:0012:main: nums: 4660, 22136
debug:pru3225.c:0013:main: How to have no arguments?
$ _
我有一个调试宏,用于快速和脏输出。我一直在尝试确定如何在 gcc 5.4 上使代码编译干净。它不会在早期版本的 gcc (4.x) 或 clang (11.0.3) 上出现任何问题。错误是这样的:
main.c: In function ‘main’:
main.c:4:38: warning: too many arguments for format [-Wformat-extra-args]
do { if (1){ fprintf(stdout, "debug:%s:%04d:%s: " fmt, __FILE__, \
^
main.c:10:2: note: in expansion of macro ‘DEBUGPRINT’
DEBUGPRINT("How to have no arguments?\n", NULL);
我一直用来尝试确定如何解决此问题的代码是:
#include <stdio.h>
#define DEBUGPRINT(fmt, ...) \
do { if (1){ fprintf(stdout, "debug:%s:%04d:%s: " fmt, __FILE__, \
__LINE__, __func__, __VA_ARGS__);} } while (0)
int main(int argc, char *argv[])
{
DEBUGPRINT("nums: %04i, %04i\n", 0x1234,0x5678);
DEBUGPRINT("How to have no arguments?\n", NULL);
return(0);
}
正如大家所见,如果我有论据,那没有问题。仅当我收到一条没有参数的消息时。我想我可以用“%s”传递一个“\n”,但我只是好奇是否有办法处理 NULL。
需要省略 fmt
参数,并且您需要将打印的固定部分与传入的参数分开。
#include <stdio.h>
#define DEBUGPRINT(...) \
do { printf("debug:%s:%04d:%s: ", __FILE__, __LINE__, __func__);\
printf(__VA_ARGS__);\
} while (0)
int main(int argc, char *argv[])
{
DEBUGPRINT("nums: %04i, %04i\n", 0x1234,0x5678);
DEBUGPRINT("How to have no arguments?\n");
return(0);
}
输出:
debug:x1.c:0016:main: nums: 4660, 22136
debug:x1.c:0017:main: How to have no arguments?
为了没有参数,你可以这样做:
#include <stdio.h>
/* SEPARATE THE COMMA FROM THE __VA_ARGS__ macro with
* the ## token */
#define DEBUGPRINT(fmt, ...) \
do { if (1){ fprintf(stdout, "debug:%s:%04d:%s: " fmt, __FILE__, \
__LINE__, __func__,## __VA_ARGS__);} } while (0)
/* THIS TOKEN ELIMINATES THE COMMA ------------^^ */
int main(int argc, char *argv[])
{
DEBUGPRINT("nums: %04i, %04i\n", 0x1234,0x5678);
DEBUGPRINT("How to have no arguments?\n");
return(0);
}
恐怕它没有包含在标准中,但它是 CLANG 和 GCC 编译器共享的扩展。
这是我系统上的输出:
$ a.out
debug:pru3225.c:0012:main: nums: 4660, 22136
debug:pru3225.c:0013:main: How to have no arguments?
$ _