|
|
|
|
|
by Someone
2206 days ago
|
|
One use is aligning outputs: char *prefix = "example";
char *line1 = "line 1";
char *line2 = "line 2";
printf("%s: %n%s\n", prefix, &n, line1);
printf("%*s%s\n", n, "", line2);
will output example: line 1
line 2
That’s a bit more robust than using strlen(s)+2, where you have to keep that magic constant 2 in sync with ": ". Moving ": " to a variable and using strlen(s)+strlen(separator) would fix that, though (at the price of speed, unless you’ve a compiler that optimizes that away) |
|
strlen wouldn't even be an option if you were formatting something that's not a string e.g.