You are viewing a single comment's thread. Return to all comments →
What is a formatter? and why do we have 3 formatters but only 2 variables (i.e s1 , x)?
The formatter is a placeholder.
If you imagine a coaster (as in the little bit of card where you put your cup when you're drinking something...) That's comparable to a formatter, in the sense that it shows the place you should put something.
There are only two formatters here:
The one that you think is the third: %n actually represents a linebreak (like hitting Return on your keyboard while typing a sentence).
%s = String // a string goes here
%d = int (digit) // an int goes here
%f = float // a float goes here
Let's look at the whole statement:
("%-15s%03d%n", s1, x)
The two formatters are:
%-15s which is where the variable s1 (a String) is placed.
%03d which is where the variable x (an int) is placed.
The other parts of those formatters mean different things...
The -15 part means that:
-: when s1 is put into this spot, keep it to the left side.
15: make sure it takes up 15 spaces.
if s1 = hi then it will produce:
s1 = hi
"hi " // Note that the string is 15 characters long
The 03 part means that when x is put into this spot:
3: it must take up three spaces.
0: any spaces it doesn't use are to be filled with 0's.
N.B. The default padder is whitespace " " if 0 is not used.
if x = 70 then it will produce:
x = 70
"070" // The result is 3 characters long, with a '0' filling the unused space
Time to watch some tutorial videos I guess! Good luck.
superb expelnation man
clear explanation :)
thank you for this awesome explanation
but how the spaces between string and integer are denoted in program
Did you get an answer? I had the same question about the space in the centre
Can you tell me please, where i can find some serious TuT Videos?
I would love to learn this kind of things. Between, thanks for the clear explanation!
Awesome explanation Bro!!
you cleared my doubt, thank you
Thank you for the clear explanation!
good explanations learned something new
what is the use of % ?
Default padder " " will always be in the middle? Keeping the string and int on the ends.
good explination bro
thank you superb explanation
perfect explanation !
amazing explanation. thanks a lot !
Thanks for your explaination. It really helped. It was clear and the best part was that you expained it by breaking it in parts... Every letter in that segment.
Best explanation on this thread, thanks a lot.