- The C++ standard library provides functions
for formatted character output.
This is printf.cpp from chapter eight of the exercise files.
Printf is the most common function
for formatting text in C and C++.
And you notice down here on line 10,
I have a printf statement.
This printf statement has a number of arguments.
The first argument is this string, the C string,
and it includes format specifiers,
which serve as placeholders for the formatted output.
And then after these string is a very attic list
of arguments and the number of arguments should equal
the number of placeholders in the format string.
Printf writes to stdout.
So when I build a run, you'll see that this %d
is replaced with the number 5
which is the first argument, which is an integer.
So %d formats and integer.
%ld formats a long integer.
You notice that that argument is li and that is printed out
here in place of the ld.
And then I have a %s, for a C string.
And that is replaced with the value
from this C string here, the variable s.
We can also add other formatting specifiers,
for example, I can put a number 4 here,
and that will make this decimal value,
this integer value take up four spaces.
So where you see the 5 down there,
I'm going to go ahead and build and run.
You'll notice that 5 now it takes up four spaces.
It's right justified,
I can make it left justified by putting a - before it,
we build and run and now it's left justified,
or I can give it leading zeros by putting a 0 there.
And you'll notice that it now has three leading zeros
because the four, is the total size of the field.
There are of course, other format specifiers available.
In fact, there are many, many of them,
and you can find a lot of references
for the printf format specifiers on the internet.
But for now, here is a few of the most common.
You've seen d for integer number,
f is for floating point number, s is for a C string,
C is for a character, p is for a pointer.
And if you actually need the % character,
you can use two % signs and it's no longer a placeholder,
it's simply a % character.
So let's take a look at a couple of the other ones here.
I can say printf, %p, I can say pointer,
and I can also use zd, for size of numbers.
And I can say s and we'll get the address of s
rather than the string of s, and I can say size of s,
and that will give us the sizable pointer.
And so when I build and run,
now you see that the pointer is,
that's the address of the string
and the size of the pointer on this architecture
is eight bytes, which is 64 bits.
There's a separate version of printf called fprintf,
which takes as its first argument a file string.
So I can say stdout or I can give it a file handle.
And in this case with stdout, of course,
it'll go right to my console, but you can use this
for writing to files, writing format and output to files.
So the printf function provides a great number
of format specifiers for a vast array
of different circumstances.
We've seen here a very small subset of the most common
and useful specifiers, any good C or C++ reference
will have a more complete listing.