Transcript for:
Understanding Formatted Output with printf

in this lecture we'll be looking at formatted output by printf so far we have seen that we can print stuff out using the see out directive or the stream but it is also possible to print formatted output using print which is coming from C standard iio Library C STD iio the syntax of printf is printf you've got the format string at the beginning and depending on the number of format specifiers that you have within the format string you'll have as many parameters that come afterwards remember printf is coming from c not C++ so you have to include C sdio at the top of your program if you want to use printf in your programs the reason I'm showing you printf is this style of printing has become uh a standard for many programming language including Java and cop so as you take Java into 12 you would be using printab in there and it's a good you know uh idea to learn it in C++ in your introductory programming language course let's look at an example we've got two variables one integer one flood and two printf statements the first printf statement simply prints a string literal just like uh you would do with SE so the corresponding version of this statement in with C out would be C out whats up but the second one is different where we've got a format string that has two format specifiers so this percent D is called a format specifier and the second one is this one the percent. 3F is called another format specifier so what happens is when printf processes the format string it's going to look for this percent uh special format specifiers percent B percent F percent c as soon as it sees one a percent D it's going to correspond to the first parameter which is X basically what we are saying in here is when you instead of you know printing percent D on the screen take the first parameter which is X and print it in this place so in in the place of percent D we'll be printing an X and in the place of the second format fire which is percent. 3F we would be printing the second parameter which is y so the number so as you you have to have as many parameters in this thing as the number of format specifiers within the format string so let me show you uh this example the example would be formatted iio this is five format to a go there open it using code and it is the first example so you can see that the first example is doing what I showed you on the slide so I've got two variables and two print of statements again let me go step by step in the debug mode so that we can see what's coming up um out of these print statements on the screen so let me you know close this up and go to the terminal and let me execute the first print statement so here it is as you can see we are printing the message whats up because we have the new line character at the end of the sit string literal after printing whats up the cursor is now uh at the beginning of the next line so now we are on to the second printf statement again um think about how this is going to print so it's going to print this string uh format string and look for format specifier so percent is a format specifier that means the printf statement is going to look for the first parameter and take the Val value of this first parameter and insert it in here in as an integer so our integer value is 45 so it should be X column 45 comma then the second format specifier says print the second parameter as a floating Point number that's what this F is all about and um after you know with two three Precision after dot so let me execute this and so that you can see here it is um as you can see x45 is printed so the first format specifier takes the value of x and prints it and the second format specifier takes the value of y and prints it in this in its place so we see 56.74 6 on the screen so that's how printf Works um again when we execute the next printf statement it just has a format string without any format specifiers we basically printing this message on the screen with a new line so let me do that as you can see that L string Lal came up and the cursor has moved to the beginning of the next line as we mentioned the format string in printf may have some format specifiers inside um and every format specifier must start with the percent character followed by a specifier that specifies the type of object that we going to print and optionally it may have some Flags the width and the Precision by default all output of the print of statement is Right aligned but you could make them left aligned by putting a minus sign in the flax part as we will see here are some of the format specifiers for printf percent C is used to print a character percent D or percent I is used to print a signed decimal integer percent lldd is used to print a long long integer an 8 by decimal Al integer uh percent X is used to print the number in base 16 as a heximal number percent f is used to print a floating point or double number meaning a floating Point number a real number in general per and percent s is used to print a C string uh and if you want to print the percent character then you have you kind of Escape it by saying percent percent that just prints one percent sign on the screen let's now continue running our example and see what comes out of every PR of statement so we've got a character a CH is equal to a and integer flag is equal to one so let me initialize them and then um let's see when I print this character using percent C what comes out obviously because we are using percent C it should print a on the screen so let me do that and you can see character a so it's taking the format specific 5% C and replacing it with the value of CH which is an A in here I'm printing flag and not flag so basically flag would be because it's one it should be one and not flag is going to be zero don't worry about this not this an operator um we'll see later on I just want to show you the values that come out so percent is going to print an integer because this is a flag it should be printing um you know one and then not of it would be zero so let's continue uh we've got now integers so here's an integer I which with the value of 1 2 3 4 uh here I am specifying that I would like to print this value of i as a decimal number and this 10 specifies the width amount of space that I would like um the printf statement to to allocate for this uh for this format specifier so it's going to allocate 10 spaces and then print the value of I and has a right aligned value uh to do right so it's a right aligned value so let me show that here you can see this is you've got 10 spaces in here right um and 1 2 3 4 has been printed right aligned within this allocated space this time I am using percent X to print the value of i as an heximal number you know if you if you if you don't know anything about exod decimal numbers don't worry about it it's just you know another way to represent uh num num s uh the next one is I am saying okay put you know allocate 10 uh spaces for for the printf but um prefill it with zero characters at the beginning so this zero at the beginning says that uh you know fill up the the the space that comes before the integer with zero so that's what you're going to see so the difference between the first one and this one is in the first one we did not specify filling characters in here I specified zero as the prefill character so 1 2 3 4 was the value of I and zeros were the prefill characters with which I specified in here and in the last one actually this one I would be saying that print this integer left aligned that's what this minus means so let's do that as you can see now this 1 2 3 4 is left aligned within this 10 uh spaces that that we allocate and finally I am allocate you know I'm printing a long long integer that's why I have to say lldd uh and there it is all right so let's now continue and print some floating Point numbers here we have we've got a floating Point number double f with the value of 23 Point whatever so here what I'm saying is print this floating Point number and only print two precisions after that so it will be 23 point and two digits after uh the the U separator dot so in the fractional part we'll be only seeing two two digits two um yeah two digits so 2346 so it converts this into 46 and prints it I can only specify the amount of space that I would like this print printer statement to allocate for this specifier so 10 is the amount of space that it allocates and I would like only two um digit Precision after Dot and I would like it to fill uh use zero as the fill out fill character just like I did with integers so here it is so it is 2346 again but it is pre filling the uh space the 10 spaces that it allocated with zeros this time I am saying you now print put three character digit precisions after that allocate again 10 spaces but it make it left aligned right so minus means left aligned and the that's what I'm going to do is left align this time and there are three digits the three digigit Precision after that this time I would be just be asking it to print one Precision right one digit after that so it Tak a lot of digits after that but I want I wanted to only print one digit so let's do that as you can see it's just printing one digit after that and this I'm using asking you to print this value this floating Point number in scientific notation this e means scientific notation so let's do that as you can see it's representing it as in the scientific notation you can also print C++ strings using printf but they have to be converted into what's called C strings so this is all little bit complicated I am aware of it but just to show you that we could print uh C++ strings using printf so it is printf I want the printf format specifier uh you know basically percent s is used to print a string so I wanted to allocate 20 spaces right this 20 SP specifies that I would like the format specified to allocate 20 spaces and print this string of course you can this is a C++ string object that does not exist in C obviously we have to convert the string uh C++ string object into a c style uh string that's why we saying SDR doc string we we are making a call to the method C string to convert the string into a c style string and then printing printing it the next one just prints it as left aligned and let's do that so it is let continue this is going to print it right aligned and this is going to print it left aligned and the last one just prints I'm just you know taking a empty string and saying print it into um a allocated you know 20 character spaces and then print this empty string so that means I'm going to print 20 empty uh space characters on the screen that's that's it that's all there is to it as far as print f is concern I've got another example where I'm printing a nicely formatted um table which you would need in Project number lab number two so let me go over this one so that um you know you understand how you can use printf to print these kinds of nicely formatted tables so here I've got you know a couple of variable declarations at the top this is example two uh the first printf statement is just uh that it has no format specifier so it's just going to print this string literal literal on the screen so you've got the uh table kind of the top of the table uh Top Line then what here is look at it what I'm saying so I'm saying allocate seven spaces and print a character and make it right aligned and here I'm saying um allocate again seven spaces but this time the character is going to be printed left align so let me print this you you're going to see so you can see there are seven spaces here seven spaces here in the first one the um you know the character one is um printed right aligned in the second case character two is printed left aligned continue again allocate seven spaces this time I'm printing two integers because we've got percent D percent D the first one is left aligned the second one is right aligned so let's do that as you can see the first number integer which is 11 is printed left aligned and Y which is the second integer is printed right aligned and finally I'm printing two floating Point numbers again I'm allocating seven spaces because I don't want to change the format of this table uh after thatt with two precisions two digit precisions after that here with one digit Precision again this is right align this is left align so let me print that as you can see the first uh floating Point number is Right align the second floating Point number is is left aligned and I'm now printing the bottom line of the table so you can see using print of statements you can actually print these kinds of nicely formatted tables which you would be required to do in lab two which is coming up in 10 days I guess uh this is all I have for this lecture I just wanted to show you how you can use printf to print formatted output which comes handy in uh in a lot of programming languages kind of it it has become a standard in a lot of programming languages it's coming again the origins of it is in C I don't really know if it there was there were any predecessor to C that used to use this printer statement in a sense but I'm a c programmer as I told you so this was the this was the printer this was how we used to print things and it is still continued in C++ and also in Java and C any questions you might have just post it in the comment section