Printing numbers¶
You have already seen puts
, from the stdio
library.
The most general-purpose output routine in C, also in stdio
,
is printf
(‘print formatted’). You can use printf
to
put a string on the output.
printf("hello, world");
The only difference between that and the same thing with puts
is
that puts
starts a new line after the message, but printf
does not, so anything that gets printed later (including your command
prompt when the program ends) is still on the same line. If you want to
start a new line, you must include the newline character in the message
explicitly. To avoid having to type a literal newline in your source
code, the language provides instead a backslash-escaped special value,
\n
, meaning newline.
printf("hello, world\n");
As long as this is all you are doing, it probably makes more sense
to use puts
. However, the argument to printf
is more
than just a message to be printed: it is a ‘format string’, which
can contain blanks (like a mad lib) to be filled with with values from
your program. Most characters, like those in "hello, world\n"
,
stand for themselves. However, the %
sign begins a ‘conversion
specification’. There is a whole sub-language describing what can be
filled in and how, but to start out, %d
means a conversion of
an int
to decimal (d
for ‘decimal’). The value to
be filled in/converted is given as the next argument to printf
,
as shown in this more user-friendly program to calculate \(2+2\).
#include <stdio.h>
int main()
{
int x = 2 + 2;
printf("The number %d is your answer!\n", x);
}
The %d
conversion specification will be applied to the value
x
, and the printed output will say ‘The number 4 is your
answer!’.
We can add output to the factorial function similarly.
#include <stdio.h>
int main()
{
int n = 1;
n = n * 2;
n = n * 3;
n = n * 4;
n = n * 5;
printf("5! = %d\n", n);
}
Try printing out the value of n
in other places, to view
intermediate calculations. This sort of debugging print statement is a
common debugging technique, in addition to inspection in the interactive
debugger.
For most types, there is a relevant conversion specifier for
printf
. A double
can be printed using the f
specifier.
#include <stdio.h>
int main()
{
double celsius = 20.0;
double fahrenheit = celsius * 9.0 / 5.0 + 32.0;
printf("%f Celsius is %f Fahrenheit\n", celsius, fahrenheit);
}
This example shows not only how to print out a double
, but
also that there can be any number of conversion specifications in
a printf
format string. Here there are two. After the format
string, there must be as many further arguments as there are conversion
specifications, and they will be filled in in order.
To learn more about the very rich language of printf
, you can
read the manual page. One of the nice features of unix is its online
manual pages, accessed through the man command. The manual
is divided into sections, and section 3 is dedicated to C libraries
and functions. So, man 3 printf
will bring up the appropriate
manpage. Some functions have brief documentation, while printf
can be a bit overwhelming; just scan for useful information.