I believe I have carefully read the entire printf()
documentation but could not find any way to have it print out, say, the elements of a 10-element array of float(s)
.
E.g., if I have
float[] foo = {1., 2., 3., ..., 10.};
Then I'd like to have a single statement such as
printf("what_do_I_put_here\n", foo);
Which would print out something along the lines of:
1. 2. 3. .... 10.
Is there a way to do that in vanilla C?
You have to loop through the array and printf()
each element:
for(int i=0;i<10;++i) {
printf("%.2f ", foo[i]);
}
printf("\n");
To be Honest All Are good but it will be easy if or more efficient if someone use n time numbers and show them in out put.so prefer this will be a good option. Do not predefined array variable let user define and show the result. Like this..
int main()
{
int i,j,n,t;
int arry[100];
scanf("%d",&n);
for (i=0;i<n;i++)
{ scanf("%d",&t);
arry[i]=t;
}
for(j=0;j<n;j++)
printf("%d",arry[j]);
return 0;
}
You need to go for a loop:
for (int i = 0; i < sizeof(foo) / sizeof(float); ++i)
printf("%f", foo[i]);
printf("\n");
C is not object oriented programming (OOP) language. So you can not use properties in OOP. Eg. There is no .length property in C. So you need to use loops for your task.
you can print it as string:
printf("%s\n", foo);
I don't think there is a way to print array for you in printf. "printf" function has no idea how long your array is.
Source: Stackoverflow.com