I tried the search function but only found questions regarding reading in comma/space delimited files.
我尝试了搜索功能,但是只找到了有关在逗号/空格分隔的文件中读取的问题。
My question is however, how do you usually approach this. Say I have a list/array/... of values, like {1, 2, 3, 4} and want to print them with a delimiter.
然而,我的问题是,你通常是如何做到这一点的。假设我有一个列表/数组/…的值,如{1,2,3,4},并希望使用分隔符打印它们。
The simplest version would be something like:
最简单的版本是:
#include <stdio.h>
int main(void)
{
char list[] = {1, 2, 3, 4};
unsigned int i;
for (i = 0; i < 4; ++i)
printf("%d, ", list[i]);
return 0;
}
which will obviously print "1, 2, 3, 4, ". The problem I have with that is the comma and space character at the end.
很明显,它会打印"1 2 3 4 "我的问题是最后的逗号和空格。
Now I could do:
现在我能做的:
#include <stdio.h>
int main(void)
{
char list[] = {1, 2, 3, 4};
unsigned int i;
for (i = 0; i < 4; ++i)
{
printf("%d", list[i]);
if (i < 3)
printf(", ");
}
return 0;
}
Bút that doesn't seem like the best way to do it. Can somebody point me into the right direction? Thanks
但这似乎不是最好的方法。谁能给我指出正确的方向吗?谢谢
PS: No, I don't usually hardcode values
PPS: No, I am not trying to write .csv files ;)
PS:不,我通常不会硬编码值PPS:不,我不是在写。csv文件;)
7 个解决方案
#1
4
I use this idiom:
我用这个成语:
assert(n > 0);
printf("%d", list[0]);
for (i = 1; i < n; ++i)
printf(", %d", list[i]);
Its one disadvantage is that it doesn't scale nicely for n == 0, like a simple loop. Alternatively, you can add protection against n == 0:
它的一个缺点是它不能很好地扩展到n = 0,就像一个简单的循环。另外,还可以添加n = 0的保护:
if (n > 0)
printf("%d", list[0]);
for (i = 1; i < n; ++i)
printf(", %d", list[i]);
#2
11
My standard technique for this is:
我对此的标准技术是:
const char *pad = "";
for (int i = 0; i < n; i++)
{
printf("%s%d", pad, list[i]);
pad = ", ";
}
Sometimes, the initial value of pad is a blank, or a colon blank, or whatever else works in context.
有时,pad的初始值是空的,或冒号空,或任何其他在上下文中工作的值。
#3
2
I picked up this format with the conditional operator from K&R2:
我用K&R2中的条件运算符找到了这个格式:
for (i = 0; i < n; i++)
printf("%d%s", list[i], i+1 < n ? ", " : "\n");
#4
1
Well even thought there is already an accepted answer, nobody has come with the obvious one to my taste:
尽管已经有了一个公认的答案,但没有人能给出我最喜欢的答案:
#include <stdio.h>
int main(void) {
unsigned list[] = {1, 2, 3, 4};
unsigned const n = 4;
if (n) for (unsigned i = 0; ; ++i) {
printf("%d", list[i]);
if (i >= n) break;
printf(", ");
}
printf("\n");
return 0;
}
#5
0
Use Michal Trybus's version or the reverse
使用米哈尔·特蕾布斯的版本或者相反的版本
for (i = 0; i < (n - 1); ++i)
{
printf("%d, ", list[i]);
}
printf("%d", list[n - 1]);
#6
0
for ( printf("%d",list[i=0]) ; i < n ; printf(", %d", list[++i]) ) ;
#7
0
Why not just another version while we're at it. Here's what I normally do
为什么不换一个版本呢?这是我通常做的
for (i=0;i<n;++i)
{
if (i) printf(", ");
printf("%d",list[i]);
}
#1
4
I use this idiom:
我用这个成语:
assert(n > 0);
printf("%d", list[0]);
for (i = 1; i < n; ++i)
printf(", %d", list[i]);
Its one disadvantage is that it doesn't scale nicely for n == 0, like a simple loop. Alternatively, you can add protection against n == 0:
它的一个缺点是它不能很好地扩展到n = 0,就像一个简单的循环。另外,还可以添加n = 0的保护:
if (n > 0)
printf("%d", list[0]);
for (i = 1; i < n; ++i)
printf(", %d", list[i]);
#2
11
My standard technique for this is:
我对此的标准技术是:
const char *pad = "";
for (int i = 0; i < n; i++)
{
printf("%s%d", pad, list[i]);
pad = ", ";
}
Sometimes, the initial value of pad is a blank, or a colon blank, or whatever else works in context.
有时,pad的初始值是空的,或冒号空,或任何其他在上下文中工作的值。
#3
2
I picked up this format with the conditional operator from K&R2:
我用K&R2中的条件运算符找到了这个格式:
for (i = 0; i < n; i++)
printf("%d%s", list[i], i+1 < n ? ", " : "\n");
#4
1
Well even thought there is already an accepted answer, nobody has come with the obvious one to my taste:
尽管已经有了一个公认的答案,但没有人能给出我最喜欢的答案:
#include <stdio.h>
int main(void) {
unsigned list[] = {1, 2, 3, 4};
unsigned const n = 4;
if (n) for (unsigned i = 0; ; ++i) {
printf("%d", list[i]);
if (i >= n) break;
printf(", ");
}
printf("\n");
return 0;
}
#5
0
Use Michal Trybus's version or the reverse
使用米哈尔·特蕾布斯的版本或者相反的版本
for (i = 0; i < (n - 1); ++i)
{
printf("%d, ", list[i]);
}
printf("%d", list[n - 1]);
#6
0
for ( printf("%d",list[i=0]) ; i < n ; printf(", %d", list[++i]) ) ;
#7
0
Why not just another version while we're at it. Here's what I normally do
为什么不换一个版本呢?这是我通常做的
for (i=0;i<n;++i)
{
if (i) printf(", ");
printf("%d",list[i]);
}