I tried the search function, but only found questions regarding reading in comma-delimited / space-separated files.
My question, however, is how you usually approach this. Say I have a list of / array / ... values, for example {1, 2, 3, 4}, and you want to print them using a separator.
The simplest version will look something like this:
#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 with this is the comma and the space at the end.
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;
}
This does not seem to be the best way to do this. Can someone point me in the right direction? Thanks
PS: No, usually there are no hard code values.
PPS: No, I'm not trying to write .csv files;)