Can someone explain what does %-25s and %-90s do in the following code:
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
printf ("%-25s %-90s","hello" ,"world");
}
Can someone explain what does %-25s and %-90s do in the following code:
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
printf ("%-25s %-90s","hello" ,"world");
}
From the reference for std::printf, the -
specifier after %
is an optional flag that left-justifies the string to be printed:
-
: the result of the conversion is left-justified within the field (by default it is right-justified)
So your program will write out "hello" followed by 20 whitespace characters, and then "world" followed by 85 whitespace characters.
-25
means left justify text in 25 blocks (character spaces). and %-90
means same just in 90 blocks. So for this:
printf ("|%-25s|%-15%|","hello", "world");
output would be:
|Hello |world |
// ^ 20 spaces ^ 10 spaces
or this
printf ("|%-25s|%-15%|","hello", "world!!!");
outputs this:
|Hello |world!!! |
// ^ 20 spaces ^ 7 spaces
If you omit -
printf ("|%25s|%15%|","hello", "world!!!");
you would get this:
| Hello| world!!!|
// ^ 20 spaces ^ 7 spaces
I hope this helps, printing 90 spaces would be ridiculous.
@cigien beat me but go to c++ reference page.
Left-justify (-) a null-terminated string (s) parameter (%) with 25 (25) elements length/padding (hello)
Left-justify (-) a null-terminated string (s) parameter (%) with 90 (90) elements length/padding (world)
For more information you may see also: https://www.cplusplus.com/reference/cstdio/printf/