Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to printf a loop result?

Tags:

c

for-loop

printf

How to printf results from a loop?

For example if i have this something simple like this:

k[0]=2;
k[1]=3;
k[2]=4;

for (i = 0 ; i <= 2 ; i++)
{
    x[i]=5*k[i];
}

How do I print out the results for x[0],x[1],x[2] without having to keep repeating the array in printf? As in

printf("%d %d %d\n",x[0],x[1],x[2]);

I dont really want to do the printf above because for my problem i actally have an array of 100 values, i cant be repeating the x[0],x[1]... for one hundred times.

Hope someone can help out thanks loads!

like image 774
esther Avatar asked Jul 02 '26 05:07

esther


2 Answers

Maybe this:

for (j = 0; j < 100; j++) {
    printf ("%d ",x[j]);
}
printf ("\n");

You can put a printf in a loop. If you don't put a "\n" in the printf, the next printf will be on the same line. Then when you are done with the loop, printf just a newline.

There is no printf type specifier for an array, you have to loop through the array elements and print them one by one.

like image 21
Lou Franco Avatar answered Jul 03 '26 22:07

Lou Franco