 Welcome back to 105. Thank you for joining me on this wonderful late Friday. This is not a great time. So where were we at last time? So we actually want our programs to do something and so far they can't do anything. We want them to kind of behave like that engineering black box where we take some input. Our program does something with it and then produces some output and right now we don't know how to do either. After this lecture you will be able to do both and you should be able to at least do lab one part one and more or less part two. You might need lecture four if some bad things happen though. So let's revisit our first Hello World program because that actually gave us some output although we didn't quite understand what was going on now but now after understanding data types a bit more we should be able to explain it a bit better. So remember the first line here that include std.io that's short for standard input output that's what we're going over today and it contains the what's called a declaration of printf so it tells you what inputs and outputs printf has that's what a declaration means. So after int main void we actually use the printf function and what it actually does is it outputs a string so that is also a new term for us and a string is simply just a sequence of characters so remember we went over characters last lecture a character is basically anything you can type on your keyboard assuming you have a standard US keyboard because unfortunately it is a bit US centric and note just like texting where you have to hit send or hit enter at the end of the line it should always have a backslash n which is that new line control character that we went over yesterday and the backslash was that escape code. So specifically that printf the string how you create a string or how you write out a string value is it will begin with a double quote and not a single quote single quote again is just for a single character sequence of them we it's bigger so we need two quotes I guess so string begins with a double quote followed by any number of characters until there is a another double quote and that signifies the end and whenever it's printed out you don't see the quotes they're not part of what's actually being printed just like for characters the single quotes aren't used so that main is a function and it can take inputs and output as well so that main is a function that takes no inputs that's what that void type means it just means nothing why it's a different word I don't know they like the word void whoever designed this language and at the beginning that int means the output of it is an int value and then that tells the operating system something for now we just always use the value zero to indicate that no problems have occurred so for functions recall the inputs to functions arguments so you'll probably hear that term no one says the input to a function they say the functions arguments but it means the same thing that's the input to the function so they're separated by commas if there is more than one so functions can take multiple inputs and you separate them by commas so print F how we would say it is it takes a string argument so the first input is supposed to be a string we'll get to the type of a string much later in the course but for now we can just ignore it and what it does is print F actually also outputs an int value but no one really cares about it it actually outputs a value that says how many characters were shown to the terminal and it also does the work of actually displaying the characters in the terminal which is the real important part here you don't really care about the return value of it or what its output is generally people ignore it so another definition is function call and that is just running a function so in our main when we had print F there we say we used print F or we'd say that we had a function call or we called print F so it can actually take multiple arguments or again multiple inputs so here's that example again so for print F at the beginning there I have my string that's hello world with a new line and then I could put comma and then one and I could give it as many inputs as I want but right now it can't use them so if I ran this program I would just still see hello world so like it and that value one there well because we wrote it out like that it is an int so the second argument in this case is an int but we'll see later what flexibility print F has so if we actually want to use print F to print values of things we can use something called a format string so I could give print F as the string argument I could say integer and then something that looks a bit weird right now I could say percent D and then a new line and what will actually happen is print F will go ahead and replace percent sign D with the value of the first integer argument so just like for characters we had an escape character to actually tell the computer that we're inputting something a bit weird the format string has its own escape character and it is that percent sign so because it's part of a format string it's technically called a format specifier is the technical name of it so some format specifiers we use it just tells print F what to what format to present the data actually in so percent sign D means output it as an integer so it should be either a negative or a positive whole number percent sign L F is for doubles so if I want something with a decimal point in it and the type of it is a double I should use L F if I want to print a character it's just percent sign C most of them make sense the only one that doesn't really make sense is L F but you just have to remember it unfortunately it stands for long float because they were not very creative with the names and they couldn't use D because they used it for int because it was a digit I guess so you might notice that because of this escape character that if you actually want to show a literal percent character well just like with the character if you want to use a backslash character you do backslash backslash to say I really mean a backslash same idea for the format specifier if you want to show a percent sign you have to really mean it you have to say percent sign percent sign and that will print a single percent sign any questions so far all right things we will just have to unfortunately remember a bit there's not much memorization this course but this is something you will get used to so this is how I would use it so for the string in print F that's my format string so I could say integer and then percent sign D and then as the second argument I could say one now these values are used in order so the first format specifier corresponds to how it's going to present the first argument so in this case if I actually ran it what would I see I would see integer column or colon space one so I would just see the number output in my terminal so any questions about that yep yeah so question is is there a limit of the number of arguments you can have and the answer is practically no you can have as many as you want until your computer runs out of memory which will be a very long time alright anything else quick so we could extend this a little bit so say I combined everything we know so far I could create a variable called x initialize it or to set its first value to one create a y set it equal to two and then I could print off two values so here is me using two other arguments with my format string so I could say point percent D comma percent D and again they're going to be in order so that means x will be printed first because it is the first format specifier there and then y will be second so these will always be done in order so after the format string the first format specifier is the first argument so on and so forth so any questions about that so if we run this we would see in our terminal point one two because well x is one it came first and then y is two and it came second so now we can actually write programs that do a little bit of something so so now we can kind of combine things together so if we want to see that well really computers just use numbers I can kind of mix and match them if I really want to so I could do something like say the integer value of just the character a so I could use the format specifier to say hey print it as an integer so interpret it as an integer and then I can give it as an argument just the char a and this is another way you can figure out what the value actually is so if I go ahead and compile that and then run it I'll see that the integer value of a is 65 so if I wanted to and I wanted them to all match all I have to do is change the format specifier I could say the character value of a I could save it go ahead compile it run it again and it would simply just print an a but really we know because if you look up in the ASCII table just because someone a long time ago said so capital a is equal to 65 everything on computer at the end of the day they're stored as bytes it will just be a number at some point so any questions about that yep yeah so the questions can I just put 65 here and then will it just print print a so if I do that so I saved it I can pile it run again so if I want just to see that that wasn't magic well the designers of it had it make sense so the next number up is just the next letter of the alphabet so by just arguing about it 66 should be B and you can go up so on and so forth if you want to see all the numbers ASCII ASCII table calm so any other questions about that yep so remember for like 65 66 since they're integers we just write out the integer value and for characters just the rule how C defines it is we have to do it with a single quote it's just C's rule I didn't make the rules so oh geez all right so the other piece of the puzzle is we can use scan f and scan f allows you to get input from the terminal so it's kind of like the opposite of print f so print f is for output and then scan f is for input the nice thing about it is it's kind of consistent they use that same format string but however instead of using a value it actually assigns to a value so that first thing we saw before with how we changed the value well that's what print f will or scan f will do for us it will go ahead and change our variable to another value but for it to do that it gets a little strange and we actually have to talk about the details of memory a little bit again which is why we started with that so in order for scan f to work annoyingly it needs the starting memory address of the variable and not the value of a variable itself so if I declared in x well it just means x is a value that is of type integer so can't really do anything with that for scan f it doesn't really care about the value it wants to change a value so with computers it actually needs to know where that is stored in memory so remember that value of a variable if it is an integer takes up four bytes so it will take up four of these addresses because remember memory is just a bunch of bytes each number is just where a byte is in memory so if it's an it it will take up four of them and it just so happens that they will always be sequential so they'll always be in a row so in this case we only really we just need two pieces of information where it starts and how big it is and because it's an integer we know how big it is so we just need to know where in memory it starts so in this case it would start at this address 512 and 4 so in order to actually get that number out of C well we need to do something with it and yes recall that it means it means four bytes so in C to get that starting address it makes things easy for you you just have to type a single character for it so if you put an ampersand in front of a variable name that will get the starting address of where that is stored in memory so it will become a pain point later but for now we just need to know that scan f needs that address in order to set a value so if we assume we have something called int x again that takes up four bytes a ampersand is something called like a unary operator so plus we need two numbers to add together that's a binary operator an ampersand just needs one single operand that's why it's a unary operator so we just need one name of a variable so we would say something like ampersand x and that would be the starting address of x in memory so the result of ampersand x so that's a new value it will have a new type that we will get into later but really that type is how it looks to you is it's just going to be original type with that star at the end so if my original type was int x the type of ampersand x in this case is going to be int star so if the compiler is telling you about types and they have a star in them that means it's talking about its address and we'll get to that at like the halfway point of the course where the details actually really really matter but for now just a starting address of that variable wherever it is in memory so now we can actually use our terminal for input and output so here is a full program that will take some input from the user and then provide some output so here we would have like printf here let's just go to the example itself so here we could printf and we're just prompting the user usually it's considered polite to ask or to tell the user what they need to actually type so here we'll just say input x and then give it a space so it looks pretty and then we'll just define a very or declare a variable called x and then we'll use scanf to actually assign a value to x for us so as part of our format specifier what we want from the user is just a single integer if I wanted two integers I can just type another percent sign D but then I need to provide another address of an int to actually store that value to so here I only want a single integer as input so I just say percent sign D and then I give it the address of x and then after that I will just use printf with the same format specifier so it's a digit so I'll print off the value of x so basically with this silly program whatever I input I should immediately output but I can see that I can type in the number and then display it so if we go ahead and compile that and actually run it it will say input x then wait for us to actually type something so I can type I don't know 14 hit enter and then it will go ahead store that to x and then actually display what x was so if I wanted to do something more substantial I could do something with x use it to compute something more exciting than nothing and actually output it when I'm done so any questions with this yep oh so the question is for the address why did in this example I have it start from like 500 in some gigantic number so that's just to illustrate you don't actually know this number it doesn't actually matter so your C compiler is going to pick the number for you and you don't have a choice about it so this is just a random number that I picked to show that it doesn't really matter yep yeah so the question is if I print ampersand x will it show me the location in memory and yes so we can do that to see that it is fairly meaningless it'll probably yell at me a little bit so if I change this well this is the address of x if I do that I'll probably get some nonsensical number here so I got some not oops so I'd so here I change it to be address of x what I output and I get this weird number so it's really really really big it's just going to be essentially a random number yep yeah so this is a 64-bit machine so that address is 64 bits ints are 32 so it's not quite big enough so we get some weird things we could make it long giant number so it's just gonna be some nonsensical number and it'll probably change every time too because that's just at the bottom just the add the byte it is in memory or the starting byte in memory so nice thing about C is it picks us picks it for us we don't have to really worry about what the actual value is we just use it yep yep yeah so the question is what about if I just you know completely ignore it and be like so I'll get some nonsensical output we don't we don't actually know how to check for errors or anything yet that the user did something silly so all the test cases for the labs right now just assume the user is nice to you and didn't do something silly like that but in real programs that's something you'll have to consider for a good while in this course we will be nice to you and assume you get valid input but it's something we should check all right any other questions all righty so so here that scanf there converts the characters part of scanf's job is to convert the characters we type into an int value and then it assigns that value to x using its address address through some type of magic that C can figure out and that you will figure out like halfway through the course so don't worry about it quite yet so one of the things scanf does is well it that function actually also outputs a number and it returns the number of values successfully assigned so in that case where I just typed hello it would probably output a zero while if I type something valid it would probably return a one but we don't know how to actually do anything about that yet and in fact we'll need another week to figure out how to do something about that so just note here that the type of the arguments for scanf and the format specifiers should match otherwise you'll get very weird and unpredictable results so like I showed before sometimes it's okay for printf and that it will make somewhat sense like when I printed the integer value of a and just converted it to an integer just so I could see whatever the value was sometimes it makes sense to mix them but in general they should always be the same so next little thing we can do is we know how to create variables and we can constantly reassign them sometimes that is confusing so we can actually create variables that do not change in C so we do the same thing we can declare a variable with a type and then a name but now we just add a const keyword before the type and a keyword just means it's a name reserved by C that you're not allowed to use as a name so if you tried to create a variable called const you would not be able to do it and const you might have noticed a trend now where C programmers like to just save typing a little bit for whatever reason so const is just short for constant we pretty much just chop off letters because we're lazy I guess so now if you want you could write const int x equals 1 and now what that means is you not it is not allowed to change anymore it's more like that mathematical sense so you aren't allowed to reassign x after you declare it so it's a good practice to add const to any variables that should never change because you're just not allowed to do it it will stop you from making a mistake but gets and this keyword gets much more complicated later on in fact I got a PhD out of it so it gets pretty complicated no do not clap for that one that was no but alright so so you should actually be consistent with variable naming as well so there's a certain style we should follow so normal variable names the style or the convention we use convention is just another term for rules but they're not really enforced so the rules ish are variable names should start with a lowercase letter and instead of a space we just capitalize the next word and the rule for const variables since they're a bit different and they're a bit special we can't change we just start them with a capital letter instead of a lowercase one so if it starts with the capital we know it's something we can't change so there's some special values where every letter is capitalized which you might see if you started lab one but we'll get into a bit later and for those the words are just separated by underscores if they are words so consistent coding style is really really important especially when you go back to code after not reading it for a while the C compiler itself ignores the white space so white space is just like tab characters spaces enters all that stuff the compiler doesn't really care about them therefore you as a human to add some structure to your code so you can actually read it so lab zero has you set up a formatter but you should also know the rules so a function definition like main so you tell it the input and output types and its names well that should be start at the beginning of the file and then every time you start a new set of curly brackets so I'm telling the computer to do something you need to indent the next lines and that just means I shove it in four spaces and for now I only have like one level of indentation because well we don't really have other sets of curly brackets or anything like that but that's the rule the indent is four spaces so that's why print F it's four spaces in so now we can actually write a program to actually do something useful only took us three lectures so we can whoops I didn't hit the button so we can create a program together just to do some simple conversions so we can convert let's say inches to centimeters so if we want to do that well if we think at a high level about what our program should do our input should probably be the inches and then our output is just that same measurement but now in centimeters so to start it off well I know that I'm going to convert two values and the inches per centimeter does not change so I could put a constant in front of it and because it is a number that needs a decimal I should use a double if it didn't need a decimal I should have used an int in this case I should probably use a double so we thought about the input the input should be inches so I should probably prompt the user first before I try to get some input so I would say enter number of inches something like whoops all right so now I'm prompting the users for something so now I need to get some type of input from them right so I'll probably need some help here because I don't know why I don't have a good excuse all right so I want some input I want just a double to represent the number of inches the user has typed so I need the format specifier LF so if I'm using scan F I need something else where am I actually going to store the value that the user has inputted what should I do next yep yeah I need to declare a variable that I can actually store the number of inches that the user actually inputs right so since inches will let them input a double so I'll declare a double I'll just call it inches and for scan F I have to tell scan F the address of the variable to actually change so I want to change it to inches like that so if I want to be really really safe when I program remember our first rule is well it's probably a good idea to just initialize variables just in case something bad happens because right here inches is just some random value and who knows of scan F something bad might happen to it so just to be on the safe side generally you can initialize variables to a zero it's generally a safe thing to do but in this case it probably won't matter so now that I have the inches and I want to convert to centimeters while I could just be really explicit about it and create a double called centimeters so if I want to compute the number of centimeters I can just assign it a new value and here I already have my input so my input is inches and we'll go over this more in the next lecture but if see you can just multiply two numbers together it's essentially lets you represent math very simply so I can check the number of inches and then multiply it by the inches per centimeter and then after that I will have the number of centimeters in the variable called cm so now if I actually want to display that to the user let's start writing we want if we want to output something to the user we have to use print F so I'll say measurement measurement in cm is now what format specifiers should I use right here to actually print off that value of that double yep LF right and format specifiers start with an ampersand so it's just ampersand LF am I done writing this format string no because I should be polite and essentially hit enter how do I represent an enter in a string back slash n right so now that's why I'm going to input or output and I need the actual value of the variable to output which was centimeters so now if I go ahead and I compile this so I say GCC inches to cm and then create a file called inches to cm and I run it it'll say enter the number of inches let's just verify that it can multiply by one so I'll just say one and then it says measurement in cm is ooh that's a lot of digits it says 2.50000 which kind of is a little bit ugly so what you will do in lab one and what you can do is part of this format specifier it will do some rounding for you and you can just use it to round so some extension we can do to this format specifier is before the LF we can say dot and then some number here and what that means is the number of digits to print after that decimal point and it will do the rounding for you so in lab one when it says round don't worry about having to round it yourself just use the format specifier to to change the data so now if I do something like this and I enter the number of inches let's say I don't know 12 which is like a foot says measurement cm is 30.48 which is just two decimal places which is properly rounded and we can just do that so any questions about that yep point to f without the L so like so if we do this pretty sure nothing bad happens yeah so for for print f you can use f or LF and it doesn't matter but for scan f it matters so for this course since we're not using just a float just always use LF but in the case you forget it for print f it doesn't actually matter for rules we won't get into yep yeah so if you get in the details and you just print f so if you don't actually need to know this because you shouldn't never use f but f f still converts it to a double anyways and it's weird so for print f LF and f actually mean the same thing for weird historical reasons yep yeah so there wait around to the nearest ten and this number here is just the number of decimal places or number of things to print after the decimal place right so if I want to run to the ten I could just change the two to one yep oh you have to run to the tens no if you want to run to the tens you would have to essentially divide by ten then round then multiply by ten so yeah so the rounding we'll go over rounding functions in the next lecture no it'll be two lectures from now but they only round to the nearest integer so if you want to round to a different decimal place you have to mess around with the decimal point any other questions anything else fun we can do all right so we can think it a bit I will finish the slides and then we can I have some fun things plan so here is what we had before it printed way too many digits and this one did not initialize inches so it's on the lecture side so for you so you have it but this is just what we typed so note if I really wanted to I could actually declare multiple variables on the same line by just separating them by commas so at the beginning if I wanted to go ahead and declare inches and centimeters ahead of time I could have done double comma centimeters and that would create a double called inches and double called centimeters generally I find that confusing so you can just have them on separate lines that C doesn't really care and it might be easier to read because with that comma things tend to be able to hide on you and you might actually miss it when you're reading it so it's better to put them all on separate lines so doubles by themselves contain approximately 16 decimal places what we saw was something called a subspecifier to change how to actually print the value so four numbers after you start with that function specifier or format specifier put a dot followed by a whole number to indicate the number of decimal places per to print there's a full list of subspecifiers you can use to change how you actually print things if you want them to use a certain number of characters or you know be left aligned or right aligned or whatever you want so the full list there but we don't really need to know any of them aside from that rounding one so the other thing about that rounding one is here go back so here when we let's say we just did point one here and let's say we just have one inch whoops I didn't recompile it I didn't save so here if I only have it to one it rounds to 2.5 but note that the 2.5 here is only what it shows you that value of that variable cm is still 2.5 4 dot the dot the dot just because you're printing it it does not change the value of that variable at all still it's just changing however you display it to the user so doesn't change that actual variable already so so other thing is we can change that to only output two decimal places and then sometimes it's good practice not to use like what are called magic values so when I returned zero from main it was just a number I picked up out of thin air that you didn't really know why I was using it generally it's a good idea to give things names and try and be expressive about them so I could include standard light standard lib dot h so that short for standard library and it defines something value called exit underscore success for you that you can use that's what you're supposed to use to like express that there's no errors in your program so we could be a bit more expressive and a bit more intentional I guess and use that whenever we're returning from main so instead of returning zero we can return exit success it will actually mean the same thing but it makes a bit more sense to humans right so writing an a is a lot easier than writing 65 and looking it up every time so the same idea it's the same idea here alright any other questions so now we can do very broken things which is fun yep sorry how yeah so here standard library and standard output so the question is how are they different so they're two different files and they include different things in them so for example this exit success is in the standard library dot h while in the standard I oh one that is where print f and scan f are so the different files will have different things how you know which one has which is you have to either look it up or remember it most of the time you just look it up some of them you'll get used to yep yeah if you don't do include either of them the compiler will be very angry at you and it may or may not refuse to compile it depends on how the compiler set up sometimes it will be chill about it sometimes it will not yep yep yeah so the question is why does that Boolean type have a separate header file for it and why do we need to use it so basically for historical reasons it didn't exist when the language was first created so it was something brought in after so they decided to do it like that why I don't know all right so no other questions I'll start doing random stuff to to be fun so we had a good question last time of like hey what happens if I write a number that is bigger than an integer so we went over it last time the maximum value of integer was like two to the 31 minus one for positive values so that's essentially this number but with the seven is the maximum so if I go one over the maximum what we think is going to happen so it can't represent it anymore it's too big so let's actually see what happens because this is my warning for you of like computers only have a finite amount of space so if I go ahead and compile this I'll see that my compiler is complaining a bit that the types don't match because well it is bigger than whoops all right here we go all right sorry so I'll restart so here I have print f and I am going to print a try to print a value of an int which is bigger than an int so the maximum size of an int is essentially this number but with the seven so if I just write this out this is as big as an int can represent so if I compile that and I run that I get exactly what I put in right makes sense so this is just a warning that computers get weird you'll be able to explain this by second year I believe but just because we have extra time there's no other questions I'll do weird stuff so if I compiled again seems to compile okay I get a little bit of a warning here that says format specifies int but argument has type long which we don't know what that is you have basically long is just a longer int so it takes up eight bytes instead of four basically it's trying to tell us that this don't fit so if I just happily ignore it I get that number but negative huh yeah that'd be weird for your program hmm and here if I increase it by one so to 49 well you might think it's that same number but negative that but actually same number but negative at the end seven so it does weird thing so this is actually defined so the rule is if you go over the maximum it just loops back around and then it goes to the smallest number it can represent so here was the smallest number it represents and it just keeps doing that over and over again so thankfully in this course you won't have to deal with this will be nice and everything will be within the limits but if you're actually developing computer software it's good to know that everything has a limit you can only represent a certain amount of values and if you go over that bad things happen so any other questions are silly things you want me to do after doing something that silly alright well we still can't do anything that silly so we'll do a lot more silly things in the next lecture but for now you should be able to do lab one part one and maybe two if you guys like a little bit on two it'll be all covered by the next lecture so just remember I'm pulling for you we're all in this together