简体   繁体   English

宏的“格式参数太多”

[英]“too many arguments for format” with macro

I have a debug macro that I use for quick and dirty outputs.我有一个调试宏,用于快速和脏输出。 I've been trying to determine how to make code compile clean on gcc 5.4.我一直在尝试确定如何在 gcc 5.4 上使代码编译干净。 It doesn't give any issues on earlier version of gcc (4.x) or clang (11.0.3).它不会对早期版本的 gcc (4.x) 或 clang (11.0.3) 产生任何问题。 The error is this:错误是这样的:

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);

The code that I've been using to try to determine how to fix this is:我一直在尝试确定如何解决此问题的代码是:

#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);
}   

As one can see, if I have arguments, there's not problem.正如人们所看到的,如果我有争论,那就没有问题。 It's only if I have a message that's does not have arguments.只有当我有一条没有参数的消息时。 I guess that I could pass a "\\n" with '%s', but I was just curious if there was a way to handle the NULL.我想我可以用 '%s' 传递一个“\\n”,但我只是好奇是否有办法处理 NULL。

The fmt argument needs to be left out, and you'll need to split the fixed part of the printing from the arguments you pass in. 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);
}

Output:输出:

debug:x1.c:0016:main: nums: 4660, 22136
debug:x1.c:0017:main: How to have no arguments?

In order to have no arguments, you can do the following:为了没有参数,您可以执行以下操作:

#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);
}   

I'm afrid it is not included in the standard, but it is an extension shared by CLANG and GCC compilers.我很害怕它没有包含在标准中,但它是 CLANG 和 GCC 编译器共享的扩展。

This is the output on my system:这是我系统上的输出:

$ a.out
debug:pru3225.c:0012:main: nums: 4660, 22136
debug:pru3225.c:0013:main: How to have no arguments?
$ _

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM