I'm currently encountering this problem. I wanted to print out the # as I defined in the code block below, thing is when I pass the printf argument as printf("%*s\n", x, BORDER), it prints out all the # I defined at the beginning. However, when I write it as printf("%.*s\n", x, BORDER) then it prints out just as many # as I wanted. Can someone tell me what's the difference that triggered this problem? I know width and precision stand an important role when it comes to float number printout, but this is string print out...
#include <stdio.h>
#include <string.h>
#define BORDER "############################################"
int main(void) {
char word[26];
int x;
scanf("%25s", word);
x = strlen(word) + 2;
printf("x = %d\n", x);
printf("%*s\n", x, BORDER);
printf("#%s#\n", word);
printf("%*s\n", x, BORDER);
return 0;
}
printf, and read it carefully. When usings, "Precision specifies the maximum number of bytes to be written."