有没有一个“空”不打印任何东西的printf代码,用于跳过参数? [英] Is there a "null" printf code that doesn't print anything, used to skip a parameter?

查看:196
本文介绍了有没有一个“空”不打印任何东西的printf代码,用于跳过参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



pre $ const char * fmtDefault =%ux%s($%。2f each)\\\
;
const char * fmtMultiLine =数量:%3u \ nItem:%s \ nPrice per item:$%。2f\\\
\\\
;
const char * fmtCSV =%u,%s,%。2f\\\
;

const char * fmt;
switch(which_format){
case 1:fmt = fmtMultiLine;打破;
情况2:fmt = fmtCSV;打破;
默认:fmt = fmtDefault;
}

printf(fmt,quantity,item_description,price);

由于价格是最后指定的,我还可以添加一个不列出价格的价格: p>

  const char * fmtNoPrices =%ux%s \\\
;

但是如果我想省略数量呢?如果我这样做:

pre code $ const char * fmtNoQuantity =%s的价格是$%。 ;

然后发生未定义的行为(很可能是段错误),而不是我想要的。这是因为它将把第一个参数作为一个指向字符串的指针,即使它实际上是一个unsigned int。这个unsigned int很可能指向除有效字符串数据之外的东西,或者(更有可能的情况是,特别是如果你不购买数以亿计的同一个项目)一个无效的内存位置,导致分段错误。



我想知道的是如果有一个代码可以放在某个地方(在这个例子中是%Z )来告诉它跳过这个参数,像这样:

$ p codeonst char * fmtNoQuantity =%Z%s的价格是$%。 ;


解决方案

%s 值,有一个nullprintf()代码:%。0s



您可以通过以下方式获得一个通用的解决方案:

如果可能的话,重新排列,使得非%s 值为最后,然后在指定的格式字符串下。

我最喜欢的是有3个单独的printf()调用,每个值使用自己的格式。当不需要这个值的时候,只需要提供一个没有说明符的格式化字符串即可。

  const char * Format1q =; 
const char * Format1id =%s的价格;
const char * Format1p =是$%。2f each.\\\
;
...
printf(Format1q,quantity);
printf(Format1id,item_description);
printf(Format1p,price);

奇怪的解决方案:

你可以尝试使用%。0s 的Undefined Behavior。 (在gcc 4.5.3中使用过一些示例,谁知道其他编译器或未来)。

对于与指针相同大小的N x大小可以尝试使用%。0s N次的未定义行为。 (使用gcc 4.5.3中的一些示例,谁知道其他编译器或未来)。

If I want a program to have multiple text output formats, I could do something like this:

const char *fmtDefault = "%u x %s ($%.2f each)\n";
const char *fmtMultiLine = "Qty: %3u\nItem: %s\nPrice per item: $%.2f\n\n";
const char *fmtCSV = "%u,%s,%.2f\n";

const char *fmt;
switch (which_format) {
    case 1: fmt = fmtMultiLine; break;
    case 2: fmt = fmtCSV; break;
    default: fmt = fmtDefault;
}

printf(fmt, quantity, item_description, price);

Since the price is specified last, I could also add one that doesn't list prices:

const char *fmtNoPrices = "%u x %s\n";

But what if I want to omit the quantity instead? If I did this:

const char *fmtNoQuantity = "The price of %s is $%.2f each.\n";

then undefined behavior (most likely a segfault) will occur rather than what I want. This is because it will treat the first parameter as a pointer to a string, even though it's actually an unsigned int. This unsigned int will most likely point to something other than valid string data, or (much more likely, especially if you're not buying hundreds of millions of the same item) an invalid memory location, resulting in a segmentation fault.

What I want to know is if there's a code I can put somewhere (%Z in this example) to tell it to skip that parameter, like this:

const char *fmtNoQuantity = "%ZThe price of %s is $%.2f each.";

解决方案

For %s values, there is a "null" printf() code: %.0s.

You could reach a general solution via:

When possible, re-arrange so that non-%s values are last, and then under specify the format string.

My favorite for you is to have 3 separate printf() calls, one for each value using its own format. When the value is not needed, simply supply a format string with no specifiers.

const char * Format1q   = "";
const char * Format1id  = "The price of %s";
const char * Format1p   = " is $%.2f each.\n";
...
printf(Format1q,  quantity); 
printf(Format1id, item_description);
printf(Format1p,  price);

Weird solutions:

For other values that are the same size you could attempt the Undefined Behavior of also using %.0s. (worked with some samples in gcc 4.5.3, who knows in other compilers or the future.)

For other values that are the N x the same size as a pointer size you could attempt the Undefined Behavior of also using %.0s N times. (worked with some samples in gcc 4.5.3, who knows in other compilers or the future.)

这篇关于有没有一个“空”不打印任何东西的printf代码,用于跳过参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆