sprintf的格式说明由什么代替 [英] sprintf format specifier replace by nothing
问题描述
我目前不知道是否有一种方法通过使用无sprintf的更换格式说明%U
I am currently wondering if there is a way to replace the format specifier %u by nothing using sprintf
我的问题是关于sprintf的使用三元运算符的这要去一个值或空字符替换%U
My question is about the use of a ternary operator in sprintf which gonna replace %u by a value or by nothing.
下面是什么,我试图做一个例子:
Here is an example of what I am trying to do :
int main (void)
{
char mytab[10]={'\0'};
uint_32 i=0;
scanf("%u",&i);
sprintf(mytab, "\"%u"\",i>0?i:/*Here is the syntax I want to find if it exists*/);
printf("%s\r\n",mytab);
return 0;
}
在code我想获得的结果是,例如1,如果输入为1(或2,如果输入是2 ......)和如果输入为0。
The result of the code I am trying to get is for example "1" if the input is 1 (or "2" if the input is 2...) and "" if the input is 0.
你对此有任何意见或explantion?由于提前通过
Do you have any ideas or explantion about it ? Thanks by advance.
推荐答案
我想一个简单的如果
语句仍然是最干净的选项:
I think a simple if
statement is still the cleanest option:
char mytab[10] = "\"\"";
if (n > 0) {
snprintf(mytab, sizeof mytab, "\"%" PRIu32 "\"", n);
}
这篇关于sprintf的格式说明由什么代替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!