- A value is typically numeric,
although it doesn't have to be.
In your code you can specify values such as three,
five or even 100,000,000.
The printf function can be used to display
those values providing you know about something
called a place holder.
In this movie, I continue exploration
of the printf function.
I show you how place holders can be used
to display values, not only numbers
but strings and individual characters as well.
To view values in action,
you can use the printf function
but you have to take that function up a notch.
The printf statement at line five generates outputs
that would have you believe the number 27
was a value but it's not, it's part of a string,
just the characters two and seven.
To specify a value, you need to do two things.
First, you must place a value into the code,
in this case, the number 27.
Second, you must direct the printf function
to display that value.
Edit line five to replace the characters
two and seven with %d.
In some editors, you may see the %d highlighted,
that's because it's a special symbol
for the printf function.
It's a place holder.
In this case, a place holder for an integer value,
a whole number.
Place a comma after the string
and add a second argument to the printf function,
the value 27.
Note how the value is shown in its own color.
The printf function now has two arguments.
The first is called the formatting string.
It contains one place holder, %d.
Matching that place holder is the second argument,
the integer value 27.
Save, Build and run.
In this output the 27 represents the value 27,
it's output is text, of course, but inside the program
it's a number and not text.
Copy line five and paste it in as line six.
Change the value to 29.
Save, Build and run.
Two lines are output, the same place holder is used
in both printf functions
but because a different value is specified,
that new value is output for the second statement.
The %d place holder is just that,
it holds the place of another argument.
In fact, you can specify multiple place holders
providing that you have arguments to match.
Type a new line seven that reads,
printf ("and here are the values %d, %d, and %d\n",
51,52,53); remember the semi-colon to end the statement.
Save this modification, Build and run.
You can even do math in a printf function
although it's the C language that does the math
not the printf function.
Open exercise file, value two.
Here you see an immediate value
specified in the printf statement
but math isn't always going to be that simple.
Replace the four with the equation two plus two,
Save, Build and run.
The program makes the calculation for you,
it adds two and two, the result is fetched
from the %d place holder in the printf statement.
Now replace the equation in line five with this: 278*956
On a computer the asterisk is used
as the multiplication operator.
I'll officially introduce that as well as
other math concepts in a later movie.
Change the format string as well,
replacing two plus two with 278 times 956
and who knows what that value is?
Well, Save, Build and run,
and apparently the result is 265,768.
Neato.
The printf statement employs a swath of place holders,
each for a specific type of value.
Here is a partial list showing some of the more
common place holders, it helps to know
about the different types of values
in the C language before you can use each of these
as well as understand the whole lot of them
that aren't shown.
Also, even then the place holders have options
that I'll get into in a later movie.
The %s place holder is used to display strings
and it may look a little odd but it does come in handy.
Open Exercise File value3.c
In this code, the %s place holder
represents the string "programmer",
which is the second argument of the printf function.
Build and run this code
and indeed, you are a programmer
but what kind of programmer?
Modify line five in the code to add another place holder
and another argument to the printf function.
Place %c before %s and squeeze a space
in between the two.
Now you have to add an argument for the %c
so after the comma, type a single quote,
big C and then another single quote and a comma.
Many editors will assign the single character value
to its own color, as shown here.
In the C language, single characters
are specified with single quotes.
If you use double quotes, you create
a one character string,
that's not what you need to match the %c
place holder, Save, Build and run
and indeed you are a C programmer.
One final thing, because the percent character
is used as a place holder, you must specify
two of them when you use a percent symbol
in the printf statement's formatting string.
Open Exercise File, value 4.
Read the printf statement,
the %d place holder should match up
with the second argument, 98.
It's followed by a percent sign
which indicates percent, not a place holder, right?
Build this code.
Now if you don't see any warnings, that's okay,
the output is still going to look weird
and some compilers you will see warnings indicating trouble.
The compiler believes that a second place holder
is desired and it doesn't understand the place holder
as presented, plus it can't find a matching argument.
If you don't see the warnings,
try to run the code and see what happens.
Weird, huh?
The solution is to edit line five
and stick in a second percent sign.
That looks odd and it's cryptic,
it's the cryptic side of the C language in fact
that enthralls so many of the nerds
but it's going to work.
Save the changes, Build and run,
and now the output looks more sensible
and I hope you do get 98 percent or more
on your next C exam.