[MUSIC] We're going to start a new and very important topic and that is to do more advanced things with C input and output. And that of course also connects up with some ideas about the operating system and how the operating system connects to file I/O. So that gives you a lot more power and flexibility in using your computer. Now, what have we done so far? Largely what we've done is use printf and scanf. Those are two workhorses, they're fairly basic. Print goes to the screen, it uses standard out. Scanf goes to the keyboard, it uses standard in. We've also used occasionally what we call redirection which is, instead of going to the screen, we have this greater than sign, and then the output goes to a file. And if we have code.exe less than a file, then the file is providing input into scanf. So, we've also occasionally made use of what's called file redirection which uses a feature of the operating system. Now we want to get more sophisticated. So one thing we can do is learn more about printf. Printf always has a control string, which is a quoted string, and that gives instructions either verbatim as to what's printed out on the on the screen like hello world. Or it has things with percents in them that do conversion of things that occur in a list of other arguments and that's a comma separated list of arguments. And here's an appropriate example. If we were to say printf sum of x and y is %d and %d =%d, we would get on the screen sum of x and y is say x is 3 y, is 5, x plus y is z. And we would get 3, 5 and 8 the equal sign would show up and that would be on the screen. And that's sort of the standard kind of thing we've already done to date. And that's where reviews %d is a conversion of one of these arguments. Well, there's lots of conversions. They haven't use the full number. Some are fairly obscure for example percent sign, it's a way to print the percent sign. So if you have a double percent sign, rather than percent with some character, you actually literally put out a percent sign and you don't do a conversion. What we've generally use so far things like d for integer, f for float, c for character, s for string. Those are what have been most used in this course. This is where we've already mentioned where you have two percent signs would present a percent. Now we can add to that something called format specifier modifiers include modifiers and control codes. This is the table taken from something that's on OS. Sun Microsystem, was a very famous producer of, Large scale, Industrial strength, Computers, computers especially that could be used with advanced graphics. The company I believe got absorbed. I don't think you see many Sun machines anymore but there's still a lot of software that Sun produced and it had very, very good C compilers. And most of their work was in Unix using programming like the programming language C then they later actually invented Java. Now why are these modifiers useful? I put up a table there, you could see it. This is a standard thing where you'd print a character, let's say the character A. Here you're using 2C, 2 is a width modifier and that is a width where it's pushed to a right margin. So you have a blank space and capital A. And you can also do it with a minus. And if it's a minus, you have A with a blank space. We've also used %d. So if were to print 123, it would show up as 123. But if we do it with modifier for a zero, it says, however many digits you need to fill the space, the leading digits will be 0, so you see 00123 because that's of length five. So that's modifiers that do integers with padded zeros. And here is something where we modify and use a exponential way to write out a floating point number and if that floating point number is 0.123456789, it's a little less than an eighth. You would see on the screen that the 12 would get you 12 spaces. The conversion here is such that we have an exponent part. So the exponent is -1, so that's why one is really a -1 so that really means this is one-tenth and that's two one hundredths and that's three one thousandths, etc. And since it was 12, you would have a final empty space character. We're going to play with this for a second, I'll show you how to do it. Of course, very straightforward and it's a much easier way to capture these ideas because there isn't that much of a logic to it, but it really pays to experiment with it to see how the output actually appears on your own screen. And just keep in mind clear output is a sign of intelligent coding, it's a saying from Dr P. So lets go and look at, And I have already written this program. This is a program to demonstrate a little bit of what we just saw. Here's your double x equals that number, and here's some general we're going to entitle a general printing ideas, so we have some literate expression on the screen. And now watch what I'm going to do, I'm going to show you how x would be expressed as that -12.5e making sure that's what I just said. We'll also compare to just using a normal exponential, without any modifiers. We'll also use a floating point way of doing it, with some modifiers which also has a space modifier and a precision modifier. So space is 10, precision is 5 and then we're going to do something which actually is a no, no, we're going to use inappropriate conversion x is a floating point number and we're going to convert it to an integer. And we're going to see that gets us what basically is an error. But would be very hard to understand and is easily made error. So I've already compiled this let me run it. And there we are. So we can see here, this is using width of ten, and that's why there's a whole bunch of leading white space. And then was a 10.5f, so we get these five significant figures. Here, we get the ordinary use of e without any modifiers, so you get a much lengthier number. We get 1.23456 and the 7 is turned into an 8 because it was 7 and a fraction that was greater than a half. So that ups the conversion. So we're actually getting six significant figures here. Here we only got five significant figures, which is why we originally specified, and both of these are 10 to the -1. So that's why there's a 1 here as opposed to the way this floating point representation is. But here, look at this, converting it to integer, we get something that looks like nonsense. Why is that nonsense? Because internally, that was stored as a 32 bit representation of a floating point number, and this is how it converts to an integer. So there is no relation, we don't end up with what you might think would be a zero. In other words, if I had said, let me in fact do that so you can see it. I'm going to go back. I will create a second program, print2.c, I will incorporate, Now what I'm going to do here is, I won't change that, I'm going to change this and I'm going to cast it to an int. So I'm going to compile. Run it and indeed now, we get this value 0. We get what we had mistakenly thought was going to happen before. Okay, and you should play with such code yourself on modifiers and all the various kinds of conversion characters to the extent that you want to learn in a deeper way how to make use of the fancier output formats that are available to you. [MUSIC]