简体   繁体   English

确定printf参数的数量

[英]Determine number of printf arguments

Is there a standard C/C++ function that, given a printf format string, returns the number of arguments it expects? 在给定printf格式字符串的情况下,是否有标准的C / C ++函数返回期望的参数数量? Eg: 例如:

num_printf_args("%d %s") == 2;
num_printf_args("%.1f%%") == 1;
num_printf_args("%*d") == 2;

Just counting the number of % in the format string would be a first approximation, which works in the first example, but obviously not in the second and third ones. 仅计算格式字符串中的%数将是一个第一近似值,这在第一个示例中有效,但显然在第二个和第三个示例中无效。

I know gcc can do this, since at compile time it complains when the number of arguments (and also their type) actually passed to printf does not match the format string. 我知道gcc可以做到这一点,因为在编译时它会抱怨实际传递给printf的参数数量(以及它们的类型)与格式字符串不匹配。

No standard function to do this. 没有标准功能可以执行此操作。

However, it would be easy to implement. 但是,这很容易实现。

Count the number of % that are not followed by another % . 张数%未再接再% The add 1 for every one of those counted % immediately followed by a * . 计数的每个%的加1立即紧跟一个*

When counting runs of % characters, don't allow overlaps. 计算%字符的游程时,不允许重叠。 So "%%%d" should result in a value of 1 (not 0 , and certainly not 2 or 3 ). 因此,“ %%% d”的结果应为1 (而不是0 ,当然也不能是23 )。

EDIT - text below added following comment from user694733 编辑-下面的文本在user694733的评论之后添加

This will suffice for format strings along the lines of the three examples you gave. 按照您给出的三个示例,这足以满足格式字符串的要求。 However, as noted by user694733 in comments, this is not the whole story. 但是,正如user694733在评论中指出的,这不是全部。

In general terms, a format specifier follows the prototype %[flags][width][.precision][length]specifier . 一般而言,格式说明符遵循原型%[flags][width][.precision][length]specifier The approach above is a starting point, and will work for format strings have have no flags , a possible * in width specifier, no * in the precision specifier, and overlooks length and specifier fields. 上面的方法是起点,并且将用于格式字符串工作已经没有flags ,可能的*width说明符,没有*precision说明符,俯瞰lengthspecifier字段。 All of those need to be considered, for a general format string, and the string parsed accordingly. 对于一般格式的字符串,需要考虑所有这些因素,并相应地分析该字符串。 The effort to do that depends on how robust you need your count to be - for example, more effort if you need to detect an invalid format string. 要做的工作取决于您需要的计数稳健性-例如,如果需要检测无效的格式字符串,则需要做更多的工作。

I'm not sure why you need this, however your problem might be solved in this way (accepted answer from this question: Variadic macro trick ). 我不确定您为什么需要这样做 ,但是您的问题可能会以这种方式解决(此问题的答案: 可变参数宏技巧 )。

As suggested in comments, you can wrap the call of printf into a macro that will first count the number of arguments required and then proceed to whatever you want to do. 如注释中所建议,您可以将printf的调用包装到一个宏中,该宏将首先计算所需参数的数量,然后继续执行您想做的任何事情。

Code from blog post: 博客文章中的代码:

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL(__VA_ARGS__, 5,4,3,2,1)
#define VA_NUM_ARGS_IMPL(_1,_2,_3,_4,_5,N,...) N

// to verify, run the preprocessor alone (g++ -E):
VA_NUM_ARGS(x,y,z)

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

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