Talk more about what printf does and we’re gonna look at how we can use.
Print out things like variables and also things like different data like numbers and strings and all that stuff so in order to use.
Printf we just type out PR int F and.
We’re gonna make an open and closed parenthesis and a semicolon now printf is what’s called a function and basically what that means is it performs a specific task or a specific function in our case printf just print something.
Out onto the screen write the task that it’s doing is printing something out and it basically works how you’d expect it to work so if I came in here I could print out a specific piece of information and generally when we’re using printf we’re gonna be printing out text so I can make an open and closed quotation marks and I could print out like hello world in here and now this will print out onto the screen.
Hello and inside of printf I can use all sorts of special characters so if I wanted to create a new line for example I can make a backslash N and down here I could type world and now this will print out a new.
Line so you’ll see hello world on different lines we can also use these special characters so for example I could print out like a quotation mark so if I just printed a normal quotation mark right here you’ll notice that it’s messing this up so it’s sort of like counting this as the entire string if I wanted to change that I can just put a backslash here and.
Now this will literally render a quotation marks when I run my program you can see we have a quotation mark there so that’s kind of like the basic usage right you can specify some text that you want to print out onto the screen and it’ll do it but this printf function is actually extremely powerful and we can.
Use it to print out different pieces of information so in addition to just printing out plain text inside of these quotation marks I could also print out other types of information for example I could print out and if I wanted to print out a number I would have to use something called a format specifier a.
Format specifier basically tells this printf function that we want to print out.
A type of data that’s not just plain text like this so if I said % and then I said D this would actually allow me to print out a number so generally when we use these format specifiers uses % and that tells see that you’re going to want to print out something special and then after that we would.
Type in a specific letter and that’ll tell see what exactly we want to print out so if I said % D this means that we want to print out an integer so all I have to do now is I can make a comma and over here I can specify the integer that I want to print out so I could say like 500 and now.
This is gonna print out 500 to the screen so you can see over.
Here we’re getting 500 and this is cool because we can interweave these different types of data.
With text so I could say like my favorite number is % D and this acts as a placeholder for this number over here so when I run my program now it says my favorite number is 500 and that’s really cool we can also use multiple format.
Specifiers so in addition to just using this percent d i can also specify another one so over here I could say percent ass and you’ll notice that this is structured similarly to percent d % s is basically going to allow us to include some text so.
I can include some more text just like.
This so if I said % s I can make another comma here and I could say number and now.
This is gonna print out my favorite percent s so it’s gonna look for this.
String of text over here it’s gonna insert that in and it’s gonna say 500 so now when we run this it’ll say my.
Favorite number is 500 and you’ll notice here that I’m using multiple commas so I’m saying like % asks % d and over here I have a comma and I’m specifying this string that I want to print so the string of characters and then the integer and so basically the order that you put these format specifiers inside of your string of text is the order that you need to include them using these commas so you’re going to separate each of them using a comma.
Also use a bunch of other ones and I’m going to talk to you guys about some of the most common % D will print out an integer like 500 but if.
We wanted to print out a decimal number for example a double we could say percent F and this is gonna allow us to print out a decimal number so I could say like 500 point nine eight seven five four and now this is gonna print this out on to the screen actually I need to build this so.
My favorite number is five hundred point nine eight seven five four zero so essentially you can use these print FS and you can use these format specifiers in order to include different data types into your output so like this is just some general text output and we.
Can use these different guys to interweave like numbers or you know other strings and stuff like that and what are these really become useful is when we have variables so if I was to create like a number variable up here I can call it like fav num set it equal to 90 I can use this variable and print it out.
Inside of this printf so over here we could make this percent D and instead of printing out this number we can just access that variable so I could say fav num.
And this will allow me to print out that variable using that printf function so my favorite number is 90 so that’s the basics of using printf this is a very awesome tool that we can use and printf is basically just used for us to find out.
Information so when we’re running our programs a lot of times we’re gonna want to be able to get some information about what’s going.
On about what they’re doing in printf can allow us to do that so don’t be afraid to use these different access modifiers like I said % s % d.
% F those are all sort of the common ones and you can also use % C and this will allow you.
To print out a single character so I.
Can come over here create like a character variable we’ll call it my char and just set it equal to like lower case I and I could print this out using % C so now we could say my char and you’ll see that we print so play around with printf it’s extremely useful and it’s really.