... why my output is messed up (?)
Code uses a tab '\t'
which only aligns to the next tab-stop - usual every 8 columns and the stars needed exceed 8.
With judicious use of the '-'
flag, field width and precision, code can be simplified to print an array of characters.
#include <stdio.h>
#include <string.h>
int main(void) {
int n = 10;
char stars[n];
memset(stars, '*', n);
for (int i = 0; i < n; i++) {
// +------------- - flag: pad on right
// |+------------ field width: min characters to print, pad with spaces
// || +--------+- precision: max numbers of characters of the array to print
printf("%-*.*s %.*s\n", n, i + 1, stars, n - i, stars);
// | ^^^^^ ^^^^^ precision
// +--------------------- field width
}
}
Output
* **********
** *********
*** ********
**** *******
***** ******
****** *****
******* ****
******** ***
********* **
********** *
Note: stars
is not a string as it lacks a null character. Use of a precision with "%s"
allows printf()
to use a simple character array. Characters from the array are written up to (but not including) a terminating null character.