//This is the example
#include <stdio.h>
#define SIZE 10
int main() {
int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
int i, j;
printf("%sss\n", "Element", "Value", "Histogram");//This part here is what is unlcear to me
for (i = 0; i < SIZE; i) {
printf( "}d ", i, n[ i ]);
for ( j = 1; j <= n[ i ]; j )
printf( "%c",'*' );
printf( "\n" );
}
return 0;
}
I am new to arrays and i've seen this example where it's suppoesd to create a histogram of stars and i've stumbled on that Printf and i have no clue why is it there and what are the numbers after the % do
CodePudding user response:
Those numbers are there to create spacing. If you try printf("%s%s%s\n", "Element", "Value", "Histogram")
instead, the output becomes `ElementValueHistogram. Basically the spacing just says that the second string should take up 13 characters worth of space, even if there aren't 13 characters. The way that C formats these strings, the empty space is at the beginning.
CodePudding user response:
The numbers next to %
determine the number of characters it can take.
So for example, printf("%s%s%s","Element","Value","Histogram")
would output "ElementValueHistogram"
While, printf("%sss","Element","Value","Histogram")
would output "Element Value Histogram"
So Value has 5 letters, with s
it would print "_____Value". Same with Histogram with 9 letters, with s
it would print "_Histogram". Where the underscores represent the spaces.
Normally, C would print empty characters
to fill in the spaces.