Hello, and welcome back.
In this lecture, I want to talk about format specifiers.
So this is in the section for variables and data types.
And the reason is it's very important to be able to display
the value of variables as output.
And format specifiers basically help you do that.
So I want to run through some examples of showing how you can display the
value of variables and we're going to use format specifiers to specify the
type of data that we want to display.
So format specifiers are used when displaying variables as output.
You can also use them for converting data, but we're just going to talk
about using this output for now.
They specify the type of the data of the variable that you want to display.
So for example, if you declare an integer named sum and it's equal
to 89 and you want to display that variable sum, what you would do is
you would use the printf function.
And it looks kind of weird right.
The thing that you should really notice in this printf is
there's a percent symbol there.
And the way that you're going to be able to identify a format specifier
is through that percent symbol.
So format specifiers are exactly what the name implies in that
they specify the format of data and basically how something
should be formatted or displayed.
So in this example, printf is a function that we've
been using to display output.
We know how to do this because we had many different
examples and a challenge.
Printf is a function.
A function again we haven't talked about functions in detail, but a
general concept of a function just means that it's a group of code.
It's a way of organizing things like an algorithm
could be inside of a function.
Function has specific syntax in c in that you see parentheses with it.
So we know the main function is the way that you enter a program.
So we're familiar with at least one function called the main.
We've also been learning about printf because that's one of
the functions that you use to display output to the console.
So it's important to understand those two basic functions.
We don't have to understand everything about functions up to this point.
We just need to know that a function can be invoked
or called to do something.
And in this case, we're using the printf function to display output, and
we're telling it what to display based on what's inside these parentheses.
So printf is a function, it can display the output of variables.
Up to this point, if we wanted to display something using printf, we
just enclosed it in double quotes and we saw text go out to the screen.
But what happens when you want to print out a variable.
Well, when you want to print out a variable, you have to specify
the type of the data for that variable that you want to print.
The printf function needs that information.
So if you look at printf and you're trying to analyze it you don't
know anything about functions yet, things that should stick out
to you are that hey it has left parentheses and right parentheses.
There's a comma in there.
There's quotations in there.
First of all, you're going to know its function because of the parentheses.
And inside those parentheses are what what's called function arguments.
It's data that you're passing to the function that can be used
and again it's not real important that you understand all of this
but it is arguments and the arguments are separated by comma.
So the first argument for the printf function is everything
inside those double quotes.
The second argument would be that variable sum.
And so the first argument there represents what you want
to be displayed as output, everything in the double quotes.
And up to this point, we were able to display text.
But that percent d symbol and that backslash n, we know the
backslash n is an escape sequence.
So if we try to print that out, that's just going to print out a line feed.
But what does the percent d do.
Well, the percent d is basically saying the when you
see the percent symbol, don't actually print out percent.
Print out what's the next argument.
So percent d is going to actually be interpolated or
mapped to the variable sum.
So along with the display of the character string the sum is, right
after displaying the sum is percent d is going to be mapped to the variable
that you want to display as well.
And then backslash n is going to be interpreted as a line feed.
So this is going to display the sum is the value of sum and then a line feed.
Now the percent symbol tells you that it's a format specifier.
The d is telling you that it's an integer.
And depending on the type of that data, what's after the percent
symbol is going to change.
So that's what's going on with the printf function.
The percent character is what's important in terms
of the format specifier.
It's a special character recognized by the printf function.
And what immediately follows that percent symbol is going
to identify the type of the data that we want to print.
The type of the data that we want to print is just going to be a
single character like this is d.
The data that we're going to actually print is after the comma.
And so you can put many different percent symbols in there to print
many different types of data and we'll go through some examples.
So here's an example where we're creating some
variables of different types.
We're creating an integer variable, a float variable, double
variable and a character variable.
And we're assigning all these different data values to them.
We then want to display these actual variables values.
And so what you should see here in these printf examples
are after the percent symbol there's different letters.
We have percent I, percent f, percent e, percent g and percent c.
Again, those letters identify data types.
Percent i is going to represent an integer.
Percent d also represents an integer.
Percent f represents a flow.
Percent e is actually going to represent a double.
Percent g is going to represent a double as well,
just a different format.
Percent c is going to represent a character variable.
And then Boolean values, remember, we mentioned that Boolean
values are true or false 0, 1.
So you could use a percent i there as well.
So let's go through this example in code blocks.
So here's our familiar example, our main function, our main program.
And I'm just going to copy and paste in the values that
we had for our little program that had different data types.
So this is the example from the slide.
Again, I have pretty big font here, so hopefully everyone can see it.
All we're doing in this example is we're declaring some variables
of different data types.
So in line 12, we're declaring a variable of type int named
integer variable, integer var, and we're assigning it 100.
So we're initializing and declaring variables.
We have an int, on line 13, we have a flow, line 14, we have a double,
line 15, we have a character.
We know that it's a character because it's inside a single quote.
And then our Boolean which we can just use underscore bool again
is what was defined in the C99 standard, but we can just use bool.
I think that that would be better.
And then we can set that to 0.
The printf function that's displaying the output.
And the first argument inside the double quotes is going to be
what's displayed to the screen.
The second argument is going to be mapped to what's the
format specifier, percent i.
And then the backslash n is just printing new lines because
that's an escape sequence.
So if I were to run this code, first I need to compile and
let's see it looks like we have an extra character somewhere.
When you're in code blocks, one of the ways that you can identify
errors is this color coding.
It will tell you.
So if you see things that are highlighted it's usually
a good indication that something weird is going on.
So now we save it, we build it, we do have unnamed type bool.
So we're just going to use underscore pool for now
and we still have some bad characters here and we know that because
the printf is actually not...
again i should have copied and pasted this i copied and
pasted this from the micro from the PowerPoint slides and it basically
added all these weird characters.
So now I have to make sure they're not there.
And now we have no errors not sure why I wasn't able to
use bool
because it's a c++.
So bool is actually for c not defined as an alias,
I think, until the c11 standard.
Well, you have to use underscore bool.
So let's run this thing.
And what we'll notice is output is it's going to be displaying the
output, so you'll notice again that it's displaying integer values.
So it displays what's ever in the quotes and then percent I gets
mapped to whatever the value is of integer var, which is 100.
So percent f is going to get mapped to floating var and vice versa.
So what you see output is integer var equals 100, floating var
equals 331.79 and so forth, double var and double bar.
And you'll notice the format's a little different depending
on the specifier you provide.
You also notice bool var is equal to 0 because bool is actually an integer.
Other things that you should notice is in line the floating var is
actually printed out as 331.79009.
But we assigned it 331.79, and the reason it's displayed with more
precision is because of the specifier f and depending on what machine you're
in because it's machine independent it may display differently.
And so this is just an example of how you can use format specifiers.
And also, if you wanted to print more than one variable on the
same line so we could actually put it else that we want in here
anything else inside in here.
And then we could do another thing percent d or let's percent
let's print out the floating var.
We could do percent f and then all we need to do if we want
to print out two variables.
We had another comma and we say floating var.
So you see what I did here is I wanted to print out two variables on
one statement, one print statement.
So I added another format specifier and then I just added
another comma to specify the variable that I want to print.
So you can put as many format specifiers in
between the double quotes.
And those are going to get mapped to whatever after the
commas for each variable.
Again, I wanted to talk about format specifiers because we're going to
be printing out data for debugging.
It's important to know how to print out variables.
And there are all sorts of different format specifiers
in the c programming language.
I want to provide you with a little illustration of some of
the ones you're basically going to use depending on the data type.
But format specifiers can get any more complicated.
You could put a percent here and say 2 dot and this will
print it to two decimal points.
There's all sorts of nice things you can do with format
specifiers to format your data.
But we're just going to talk about the basics, basically how you use the
format specifier specify the type of data that's going to be outputted and
then put the variable with the commas.
So you can play around with this.
Again, we'll have an activity or a little challenge
to help understand this.
But this program just demonstrated some declaration
initialization of variables and then using format specifiers to display them.
So let's go back to the slides and summarize some of these
different format specifiers that are available in c.
Let's look at one more example related to format specifiers
which is the with specifier.
So the width specifier is where you have a number after the percent
symbol that specifies the minimum field width to be printed if the
characters are less than the size of the width that remain in the space.
it so it really just has to do with precision.
So this will make more sense if I show you an actual example.
So this has to do with really floating point numbers and whole numbers.
So let's say we have a flow called x and this is going
to be equal to 3.99983 or bunch of different decimals.
We can print the width of this by specifying how far
or how precise the number is.
So we can do is we can say printf.
And if we want to print percent x, we usually use something
like percent f and then we want to print out our float.
But what we want to do instead is we want to specify.
So it would look like something like this.
As opposed to percent f, you can actually say percent dot and then
you can say something like dot 2.
And what this is going to do it's only going to print out 3.99 because the
dot and then the number specifies the width of the floating point number.
So let's go ahead and build this and then when we run it, we can
say and we want to make sure this is x, we want to make sure that it
only prints out to two precision.
So when we run this you'll see that it just prints out 4.00, so it rounds it.
But again percent two is the specifier.
Now you can change this.
If you don't want to use 2, you can say 5 and that will print up
everything and it will round it.
So let's actually do 9.3 and it will print out 5 precision.
So now when we run this, you'll see that it prints out 3.93232.
So there's five decimal points.
And so you can use the width operator for how you want to provide it.
So in this summary slide, it actually tells you the format specifiers
that you would use for printf in the third column here, print f charts.
So if you want to print out a character, use percent c.
If you want to print out a bool, use percent i or percent u and
that will print out 0 or 1.
In the const, there's also examples of what represents these data types.
Short int would be percent hi, percent hx and so forth.
The ones that you're probably going to use a lot int for percent i or percent
d, percent d works with int is usually used depending on the standard.
Percent f, you get a u associated with that for floats, doubles,
percent f, percent e, percent g.
Each one is going to determine the precision.
Long double percent lf, another real popular one is percent s for
strings, but we haven't talked about strings yet, which are
just a sequence of characters.
But again, all these different format specifiers can be included to
convert data as well as displaying data as output, and we'll be doing
a lot of this in many examples.
Thank you.