Transcript for:
Fundamentals of C Programming

Hey, welcome to Giraffe Academy My name is Mike in this course I'm gonna be teaching you guys everything you need to know to get started in the C programming language C is an awesome programming language and it's actually one of the oldest programming languages around in fact A lot of modern programming languages are based off of C so it's a really good idea if you want to get into C or even if you're trying to get into something like C++ to learn the basics and in this course, we're gonna cover everything you need to know I'm gonna talk to you guys about installing a text editor and using the C compiler and we're gonna write some basic code I'll talk to you guys about you know, what is a program and how the program's work and how does C Read the instructions that you give it and we're gonna get into some more advanced stuff We're gonna look at things like if statements and loops we're gonna create different variables We're gonna talk about the different types of data that you can use and C then we're gonna get more advanced We're gonna talk about things like structures and the functions. We're going to talk about pointers And basically I'm just gonna give you a full overview of all of the core concepts in C So by the end of this course, you'll have a really good understanding and a really good foundation Which you can build on you can kind of go forward and learn some more I'm really excited to be bringing you guys this basic course on C I'm really excited if you guys to dive in and start using these tutorials So feel free to click around through all the videos and hopefully you'll learn something awesome about C In this tutorial I'm gonna talk to you guys about getting everything set up to start programming in C now in order to program in C We're actually going to need two things The first thing we're gonna need is an environment where we can write our C programs Now there's a bunch of these different environments. Essentially. All you need is just a text editor so anything that can you know, allow you to Write text and then save that text in a specific file format is gonna work although when we're working with C a lot of times it can be useful to use a special environment called an IDE an IDE stands for integrated Development environment. Basically, this is a special text editor which makes it a lot easier for us to write our C programs And so I'm gonna show you guys how to install an IDE called code blocks the second thing We need to write our C programs is going to be something called a C compiler now C is a programming language It means we can basically write out You know instructions that we want to give to the computer but eventually for the computer to be able to execute those instructions they have to be Compiled which basically means like translated or transformed into a language that the computer can understand So we're gonna basically download and install a special program that will do that for us. So let's get started I am going to come over to my web browser and I'm just gonna go up to the Google search bar. And I'm gonna search for a program called code block. So just type in Code blocks C. And this link should come up. It's code blocks org now This is an integrated development environment It's basically a text editor that will make it really easy for us to write our C programs. So I'm gonna come down here into downloads and There's a bunch of options here one says download the binary or at least download the source code We want to click on download the binary release. This is going to be the easiest option So if you're on Windows or Linux or Mac, you can install it from this page So depending on the operating system that you're on you want to click that. I'm on Windows So I'm gonna come down here to the windows option. You'll see there's little options for us to download this stuff So there's a bunch of options here. What we wanted download is going to be this one right here. It says code blocks And then the version number and then it says min GW - set up Basically what this is is it's gonna allow us to install code blocks So we're gonna be able to install that IDE program, and we're also going to be able to install AC Compiler. So remember I said we needed those two programs We needed those two things and this is actually going to give us both of them on Windows So I'm gonna come over here and click on the link to download this from sourceforge.net And you can see it's gone ahead and downloaded that So now let's head over to our downloads folder and I'm just gonna double click on this setup program so this should open up a window where we can set up the program and Let's just click through this So I'm just gonna click Next I'm gonna agree the license and basically just leave all the options as default and you should be good to go alright when that's done installing it's gonna ask us to run code blocks so we can just go ahead and do that and It's giving me this little window here. It says compilers auto detection So you can just click on the one up here. That's highlighted It says GNU GCC compiler and I'm just gonna click set as default and click OK and there you go We now have code blocks installed so in the next few tutorials We're gonna be learning about setting up code blocks writing our first C programs and really just getting started with C But for now, you have everything you need to get started In this tutorial I'm gonna talk to you guys about getting everything set up to start programming in C on OS X So if you're using a Mac Then this video will basically show you how to get everything set up in order to start working with C We're actually going to need two things. The first thing we're gonna need is a text editor basically We're going to use the text editor to write our C programs in and you can use any text editor that you want to write C programs but there's actually a special type of text editor which is called an IDE and it stands for Integrated development environment and it's basically a special environment where we can go to Write and run and manage our C programs So that's the first thing the second thing we're gonna need is what's called a C compiler now C is a programming language So essentially what we're doing when we're using it is we're writing instructions for the computer but in C We're writing instructions that us humans can understand and work with but in order for the computer to be able to carry them out they have to be compiled or Translated or transformed into a language that the computer can understand and that's what the C compiler is gonna do It's gonna take our C programs and essentially just translate them to a language that the computer can understand So the first thing I'm gonna do is show you guys how to install that compiler and then we'll get a text editor So what you want to do on your Mac is go over to the search bar and you're just gonna type in terminal and Click enter and a window that looks like this should pop up now The terminal is basically a program that allows us to interact with the computer using text commands So we're actually gonna need to use the terminal in order to do what we want to do so the first thing we want to do is check to see if you might already have AC compiler installed so in certain circumstances You might already have one. So you just want to type in C C - V and click enter and You'll see over here. I have AC compiler installed. So I'm getting all of this information. You can see over here It says like si Lang and it's giving me you know, a bunch of information If you have that, then you have your C compiler installed and you're ready to go But if you don't have that I'm going to show you guys how to get it So all you want to do is just type in Xcode select and then the space - - install and Click enter and you'll see I'm getting an error here because I already have these command line tools installed But if you don't already have them installed and obviously then you wouldn't have that C compiler Then this is gonna go off and install everything. You need to get that C compiler working so once you run that Xcode select command just type in CC - V again and You should be getting this version number Alright, so now that we have our C compiler installed What we want to do is download an IDE So we want to download a special text editor that we can use to write our C programs in so I'm gonna go over here To my web browser and I'm up here at a website called code blocks dot org, and this is an IDE called code blocks It's one of the most popular free IDE s for programming and C. So we're just gonna come down here to this downloads link and There should be a few options here download the binary released download the source code. We're gonna click download the binary release and You'll see down here We have a bunch of options for windows linux and mac click the mac option and if we come down here You'll see that here is what we need to install code blocks on the Mac So if you go over to the right side here, there's a link to SourceForge where we can download it So I just clicked that link and it should start downloading Automatically when that finishes downloading, let's head over to our downloads folder and you'll see here we have our zip file so I'm just gonna double click on this and They should give us code blocks now All you want to do is just drag and drop code blocks into your Applications folder and you're ready to go So you have code blocks installed and ready to start following along with this course and writing some awesome C programs In this tutorial, I'm gonna talk to you guys about getting our first C file setup So we're gonna load up a C file and we're gonna be able to get everything working and test out our program so I'm gonna open up my codeblocks program and this is the IDE that I'm gonna be using for the rest of this course So I'm just gonna double click on it and it should start opening once we have codeblocks open. You'll see over here There's a bunch of different options like create a new project open an existing project So we're actually going to want to create a new project So whenever we're gonna start writing some C files We're gonna want to create a new project in code blocks so you can either click this button right here, or you can go up to file new and projects so either way it's gonna bring you to this window over here and There's gonna be a bunch of options here basically Just these are different types of like C projects that we could create what we're gonna be doing is creating a console application This is like there's a basic C project that you can run on your computer. That's exactly what we need So click on console application and then click go and I'm just gonna click through this wizard and you'll see over here. It says C++ or C now C and C++ are both Different programming languages for this course, we're gonna be working with C So just highlight C and click Next and we're gonna give this a title I'm just gonna call mine draft and we're gonna have to put this project Inside of a folder so I'm gonna open this up and I'm just gonna put this on my desktop. So let's click OK and Click Next and you can leave all of these options as the default and we'll click finish so now what we should have is our first C project in code blocks now over here inside this little File Explorer you'll notice that Draf is right here. So that was the name of the project that I just created down. Here. We have this source folder So I'm going to click down and you'll see we have this file here called main dot C And this is a file that got automatically created for us by code blocks I'm just gonna right click and open it and you'll see over here. We have some default code so up here There's these little lines of code that say include down here. This says int main so this is the Program that we're given by default when we created our C project and this is sort of like the simplest C program that you can Right, essentially what this program does is it prints out hello world onto the screen So in order to run this program and test it just to make sure that everything's working I'm gonna come up here and you'll see that there's this Green play button over here and it just says run when I hover over it when I click this button This file that we have open right here main see is gonna get run So we're essentially telling code blocks to run this file and execute the program So when I click the Run button, we're getting this message. It says the project hasn't been built yet do you want to build it like yes, and Now over here this little window pops up Basically, this is the command prompt. So whenever I run a C program this little window is gonna pop up So if I was to exit out of this and run this program again You'll see this window pops up again. And it basically just executes the program and in the case of this program All we're doing is we're printing out Hello world onto the screen and that's actually what this instruction does over here. When we open up this command prompt. It's essentially just Executing the program and it's printing out whatever we told it to print out So it's printing out hello world over here and printing something out onto the screen is really easy And that's one instruction that we can give to the computer But as we go through this course we're going to learn all sorts of instructions that we can use but the purpose of this tutorial was just to get our C project set up and to test our main dot C file So as long as everything works here and you're good to go and now we can start really learning how to program in C In this tutorial I'm going to talk to you guys about the basics of writing a program in C So if you've been following along with the course up to this point, we've installed a text editor We've installed a C compiler and we set up our first C program. And our first C project Inside of code blocks. So now essentially what we have is this file over here. This is that main dot C file I just kind of want to point out a couple things that are in here first up here We have these little instructions which are just called include and I'm not gonna get too much into what those do in this tutorial Later on in the course. We're gonna talk about what these are actually doing and the specifics of you know, How they're helping our program, but for now just know that in order for us to use our program We're gonna need to have these guys up there next thing we have is this little block of code called main and It just has this name main you can see there's an open and closed parenthesis And this is actually what's called a method and don't worry too much about what methods are we're gonna again we're gonna talk more about those in future, but essentially a method is just like a container where we can put some of our code and This method main is very important because this is the method that's going to get executed when we run our program so in C we can actually write out a bunch of instructions for the computer and then we can run our program so we can tell the computer to Execute those programs and whenever the computer executes our C program It's gonna look inside of this main method in other words that's gonna look inside of all the code inside of these open and close curly brackets and it's gonna Execute that code so I have this instruction here It's called printf and basically this is just printing out some text onto the screen when we run our program It's gonna come into this main method and it's gonna execute all the instructions inside of here namely this printf method So if I was to run my program then this is gonna work now Let's talk about actually running a program whenever we want to run a program that we write in C We actually have to do two things The first thing we have to do is what's called building a program or compiling a program And basically what that does is it takes all of this C code It translates it into a language that the computer is going to be able to understand and execute So it's essentially taking this C program code Translating it into computer code and then the computer can execute it and run our program So the first thing we have to do is always build our C file The second thing we have to do is run the file that gets built so once we've built this program we've compiled it down into code the computer can understand we have to run it basically tell the computer to execute that code and in code blocks It's actually really easy to do this. So we can come over here and this little gear icon will allow us to build our program and then this play button will allow us to run our program but a lot of times when you're Programming you're gonna want to build and run your code at the same time, right? you're just gonna want to build it and run it just to see what Happened, you know? see how your program did So we have this option over here called build and run and this will both build your program and run it at the same time So generally as you're going through this course you're gonna want to be using this option That's build and run options when I click this it'll essentially build my program run it and then it'll show up here on this little window in this little window is basically a Console window you can call it. So a lot of people would refer to this as the console and It'll just like output any Information that we tell it to so if we write a C program that doesn't output information that doesn't print anything to the screen Then we wouldn't actually see this but since we're using this printf command, that window is gonna pop up So let's talk about how these programs actually get executed essentially when we write a program We have a set of instructions So when I'm writing a C program, I'm basically writing out a set of instructions imagine you were writing like a recipe or something Right a recipe is essentially just a set of instructions that when executed correctly will result in You know cooking or baking something delicious, right? And that's basically what these C programs are It's a set of instructions that when executed correctly will result in the computer doing something for us and one of the cool things about computers is that we can program them to do anything and C is one of the best languages to do that with so let's look at this instruction over here It's called print F and this prints something out onto the screen. We will notice over here at the end. We have this Semicolon and this semicolon is actually really important So whenever we write an instruction in C We always want to end it off with a semicolon That tells C that we're done with that instruction and we want to move on to the next one so I could actually copy this instruction and I could paste it down here and Now we're basically going to be executing this instruction twice so if I was to run and build my program you'll see that we're not only printing it out once but we're also printing it out twice So when we run our program the program comes down into this main method and it looks at all the instructions inside of it So it starts with this first instruction. It executes it, right? So it prints out hello world onto the screen and then it also prints out a new line So this backslash n is like a newline character and that'll move the text onto a new line When it's done with this instruction, it moves on to the next instruction and it'll execute that instruction So as we go forward and we learn how to write in C We're gonna learn more and more complex instructions And we'll be able to use all those instructions in conjunction with each other to specify some seriously complex programs but for now This is really the only instruction that we know which is printf so why don't we use this printf? Instruction in order to draw out a little shape onto the screen So I'm going to show you guys how we can draw out like a little triangle onto the screen I'm gonna take this printf and I'm actually just gonna get rid of hello world and I'm gonna copy this a couple times so I'm just gonna copy it and we'll paste it down here, maybe four times and What I can do is I can specify Instructions to the computer that will tell it to draw out a particular shape So down here I could make like a forward slash and over here We'll make another one and I'll make another one and I'll make another one and you can see here I've drawn like a little diagonal line with these forward slashes. Now, I'm gonna draw vertical bars going all the way down So I'm gonna put one here I'm gonna put one here. I'll put one here and finally, why don't we draw some underscores and then we'll put one so I'm basically drawing out a little triangle shape and these Backslash ends are just printing out a new line. So anytime I print this it's gonna move it on to a new line So it'll print this guy out now on the line down the line below So if I was to save my program and execute it now the computer is going to go through it's going to execute each one Of these instructions in order and we'll be able to print out the shape onto the screen. So let's do that You see over here. We get our shape and One thing I really want to stress. Is that order matters? So if I took this instruction and I placed it up here as the first instruction now This is gonna get printed out first So when I run my program you'll see that we're printing out this kind of Funky looking shape and that's because we changed up the order of the instructions So the computer changed the order in which it executed those instructions and you'll notice here again, we're using this Semicolon after each line of code. So that's kind of the basics of writing a program That's essentially all you need to know when we're writing a program we're specifying a list of instructions that we want the computer to carry out and Depending on which instructions we give the computer and which order we give them and how complex the instructions are That'll determine what the program does and as we go through this course, we're gonna learn more and more complex instructions we're also going to learn how we can take simple instructions like printf and Do different things with them to make our programs a lot more powerful? In this tutorial I want to talk to you guys about variables in C Now when we're using C programming language a lot of times we're gonna be dealing with data so there's gonna be different data values or different information that we're gonna want to keep track of and use inside of our programs and Sometimes it can be difficult to maintain and keep track of all that data and that's where variables come in a variable is Essentially a container where we can store different pieces of information so different data values we could sort of things like numbers or texts or characters and These variables make it a lot easier for us to keep track of and manage all the data in our program So I'm gonna show you guys basically how variables can be useful and how we can use them in our C programs But down here. I have a very basic program. I'm basically just Printing out a little story. It says there once was a man named George He was 70 years old. He really liked the name George but did not like being 70. So this is a simple program We're just printing out a bunch of different lines of text and then when I run this program You'll see we print out the actual story so over here we have our story and this is great But let's say that I'm reading through my story. I'm reading through my program and I'm thinking to myself hmmm I think I want to change the character's name So instead of calling the character George, why don't we call him like John in order to make that change? I'm gonna have to look through my entire program and find every place where we mention the character's name So right here is one change it to John remember keep looking and here's another one. So we'll change this to John, right? So I had to manually go through and change every instance of the character's name To John and let's say that okay. That sounds pretty good But maybe I'm thinking I want to make the character a little bit younger. So instead of 70 Why don't we make him 35? so now again I'm gonna have to look through my entire program find every place where we mentioned the character's age and change it to 35 So there and there there you go I changed the program I updated and modified it now If I was to run this program then all that information will be updated and we'll have new story. Here's the problem though I'm dealing with a very short story. I mean, this is four lines We only mentioned the character's age and name twice But imagine if I was writing out a huge C program that had hundreds of lines in this story So story had hundreds of lines and maybe we mentioned the character's name hundreds of times. They're mentioned their age hundreds of times Well if I wanted to change the characters age your name? I would have to go through and manually change it in every single location that would take forever and it would be a huge track Basically, that's not a good way for us to manage and maintain the data and our programs All right. We basically have two pieces of data that we're working with Consistently in this program the characters age and the character's name What I want to show you guys now is how we can use variables to better keep track of and manage these pieces of information So remember a variable is basically just a container where we can store some data so I want to create two variables which will store the character's name and The character's age and I'll show you guys how this can make our program a lot easier to Manage up here above these print statements I'm just going to make a new line and I'm gonna come over here and I want to create a variable Now whenever we create a variable in C, we have to give C a couple pieces of information So we have to basically tell C a couple different things The first thing we have to tell C is what type of information we want this variable to store now I'm gonna get into the different data types that we can represent and see more in the next tutorial but for now just know that there's a bunch of different types of data that we can store and C we can store things like Numbers characters we can store text we can store decimal numbers all sorts of stuff like that in our case I'm going to store the character's name and the character's name is basically just a collection of characters So in order to create this variable The first thing I have to do is tell C that I want to create a variable that's gonna store characters So to do that, I'm just gonna type out char just like that and that'll basically tell C Hey, we're gonna store characters inside of this variable After we type out char I want to type out the name of the variable that I want to create Remember of variables that contain and it's a good idea to give these containers Descriptive names and those names will basically let us know what that variable is storing inside of it. So I'm gonna call this Character name and now what I want to do is I basically want to store multiple characters so over here I'm telling to see that I want to store a character But in C we can also store instead of just storing like one single character we can store a bunch of characters and that would be kind of like you know a name so there's like four characters in here in order to store a bunch of characters inside of this variable after we Type the character Abel's name We're gonna have to make an open and closed square bracket And that's gonna tell C that we want to store a bunch of characters inside this variable So I'm gonna type char a character name open a close square brackets. I'm gonna set this equal to something So I'm essentially assigning a value to this variable and I'm just gonna make an open and closed quotation marks and inside of here We're gonna type out the value that we want to store So in my case, I'm just gonna store the character's name, which is John and then as always in C We're gonna have to end this off with a semicolon All right so essentially what I did here was I Created a variable and I told C what I wanted to store in the variable I wanted to store a character. I gave this variable a name. It was character name and I used these open and close square brackets to sell C that I wanted to store a bunch of characters. Not just one Now what we can do is we can make another variable to store the character's age So in this case, we're gonna want to do something similar, but we're gonna want to store a number There's a bunch of different ways that we can store numbers and see but for our purposes we're storing a name so we're gonna use something called an integer and an integer is basically just a whole number so I'm just gonna type out int and That basically stands for integer. And now we're gonna give this a name so I'm gonna call this Character age and I'm gonna set this equal to a number So in our story the character is 35 So I'm just gonna type out 35 and now we can again end this off with a semicolon So I have two variables here. This one's storing a collection of characters. This one's storing an integer Now the question becomes how can we use the inside of our program? well basically What we can do is we can print them out alongside of this text and we can actually use this print F Instruction in order to do that so I'm gonna show you guys how to do this and then we're gonna talk about it So basically, I'm gonna go over here where we have the character's name I'm gonna erase this and I'm gonna replace it with a % and an S and now over here I'm gonna make a comma and I'm just gonna type in the variable name. So I'm gonna type in character name and basically what's happening is when we use this percent s We're telling see that inside of this string of text here We're gonna want to insert a another String and a string is basically just a collection of characters so I could call this up here a string variable So when I say % s this is basically a placeholder and I'm telling C I'm gonna insert a string into here and Over here I use this comma and then I type out the string that I want to include so in our case It's character name and so basically over here It's gonna say there once was a man named % s and it's gonna replace % s with whatever we specify over here So it's gonna replace % s with the value that's stored inside that character name variable so I'm gonna save this and we'll run our program and Now you'll see that it's still printing out there once was a man named John Even though we didn't actually type out John over here We just typed % ass in the character name so I can do this in this other spot too. So down here We're also using the character's name so I can type % s and over here we can type Character name and again. This is going to act as a placeholder For this string of characters that we specify over here Let's do the same thing with the age so over here we have the age is 35 I'm going to do something similar and I'm gonna say percent but now because I have a number instead of using percent s I'm gonna use Percent D and that basically means that we're gonna insert an integer number into here So I'm saying he was percent D years old and over here. I'm gonna make , and I'm just gonna type out the name of the variable that I want to be placed inside of here so I'm just gonna be Character age and we're going to do this one more time down here. I'm going to type percent D and Character age so essentially what I'm doing is I'm printing out the value That's inside of character age where I specify this placeholders so Let's go ahead and run our program and now you'll see it's gonna look exactly the same as it did before it's the same story the same name and the same age Except I didn't have to actually type out the character's age and the character's name inside the story I just referred to the variable and what's cool about? Variables is I could come up here and let's say I wanted to modify the character's age all I would have to do is modify it in this one spot so we could change the characters a name to like Tom and We could also change their age to like 67 And now when I run my program all of that's gonna get updated in the story so now it's using the name Tom and the age of 67 in our case. We only mention the character's name and the character's age a couple times But you can imagine if I was mentioning this variable like ten or twenty or thirty times Having to change it in each one of those individual spots would be extremely difficult So variables are great because we can store the value one time and then if we want to modify it We just modify it in one place. So another cool thing that we can do with these variables is we can actually modify them So let's say that halfway through the story. I wanted to change the character's age. I could actually come down here I can make a new line and right above these print statements. I could give this variable a new value so I could say character age and I can just set it equal to something else so we can set it equal to like 30 and Again, we want to include that semicolon. So now halfway through the story. The characters age is actually gonna change So you'll see it says there once was a man named Tom, he was 67 and then down here The age is being a 30 so we actually modified the value that was stored inside of the variable Halfway through printing out our story and that's really why variables are useful So that's kind of a basic overview and introduction into what variables are why they're useful how we can use them And over here again, we're storing a collection of characters, which is called a string and we're also storing this integer But there's some other data types that we can also represent So in addition to representing like a string or a number we can do some other stuff as well And I'm going to talk about that in the next video But for now, this has just been a basic overview and introduction into variables and see In this tutorial I want to talk to you guys about data types in C and in the C programming language We're gonna be dealing with a lot of data So generally when you're writing programs, you're gonna be dealing with all sorts of information and in this tutorial I just want to talk to you guys about what types of information we can represent and use Inside of our C program and these are called data types. All right So what are the different types of data that we can work with in our program? So I'm gonna show you guys all those different data types right now So a lot of times in C when we're working with data, we're gonna be storing that data inside of variables so I'm just gonna show you guys a bunch of different variables that we can create and all the different types of data that we Can store inside of those variables? so remember whenever we create a variable and C we have to tell C a couple pieces of information and the first piece of information we need to tell C is what type of Data that we want to store inside of that container inside of that variable so let's talk about the different types of data One of the most basic types of data and probably one of the types of data you're going to be using the most Gonna be numbers. There's actually two very important types of numbers that we can represent in C So the first type is called an integer and an integer is basically a whole number so it's a counting number right if I was counting on my fingers 1 2 3 4 5 I'm just counting out whole numbers and I'm not saying like 1 1 and a half to 2.3 like I'm counting in whole numbers and that's what an integer is So to create an integer we could just say int And we'll give this a name so we could say like age and we could set age equal to like 40 or something Now whenever we want to use an integer in C. We always just want to type out the number So you'll notice I'm not putting quotation marks or parentheses or anything around this I'm just typing out the number like that. And now we actually have our variable age another type of number we can create is a Decimal number so this would be a number unlike an integer So it would be like 2.5 or eight point six seven or seven point five three eight, right? It's a number that has a decimal point associated to it. So there's actually two types of decimal number in C there's something called a double and there's something called a Float, and I'm not gonna get too much into the difference between the two of them But as a beginner to see for the most part You're just gonna want to use a double So I'm just gonna type out double and this could be like someone's GPA. For example, it might be like a 3.7 or whatever so, you know, you can use the double to represent decimal numbers it's also important to note that I could also represent like the number three so I could just say like 3.0 and that's gonna be a decimal or I could say 40 point O So the difference between 40 and 40 point O is that forty point O is a double It's a decimal number and 40 without the decimal point is an integer So that's something just to keep in mind so you can make this like three point six So those are the two basic types of numbers integers and doubles and those are gonna come in handy all the time Now the next type of data that we're gonna want to store or is gonna be characters So in C we can store single characters inside of a variable so I could just say CH AR and that stands for character and we could give this a value So maybe it'd be like someone's grade and when we create a character in C We're basically going to specify the character that we want to store inside of single quotation marks So inside of these single quotation marks, I could put a letter I could put like a a lowercase a we could put h C d AF basically you can put any character that you want to store So if I put an A here now, this character variable grade is gonna store the single character a inside of it It's important to note that you can only store one character so I couldn't put like a whole bunch of characters over here And that's actually gonna throw an error All we can do is store one single character, but these three are the basic data types in C So these are the basic types of information that we can deal with in our C programs and just using these Three data types, we can represent all sorts of information I also want to talk to you guys about one sort of extra data type and it's not Technically a data type in C. It's technically like a modification of the character data type So you'll notice when we create a character we can only store one letter one single character inside of it But a lot of times we're gonna want to represent more than one Character so we're gonna want to represent like a string of characters and generally this is called a string so it's a collection of characters that are grouped together and we can use this char datatype in order to create a String so I can basically say char I can type out the name of the string that I want to create so I could say like phrase and then after this I can make an open and closed square brackets and this is basically gonna tell see that we want to string a bunch of characters together and What we're actually doing is creating something called an array and I'm gonna get into arrays later in the course But just for now know that we can create a string of characters by using char Specifying the name these open and close square brackets, and then we can create this using to open and closed quotation marks So instead of these single quotes up here We're just gonna use double quotes and I could type in like a phrase so I could type in whatever String of text I want here and this will get stored inside of this Variable which like I said is technically called an array now It's important to note that Doing something like this or creating this like string of characters is a little bit different from these guys up here So like these variables up here we could modify them We can give them new values but you can't do stuff like that with this So just know that this is like a special type of data and it's gonna act a little bit differently than these ones up here But that's one of the basics of working with data and those are all the basic datatypes that we're going to be using in our C programs In this tutorial I want to talk to you guys some more about the print F function so if you've been following along with this tutorial We've been using an instruction and our C programs called Printf and printf basically allows us to print different things out onto the screen in this tutorial We're going to talk more about what printf does and we're gonna look at how we can use it to 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 hoon addition to % s in % D We can 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 going to come in handy when we're writing more complex programs In this tutorial I want to talk to you guys about working with numbers and see now a lot of times when we're writing programs in C We're gonna want to do different things with numbers whether that's just storing numbers and keeping track of them or adding multiplying Subtracting doing math with numbers or you know, really just dealing with numbers in general So I'm just gonna give you guys a basic overview of a lot of the stuff we can do with numbers We're also gonna talk about how different number types in Iraq. So how like integers and doubles interact and all that fun stuff So down here I am printing out a number so you can see I'm using this printf function and inside of these quotation marks I'm Basically saying % F And % f stands for like a floating-point number so that could be either a double or a float basically a decimal number so we put % F in here that's gonna allow us to print out a decimal number just like this and You can see I can run my program and it'll print out 8.9 and you'll notice that it's printing out not just 8.9 But it's also printing it out to a very precise decimal point and that's what C is always gonna do so it's always gonna print it out to you know, A very precise decimal. So using these decimals I can do all sorts of stuff So obviously I can just print out the decimal but I could also do things like math so I could say like five point zero plus four point five and This is gonna go ahead and be able to do that for us when I run my program Not only is it gonna like print this out but it's gonna do the actual math operation and you see we get nine point five so in addition to addition we can also use subtraction division, which is going to be a forward slash and Multiplication which is going to be in asterisks and you can use all these different things to let you know do the four basic math Operations addition subtraction multiplication and division. So now I want to talk about how we can use floating-point numbers so like doubles and floats Alongside integers. So let's say I was going to add an integer With a double. So if I said 5 plus 4.5. Well, actually when we do this this entire answer is going to get converted into a Decimal number so when I say 5 which is an integer plus 4.5. We're going to get a floating-point number back so we're going to get 9.5 and basically any operation that you do between an integer and a floating-point number is going to return a floating-point number back So that's important to realize but if I was to change this to four now This isn't gonna work anymore because this is asking for a floating-point number and we're giving it an integer So when I do math with two integers it's going to give us an integer back and if I do math with a floating point number and integer it will give us a Floating-point number back. It's also important to realize for example if I took five and divided it by four This should actually be a decimal number right and you'd expect us to get a decimal back But if I just print it out percent D. So I printed out the result of the integer five divided by the integer four I'm actually gonna get an integer back So when I run the program you'll see we're getting a one here, but really this like the answer is one technically But it's one with a bunch of decimal points after it. Right? It's one plus a bunch of other stuff But when we just do math with two integers we're gonna get an integer back if I was to say five divided by four point zero and I've printed out a Decimal now, we're gonna get the entire answer back. So we're gonna get one point two five So that's just a little bit about dealing with integers and floating point numbers We could also take these and put these into variables so, you know in addition to just having numbers down here I could specify a variable like You know num set it equal to six and we can go ahead and print num out down here as well I need to make this a D So now I will be able to print out that variable with no problem So it can be really useful sometimes to store these numbers inside of variables I also want to talk to you guys about using more complex mathematical functions now in C We have these things called Functions which are basically just little blocks of code that we can call and when we call them they're gonna go off and do Certain things for us. So they're like modify a value or give us information about a value and these functions can be really useful I'm gonna show you guys a couple functions that we can use with math So I'm just going to come over here and I can actually start using these functions and all you have to do is just type Out the name the function that you want to use So by default C is going to give us access to a bunch of math functions that we can use So we really don't have to do anything all we have to do is just type in their names and I'm gonna show you guys want it's just POWs, so I'm just typing in P Oh w then I'm gonna type an open and closed parenthesis and inside of these parentheses I can actually give this two numbers what this is gonna do is it's gonna take the first number that we give it and it's gonna take it to the second number so if I said like 2 & 3 And you'll notice I'm separating them with a comma this is basically gonna give us 2 raised to the third power So to give us 2 cubed so when I run my program and actually we need to print this out as a decimal number So this is gonna give us a decimal number back. So I need to print this out with F When I run my program we're gonna get 8 back just like that. So it's basically Cubing 2 I could say like 4 raised to the third power and now we should get back 64 which we do So that can be pretty useful. There's a couple other ones. I'll show you another one is SQ RT So this will allow you to take a number square root So if I said like 36 in here now, we're gonna print out the square root of 36 Which is gonna be 6 and there's a couple other ones So let's say that I had a decimal I could say C EIL which stands for ceiling So if I have like thirty six point seven or 36 point three five six This is gonna give us the hot the next highest number. So it'll basically round this number up and this is gonna give us 37 as you can see We can do another one which is called floor and floor. We'll do the opposite so floor We'll just round the number down no matter what. So if I have like 36 point six five six, this will round it down to 36 So there's all sorts of little, you know things we can do little functions that we can call and get information about numbers and like I said these Functions will either like modify a number so it would like give us the floor or the ceiling Or sometimes they'll like give us information about numbers So that's sort of the basics of working with numbers, you know you can do all sorts of math with numbers you can add in some to multiply and you can even Multiply add subtract integer numbers and floating-point numbers. So play around with all these different things with numbers and if you want to find some more of these little math functions All you have to do is just go online and Google search for C math functions there's like dozens of these little functions that you can use, but I think these are some of the most widely used In this tutorial I want to talk to you guys about using comments and see well comment is a special block of code and see Which actually gets ignored when we run our program, so if you want to write a comment you can use a special starting and ending tag and any of the Text any of the code anything that you put inside of that tag isn't gonna get executed or really looked at by C So we can use comments to do all sorts of things, you know You can leave little notes or little comments inside of your program. You can use comments to Temporarily disable certain lines of code. They can be really useful So I'm gonna show you guys the basics down here in my program if I want to create a comment I can start a comment Using a forward slash and an asterisk and you'll see when I put this asterisks in all the code in my text editor changed color and basically what this means is anything that comes after this forward slash and Asterix is gonna be considered a comment in Order to close off the comment. I can use an another asterisk and another forward slash You'll see the code changed back to normal code Inside of these two asterisks is I can basically write out anything I want and it's gonna get considered as a comment so for example If I you know wrote out like my program and I ran my program this whole thing is gonna get ignored by C So it's just gonna print out comments or fun It's not gonna do anything else and you know Like I said comments are really useful so I could leave a little note up here I could write something like to do like I have to do something and a lot of times people we use comments to Explain certain lines of code so I could put a comment here and I could say like this print out text, right? So maybe you know, this was like a really complex line of code We could use this text in order to explain it You can also use comments to do something called commenting out a line of code So for example, let's say I wanted to run my program but I didn't want to have this line of code in there Well one thing I could do would just be to delete the entire line of code Right so I could delete this code and now I can run my program and it's no longer gonna print it out It's no longer gonna do anything. But here's the problem in order to do that. I had to actually delete the of code so instead of deleting it I could just bring it back and I could comment it out so I could basically surround it with these comment tags and Now the computer is just gonna ignore it. So it's not gonna render it because it's been commented out So now when I run my program it's gonna do the same thing. It won't execute that line of code But I didn't have to actually delete the line. I could just comment it out So a lot of times when you're writing programs You might want to try to run your program without a certain line or a certain you know series of lines of code and instead of deleting them and then running the program you can just comment them out and See will basically ignore them so that can be really useful So those are the two basic uses for comments and essentially comments are open, right? It's just a way that we can write out information in our files. That won't get rendered by C And so you can really use comments to do whatever you want Now I will say one best practice with comments is to use them sparingly Generally, you only want to use a comment when you absolutely have to so we can be really annoying if you open up a C File and there's like comments littered all around and obviously if you want to put a bunch of comments in your code I mean no one's stopping you but as a best practice going forward I think generally only using comments when they're absolutely necessary is kind of like a good practice But again, it's open, you know, you can use comments for whatever you want In this tutorial I want to talk to you guys about constants in C Well constant is a special type of variable and C which can't be modified So when I create a constant and C, I'm basically creating a value that is Unable to be modified. So I'm gonna show you guys basically how this works out. Let's say I created a variable Let's say I created an integer and I just called it num and I set it equal to 5 right, and then I printed out that integer onto the screen so we can print this out percent D and We're just gonna pranaam right so I can create this num variable. I can print it out onto the screen Let's say that down here I wanted to modify num so I could basically say num is equal to 8 so I can modify The value that's stored inside of the num variable And let's come down here and we'll print this out as well. So d And we'll print out num again. And actually I'm gonna print out a new line here So when I go ahead and run this program and I forgot to send me : when I go ahead and run this program you'll see we're printing out five and then we modified the value to eight and then we printed out eight so I was able to modify this value down here I was Mabel to modify num But in certain circumstances and see you're gonna want to create variables or you're gonna want to create values that can't be modified So these would be values that just like can't change and those are called constants So if I wanted to make a num a constant in other words, let's say for some reason I didn't want the num variable to be able to be modified in my program I could come over here and I could say Const so Co NS T and I'm gonna say that right before I declare the type now, you can also say Const Right after you declare the type, but I've always preferred to just put it before So I say Const int num 5 and now this program is actually gonna throw an error. So when I try to run this You'll see down here. It's getting highlighted in red. That's because I can't modify a constant So I'm trying to modify a num down here But I can't because I gave it this Const keyword because it's now considered a constant So in a lot of cases you want have variables that just can't be modified. Can't be changed and In a situation like that you want to make them consonants also a lot of times when we make constant variables in C We'll give them all uppercase names and this isn't required this is just sort of like what a lot of developers will do so they'll say like instead of n Um like this they would say like num or maybe this is like your favorite number so you could say fav underscore num So that would be how you could like distinguish two words but generally like constants will be all capital and that's just so it's kind of obvious that they're Unchangeable that they're constants. But again, you don't have to do that. But that's kind of like a best practice so this is one way that we can create a constant, and we basically create a constant variable that can't be changed but a constant could also refer to Just like any number or any text that we're using in our program So, for example, I'm gonna get rid of this if I came down here and I just like printed out some text So let's say I just printed out like Hello. This is actually also gonna be considered a constant So this is just like a string of characters a string of text That's also considered a constant because it's just like text on the screen. We can't change it. We can't modify it It's just kind of there so you would also consider this a Constant if I was printing out a number to like let's say I came over here and print out 77 T is also considered a constant So it's just considered to be like a piece of information or a piece of data in our program That is sort of unchanging. Right? So 70 this isn't gonna change I mean unless I physically came in here and changed it like This value is always gonna be 90 no matter what I can't modify it unless I like manually come in and modify it So that's also considered a constant But I would say like for the most part like where this is going to come in handy is obviously doing stuff like this But creating constant variables variables that can't change In this tutorial I'm gonna show you guys how to get input from a user in C So a lot of times in our C program So we're going to be working with all different types of information and a lot of times we're gonna want to get some of that Information from the user so I'm going to show you guys how we can prompt the user to enter in some information We can take that information store it inside of variables and then we can use it inside of our program So this is gonna be pretty cool. So down here I will basically write out the code for this and the first thing we want to do whenever we're getting input from the user is We want to prompt them. So I want to prompt the user, you know, as far as what they should enter in So I'm just gonna say printf and over here. I'm just gonna print out a prompt. So why don't we Ask the user to enter in their age. So well, I'll show you guys how we can get a number from the user so I'm just gonna say enter your age and Now that we've prompted them to enter their age I need to do two things So the first thing I want to do is create a variable where we can store the input that the user puts into the program So I want to create a variable where we can store the age that the user inputs So I'm gonna create an inn over here. Oh, it's called age. And I'm not gonna give this a value so all I'm gonna do up here is just Declare the variable. I'm just gonna tell see that I want to use this variable But I'm not gonna give it a value in other words. I'm gonna allow the user who's inputting the age to give this a value So down here I want to be able to get input for from the user so I can use another function which is called scanf and scanf is basically going to allow the user to enter in some information into our program and This works similar to printf it's kind of doing the opposite of printf right printf is printing something onto the screen Scanf is allowing the user to input something Into the program and we're gonna make an open and close quotation marks and in here I basically want to tell see what type of information I'm asking the user to enter in So in our case we're asking for an integer right age is going to be an integer it's a whole number so I'm gonna accept as an input and integer and Over here. We want to tell C where we want to put integer So I'm basically gonna tell see what variable I want to store this in. So I'm gonna store this inside of my age variable and I can essentially just type out the name of the variable here But in order to get input from the user I'm actually gonna have to type an ampersand in front of this So instead of just typing out age. I'm gonna have to type ampersand age And when I say ampersand age, this is what's called a pointer and we're gonna talk about pointers in a later tutorial I'm gonna cover everything you need to know about pointers But for now, that's a little bit beyond what we need to learn so all you need to know is that when you're using scanf and you want to store information inside of like An integer or a float or like a character you need to use this ampersand over here So down here now that we've scanned for the users age. I'm just gonna go ahead and print it out So we'll just print out like you are and I'll say % D years old and over here I'm just gonna print out that age variable. So essentially what I'm doing is I'm prompting the user to enter their age I'm storing whatever age they enter inside of this age variable and then I'm printing it out to them So let's go ahead and run this program and we'll see how we did So I'm gonna run the program and it says enter your age. So let's say someone is 50 when I click enter It's gonna take that value It's can take that integer that we entered 50 store it inside of that age variable and print it out You'll see over here. It says you are 50 years old. So that's how we can get an integer from the user We can also get like a double from the user so for example, I could say enter your GPA and so now instead of asking for an integer we're gonna be asking for a double so I can come up here and say like double and We could call this GPA and now I can do the same thing. But instead of saying D I want to say L F and L F is basically going to tell this scanf function that we're looking for a Double and then obviously instead of age. We're just gonna put GPA inside of here. And so now we can just say like your GPA is and then we can put % F because we're gonna be printing out a double and I'll just say GPA. So you'll notice here in printf when we want to use a double We're using percent F to print it out. But when we're using scanf we're going to use percent LF So that's just like a little difference and now let's go ahead and run this program And we should be able to get a GPA. So let's say someone's GPA is like 3.1. Now. It says your GPA is 3.1. So that's how we can get a double. I also want to show you guys how we can get a character so why don't we create a character up here and we'll just call it grade and We'll say enter your grade and Now when we want to get a character we can just say percent C and again I can just come down here and say ampersand grade and then down here we can just say your grade is Percent C and we'll go ahead and print out the grade So now we should be able to get a character from the user so enter the grade let's say I got an A and it says you're good as a so we can use this scanf to get specific types of Input from the user we can store those specific types of input inside of variables now I also want to show you guys one more thing we can do which is we can actually get a string from the user so in addition to getting numbers and a character, we could also get like a string of characters from the user and this is going to be a little bit different from Doing like numbers and characters. So I want to kind of show you guys how we can do this really quick so over here Let's create a variable. We'll just call it. Let's just call it name. So we'll be storing someone's name and Whenever we're creating a string of characters, we always need these open and close square brackets and inside of here I'm actually going to specify how many characters I want to be able to store inside this string of characters so I'm just gonna put 20 and In the past in this course when we when we've been creating strings. We haven't put a number in there We've just kind of said like whatever. All right, we basically just given this a value straight away But in this particular situation, we're not gonna be giving this variable of value right away in other words Like I don't know what the users name is gonna be like, I don't know that right up front They're gonna tell us what that is. And so if I'm not gonna give this a value, right? I do actually need to tell see how big I want this variable to be in other words I need to tell see how many characters I want this variable to be able to store and that way si can go ahead and Allocate enough memory for this variable. So I'm just gonna put 20 and 20 basically means this will be able to store 20 characters And I think that's enough for a name so down here. We'll just say enter your name and I can use scanf in order to get input in the form of a string but instead of saying percent see I'm just gonna say percent s and Over here instead of saying ampersand grade I'm just gonna type out the name of the string of characters. So I don't actually need this ampersand here. I can just Specify the name of the string so down here it says printf your grade is and actually let's just say your name is I would have say percent s and then over here we can print out the name So this should work just like it did in the other cases So let's go ahead and run this and it says enter your name. So my name is Mike and You'll see it says your name is Mike. So that works out really well, but there is one problem when we're using scanf in order to get a string from the user So for example, if I came in here, and I said enter your name, and I said my name was john Smith when I click enter You'll notice that it's only saying your name is. John, it's not including smith over here and Here's the problem. This is because Whenever I use this scanf function and I use it with a string. It's only gonna grab the characters up to the first Space so once it sees this space it's gonna be like, okay, we're done getting characters, right? So that's kind of a problem and that's just kind of how scanf works. It's not really, you know scanf fault There is a way that we can modify scanf in order to be able to get input with spaces but there's another function which I want to show you guys which we can use to get a line of text from the user and it's called F gets and Afghans is another function it's similar to scanf but f gets is Basically going to be more generous F gas is essentially just gonna grab like a whole line of text It's not gonna be able to grab it and store it inside of like an integer or a variable or a character It's just gonna be able to store it inside of like a string of characters. So When we're using a forgets the first argument We want to give it is the name of the variable where we want to store to the line of text So in our case, it's just gonna be this name variable The next thing we want to do is specify how many characters we want to be able to input from the user So this will essentially limit the amount of characters that the user can input And this is always a good idea when we're trying to get a string from the user because one common problem is that the user could try to Enter in like a million characters and they would overflow the like the buffer in other words Like see wouldn't be able to handle accepting that many characters and the program might break So over here with F gaps We can specify how many characters we want to be able to accept so I'm just gonna say 20 Because that's how many characters we can store inside of this variable and over here I'm just gonna say stdin and this stands for standard input essentially what we're doing over here is we're telling FCAT s-- where we want to get the information from and standard input is set as essentially just like that little console that we've been using so Now I'm using F Gatz and this is going to do exactly what scanf did so we'll be able to store the input inside of this named variable, but now we'll be able to store Multiple words instead of just one single word. So let's run this program and it says enter your name So now if we entered in John Smith You'll see it's able to enter in John Smith Now the one downside with just using this F gets is you'll notice that it printed out this new line over here So let me actually demonstrate this a little bit So if I was to print out like, you know, just some random text over here Whenever I get input using scanf, so if I entered in John Smith When I click the enter key So when I'm entering this by clicking the Enter key that's actually going to represent a newline character and that's going to get stored Inside of the string. So when I click enter you'll notice it says your name is John Smith And then it prints out a newline and it prints out this text And so that's just something that you need to be aware of when you're using this F gets function but for the most part, I would say if you're getting input from A user in the form of a string you can use F gets you can also use scanf and you could use multiple Percent asses and i'm actually going to show you guys how we could do something like that in a future tutorial But for the most part I would say whenever you're getting a string from the user you just want to use F Guess so that's the basics of getting input from the user like I said there's a couple other things that we can do and I'm gonna talk about At least one other way that we can get strings from the user in a future tutorial But this is kind of the basics and you can kind of play around with doing this stuff in your programs In this tutorial I'm going to show you guys how to build a basic calculator in C we're basically gonna build a little program where the user can enter in two numbers and then Our program will take those two numbers add them together and spit out the answer So this is gonna be kind of cool and we'll also learn about getting numbers as input from a user So over here, I want to show you guys exactly how we can do this the first thing I want to do is Print out a prompt. So I basically want to prompt the user for some input so over here I can just say printf and Inside here. We're just gonna give them a little prompt. So I'll say enter first number and Essentially what we're gonna have them do is enter in two numbers, so I'm actually going to create two variables up here so we'll make an int and we'll call it num1 and Then we'll make another inch and we'll call it num2 and i'm not actually going to give these Values right up front we're gonna end up giving these whatever the user enters in So after the user enters the first number we need to actually grab that number so I can use another see function called scanf and In here, we're basically just gonna say percent d because we want to grab an integer and then over here I'm going to say the name of the variable Where I want to store the value that gets entered now if you're following along with this course in the last tutorial We were able to get a string of characters as input from the user And basically we just typed in like the name of the variable But when we're getting input, that's not a string of characters In other words when we're getting input that's not percent s so if it's like percent d or percent 4 percent C so if it's a guy a decimal or an integer or a float or a character, we actually need to use a special symbol this ampersand So I'm gonna have to say ampersand and then the name of the variable what I want to store this value so I'm just gonna say ampersand 1 and Basically what this means is we're accessing the address of num 1 and we're gonna talk about addresses and pointers in a future video but for now just know that you need this ampersand here in order to Store the value that gets entered inside of this variable So once we do that and now we can move on and we're basically just gonna do the same exact thing but for the second number so I'm gonna copy these and I'll paste this and now instead of saying enter first number. We'll say enter Second number and we're just gonna store this inside of num2 Alright, so once we're done with this, the last thing we want to do is add them together and print out the answer So I'm just going to come down here. I'm going to say printf and We'll just say answer and over here We'll print out the answer so it's gonna be an integer and we're basically just gonna print out num1 plus num2 So we're gonna print out the value of num1 plus num2. So we have our basic calculator. We're getting the first number We're getting the second number. We're storing them inside of these variables. We're adding them together and printing them out Let's try to run our calculator. So I'm gonna run my program and you'll see over here. It says enter first number So let's go ahead and enter 6 enter Second number will enter A8 and now we're gonna get 14. So 14 is the correct answer So our program worked it was able to add the numbers correctly and everything's awesome But let me show you guys one problem with this program if I wanted to do math on Multiple non decimal numbers, for example if I said 2 and then over here I said like 6 point 8 when I add these together We're not going to get the correct answer. We're gonna get 6 plus 2 Which is gonna be an integer 8, but we're not gonna get 8 point 8, so we're not getting the correct value So instead of letting the user enter in only integers Why don't we instead let them enter in doubles so over here? I'm going to change these to double so I'm gonna say num one's gonna be a double and num2 is gonna be a double and Since we're getting doubles as input, we're gonna have to come down here and modify these So right now this is accepting an integer but we want to make this accept a double so normally if we're using Printf and we wanted to print out a double we would say f and that stands for like floating-point number but when we're scanning for a number if we want to use a double we have to say LF just like that so down here. I'm also gonna say LF and we'll change this to just F so print F is a little bit different than Scanf right and scanf if we want to accept a double as input. We need to use LF in printf We just use % F. So let's run our program now and we should be able to add floating-point numbers so decimal numbers over here I will do four point five plus six point seven and we get 11 point two, so that looks good to me Looks like everything is working properly. And now we have a basic calculator So the user can enter in any numbers that they want and the calculator will be able to handle them Now this calculator is not like 100% ironclad secure for example, like if I was to come up here and Enter in like a string of characters instead of a number you'll see that it's gonna break the program, right? So it's just like not doing what we wanted to do And as we go further in this course We're gonna learn all sorts of ways that we can check to see if certain things are getting entered correctly and we can basically mitigate circumstances like that, but for now, this is just kind of an introduction into how we can get numbers as input and do things like Addition or multiplication once we have them In this tutorial I'm going to talk to you guys about building a little game in C more specifically We're gonna be building a mad libs game now If you ever played the game mad libs it's basically a game where you write down a bunch of random words so it could be like You know nouns or verbs or someone's name or you know a verb ending in ing, something like that you take all of those words that you enter in and you kind of Sprinkle them in into a story and then generally the story is like kind of funny because it has all these random words in it It's actually if we head over to my web browser You'll see I have a picture of a Madlib up here And basically you just add in a bunch of random words into the story and then you'd read the story back and it could be Kind of funny so I'm gonna show you guys how we can build something like that in C And we're also gonna talk about some more ways that we can use that scanf function that I showed you guys in the last tutorial So over here, we have a little story that I printed out. It just says roses are red violets are blue I love you kind of like a classic poem But I think this poem would be a lot funnier if we turned it into a mad libs So that's exactly what we're gonna do. I'm gonna replace roses are red. I'm gonna replace red with a random color So we're just gonna have a user enter in a color. I'll replace violets with a plural noun So we're gonna have them enter in a plural noun, and I'm gonna have instead of saying I love you We're gonna say I love and then a specific celebrity. So I'll just type in celebrity right there Alright, so this is basically what we're gonna be printing out we're gonna be printed out roses are and then whatever color they enter in Plural nouns are blue and then I love whatever celebrity so let's talk about actually creating this program So we're actually going to need to do a couple things And actually the first thing I want to do is I want to create variables we're gonna create variables to store The color that the user inputs the plural noun that the user inputs and the celebrity the user inputs we're going to create three variables and these are basically going to be character strings, so they're going to be Collections of characters and we can store them in Variable so I'm gonna create some of these variables. Why don't we create one for color? And we remember whenever we create a String or like a collection of characters? We need to make these open and closed square brackets And what I also want to do because I'm not gonna be giving color of value right away. In other words. We're letting the user Determine the value of color I just need to tell see how many characters we want this String to be able to store that way si knows how much memory it needs to allocate for this variable So I'm just gonna say 20 and we'll basically just say they can enter in a color that is up to 20 characters we're gonna do the same thing for plural noun and again, we'll let them enter in 20 characters maximum and Finally, we're gonna do the same thing for celebrity. So again 20 characters sounds good Alright now that we have our variables created. I want to actually get information from the user so I want to prompt the user for Information and I want to take the information that they entered in and I want to store it inside of each one of these variables First order of business is to prompt them for input so I can just say print F and inside here We'll basically just type in enter a color Once we've prompted them to enter the color we can actually get whatever color they enter and store it inside of a variable I'm going to use a function called Scanf and over here. I'm gonna accept a string so I'm going to accept a string of characters and we're gonna store this inside of our color variable just like that and remember if you watched the last tutorial we use the Ampersand here when we were getting numbers or also you'd do the same thing if you were getting a single character But when we're getting input for a string of characters, we don't need that ampersand so you can just get rid of that Let's copy these and I'm gonna paste this two more times so the second thing we want to get from them is going to be the plural noun and I'm gonna store this inside of the plural noun variable so you can see now we're getting the plural noun and Finally, we're gonna do the same thing for celebrity. And again, we're gonna get that celebrity Okay, cool. So now I'm getting the color. I'm getting the plural noun and I'm getting the celebrity So the last thing we have to do is we have to take all of these variables and put them into our story Right, so we need to be able to print out the story with all of those variables So I'm gonna come down here and I'm just going to say percent s and over here. We'll pass in the color Same thing here. I'm gonna replace the plural noun here with a percent s and we'll pass in the Plural noun and finally same thing for celebrity down here Alright so everything seems to be wired up and you'll notice that I have new lines here so that this story prints out on new Lines, let's go ahead and run this program and we'll see how we did So over here, it's prompting us for a color. Why don't we enter in magenta? Enter a plural noun, let's do Microwaves and enter a celebrity. Why don't we just say? Prince so when I click enter it's gonna say roses are magenta Microwaves are blue. I love prints. So we were able to prompt the user to enter in all of that input We took everything that they input we stored it in variables Then we printed all those variables out inside of our story and we have our Madeleine the program seems to be working really well I do want to show you guys one way that this program could mess up. So let's go ahead and run this again So let's enter in a different color. I'm going to enter in like blue enter in a plural noun So why don't we enter in? phones And now enter in a celebrity so I'm gonna show you guys one way that we could actually break this program If I entered in a celebrity with a first and a last name like Tom Hanks when I click enter now You'll notice that instead of saying I love Tom Hanks. It's only saying I love Tom Here's the problem when we use that scanf function scanf is only going to grab characters up to the first Whitespace, so essentially when we put this space here. We're telling see that we don't want to grab anymore but in reality we want to be able to grab the there's Full-name want to be able to grab the celebrities first and last name if need be? So this is a situation in C where we would have to modify our a little program so what I could do is instead of just getting one variable like the celebrity I can actually get to so I could say over here like Celebrity F and that'll stand for celebrity first name and then down here We can make another variable called celebrity l that'll stand for a last name. So now when we scan instead of just scanning for one string of characters I can scan for two strings of characters and we'll have celebrity F and then celebrity l and Down here. We're gonna want to do the same thing so we can just say celebrity F and Celebrity L and we just need to add another percent here So it's gonna say I love celebrities first name and celebrities last name. So let's run our program and see how we did so I can enter in like red and Microphones and now we can enter in Tom Hanks and we're printing out the actors first and last name So that's one way that we could remedy this program and make it be able to accept two inputs with a space in the middle And it also just shows you guys a little bit more about how scanf works so it's gonna stop scanning It's gonna stop getting the input at that first space now Here's the thing about this program though if I wanted to enter in a celebrity with Who only had one name? So if I only wanted to enter in one the program actually isn't going to be able to handle that so if I said like hats and down here if I said Like Gandhi and I click enter you'll notice that it's still waiting for me to enter in a last name Right, so I could enter in something here and then the program will work But it was waiting for me after I entered in just that one name So that's something that you're gonna have to you know play around with in your programs Basically C is gonna force you to be very specific about what the user is entering so if the user needs to enter two things like two words and you need to specify that if the user is only going to Enter in one word. You have to specify that. So you have to be very specific when you're getting input from the user like that In this tutorial I'm gonna talk to you guys about using a raisin seed a lot of times when we're writing our C programs we're gonna be dealing with a bunch of different data and one of the things we can do to control and manage and sort of keep track of and organize that data is Put data inside of things called arrays an array is really useful because it's basically a container where we can store a piece of information But what happens in your programs when you're dealing with huge amounts of information? Specifically huge amounts of information that are related right imagine I had a list of like a bunch of names or a list of a bunch of numbers that I needed to keep track of Let's say I had like a list of a hundred numbers, right? I wouldn't want to have to create a hundred different variables to store all of those different numbers and this is where arrays come in an array is essentially a Data structure where we can store a bunch of different data values So inside of an array unlike a variable where I can only store one value I could store hundreds or thousands or even millions of values So inside of a single array, I could store like five things or seven things or ten numbers or twenty characters, you know I could sort a bunch of different pieces of information and then all that information would be nice and neat and Organized in my program so I'm gonna show you guys how to create arrays We can create an array a lot Like we create a normal variable and a variable in array are very similar variable is basically used to define a container that Stores a single value and an array is used to define a container that stores, you know any number of values So the first thing we're gonna have to do when we create an array is give C some Information and the first piece of information is what type of data we want to store inside of the array so for example I could say int and now I'd be creating an array that would hold integers If I said char, I'd be creating an array that would hold characters Or if I said like double the same thing but for double numbers, so why don't we create an array of integers? And I'm just gonna call this lucky numbers whenever I create an array I always want to use a special little symbol and this symbol will tell see that instead of just creating a normal Variable we want to an array and it's an open and closed Square brackets whenever I use these open and close square brackets after the name. It's gonna tell C like, okay We want to store multiple pieces of information Now there's a bunch of different ways. We can create these arrays the easiest one though is to just say equals and I can make an open and closed curly bracket and inside of this open and close curly bracket I can just start typing out some numbers that I want to put in this array so we could type out like 4 8 15 16 23 42 Now I can basically type out as many numbers as I wanted and I could store them Inside of this array so over here, I you know, I printed out six numbers I could print out 600 if I wanted You know, the only thing that's limiting me is how many I can actually put in here So you'll notice that I put in the piece of data so like 4, for example And then I put a comma and a comma is gonna separate all the elements in the array So we would say 4 is the first element in the array 8 is the second element in the array These are all considered to be elements Inside of this array and what's cool about this array is it's now storing all of this information So unlike a variable where we can only store one value now in this array. I'm storing 6 different numbers so if I wanted I can print this out and I'm actually gonna print this out just to show you guys so remember, we're gonna print out a Number and I'm gonna show you guys how we can access specific elements inside of this array So all of these numbers are stored in this lucky numbers array in this array structure But the question becomes how do we access them? Right, how can I gain access to one of these? well, I could say present' d so I'm gonna print this out and now I can just say lucky numbers so I'm gonna refer to the name of the array and when I want to access a specific element I can make an open a Close square bracket and inside of this open and close square bracket I want to put the index of the element that I want to access so all of these elements in here have a specific index And I can access them by putting the index inside of this square bracket so if I wanted to access this for for example I can put a 0 inside of here. And now when I run my program you guys will see We'll be able to print out that for So over here, I'm printing out four if I wanted to access this 15 I can put a two in here. So now when I run my program I'll be accessing the 15 as you can see so one thing you might have noticed by now is that we start the Indexes and arrays at 0 so when I wanted to access this 4 instead of putting a 1 in here I put a 0 in here, right? You might think that 4 would be at index position 1 because it's the first element in the array but in C we start array indexes at 0 so actually the first element this 4 is gonna be at index position 0 so if I was going to write out indexes I would say like 4 is at index position 0 this 8 is at index position 1 this 15 is at index position 2 16 is a 10x position 3 etc So that's gonna allow me to access all these elements inside of the array Individually, so I could print them out just by referring to the index inside of these square brackets Another thing I can do is I can modify some of the elements inside of here so Let's say I want to modify lucky numbers in x position 1 so let's say I want to modify this 8 so I don't like this 8 anymore so I can basically just assign this a different value like I normally would so I could say lucky numbers and I can make an opening close square bracket we can say 1 and I can just set it equal to something else so I could set this equal to like 200 and Now when we print out lucky numbers 1 instead of printing out an 8. It's gonna be printing out a 200 so let's go ahead and do that and run my program and you'll see we're printing out 200 instead of 8 so you can modify any of the individual elements Inside the array just by referring to the index and really when you think about this Conceptually an array is basically just holding a bunch of variables, but those variables don't have names So when I access lucky numbers 1 it's the same as me accessing an integer variable It's just that you know, the array is holding potentially hundreds or thousands or millions of these different variables So an array is a very useful structure, especially if you're storing data, that is very similar So, like I said, I could do this with an N I could also do this with a double I could do this with a float I can do this with whatever I wanted But there's gonna be situations where you might not necessarily know all the elements that you want to put inside your array So let's say I'm creating an array and I don't know what numbers I want to put in it yet So maybe I just like want to create it I want to tell C that we need it but I don't want to necessarily put anything in it right away Well up here. We're basically saying lucky numbers that open and close square brackets, and then we're giving it a bunch of information But if we don't want to give it all that information instead I can just put a call a semicolon here but inside of these square brackets I need to tell see how many elements that this array can hold so I need to tell C Like hey, this array can only hold like ten elements or this array can hold 20 elements or whatever So let's say we wanted an array that could hold like ten elements. I can put a 10 right here and now the Capacity of this integer array is 10 so it has the potential to hold 10 items and what I could do is I could come down here and I can start assigning values to those items so I could say like Lucky numbers 1 is equal to 80, right? so I'm giving lucky numbers 1 of value and now over here I could print that value out onto the screen and You'll see we're printing of 80 but if I try to print out for example lucky numbers 0 so the lucky numbers at in X position 0 This is gonna give me a negative 2 which basically means that it's not found. So it basically means that there's no Element inside of lucky numbers at index position 0 but if I wanted I could do that so I could say like lucky numbers 0 is equal to 90 and now when I run my program it's gonna be able to do that So it'll be able to get that value so in a lot of situations You're just gonna want to give these arrays values up front right off the bat, but in a lot of other situations You're not gonna necessarily know what's supposed to go in there But in a situation like that you still have to tell see how many elements the array is gonna You still have to like rigorously defined like hey This array can only hold ten elements and that's just so C is able to allocate enough memory in order to hold all of those Elements, so that's kind of the basics of working with arrays now I do want to point out one thing that we've been using in this course has been strings, so I've been creating character strings so I could say like char phrase and I could set this equal to Giraffe Academy or other times in the program we created Like when we were making our little game we created an arrays and I would just say like 20 and then I would get input from the user and Put that information in here. This is basically a String of characters. So anytime we create a string in C like if I was to create a string over here I called it like array whatever. This is an array So we've kind of just been taking this for granted Like I've just been calling this a string or calling it like a string of characters But it's basically just an array of characters The only difference is this is such a common thing to use in our programs that C makes it a little C makes it special and makes it really easy for us to create it But just like that array of integers. This is also an array So now you kind of have a better understanding of what strings are and what they're actually doing In this tutorial I'm going to talk to you guys about functions in C a Function is basically just a collection of code that performs a specific task So what you can do is you can take a bunch of code, you know maybe like five or six or twenty lines of code and put it inside of a function and then when you want to access that Code that's inside the function You can call the function and generally when you create a function you'll design the function to perform a specific task So the function will have a specific purpose so I'm gonna show you guys how to create functions how to work with them and we'll just talk about the basics of using functions like I said a function is basically just a collection of code that Does a specific task so we can actually create a function here in our C program? Now you'll notice up here. We have this block of code It says int main and there's an open and closed parenthesis and it open and close curly brackets and we've kind of just been using this for the entire course like up to this point the course we've Just been kind of using this main little block of code here. But actually this is a function and Sometimes you'll hear people will call this a method as well. So function and method are two words that Essentially mean the same thing, but you generally in C. We're gonna refer to these as functions But this is called the main function and the main function is Basically a function that's gonna get executed when we start running our program And so we've already been using a function this main function, but if you want we can actually create other functions in our program So make sure you guys have to do that So let's go down and we're gonna go here outside of this function and the way you can tell we're outside The function is we're outside of this ending curly bracket. So down here I'm going to create a function and when we create a function We actually have to give C a couple pieces of information The first thing we have to tell C is the return type of the function And so actually we're going to talk more about return type in the next tutorial But for now just know that return type is basically the type of data that the function is going to return to the user So sometimes your function can actually give information back to whoever calls it in our case though We're just gonna say void and void basically means that this function isn't to return any information That's the first thing that you have to tell see the second thing we have to do is give this function a name And generally when we're naming a function, we're gonna want to name the function according to what it does So in our case, we're gonna create a function that says hi to the user So I'm just gonna call this function say hi because that's what its gonna do. It's gonna say hi Whenever I create a function now I want to make an open and closed parenthesis and I'm gonna make an open and closed curly bracket. So now any Code that goes in between these curly brackets. It's going to be considered code that's inside of this function So let me show you guys I'm just gonna make this a very simple function. We're gonna do one thing inside of here We're just gonna make a printf and we're gonna print out hello user So I've created my function it's called say hi and inside of this function. We're just printing out. Hello user So now let's go ahead and run our program and see what happens. So I'm just gonna build and run my program But you'll see up here that hello user doesn't get printed out right in other words. I came down here I said printf hello user But this didn't get printed out and this is the first lesson with functions is that in order to use the code? Inside of the function we have to call it Calling a function basically means we're telling C that we want to execute all the code inside of it So if I want to execute this code I can Call this function And the way that I do that is just by typing out the name of the function and an open and closed parenthesis So now instead of just printing out nothing C is actually going to go over here and execute all the code in the say hi Function so let's run our code and you'll see now we're printing out. Hello a user So the difference between this main function up here and this say hi function is this main function is a very special function right when we run our program this main function basically gets called but then if we want to use other Functions we can call them from inside of this main function and that's sort of how this is gonna work So also I want to show you guys one other thing. I just want to illustrate The flow that these functions take I want to show you guys how these get executed by the program So I'm printing out top and I'm printing out bottom and I'm calling say hi right here So now I'm gonna run my program again and you'll see we're printing out top And then we're printing out hello user then we're printing out bottom and this might have looked a little bit better if I put new Lines, but essentially what's happening is when C goes to execute this program It's gonna execute this first line of code printf So it's gonna print out top then we're telling C that we want to call this a hi Function C is now gonna jump over here to the say hi function and it's gonna execute all of the code inside of here So in this case, we just have one line of code, but I could put any number of lines of code inside this function So if I have like five lines of code, it would execute all five of those lines of code Then it's gonna jump back up here and it's gonna move on to the next instruction. So that's kind of how that works And that's sort of the basics of writing functions anytime You have a block of code or a bunch of code that does one thing or that you want to kind of? Encapsulate into its own little container that you can call You can put it inside of a function and then whenever you want to access it You just call it, but these functions can actually do a lot more So one thing we can do is we can give these functions some Information so I can actually give this function a piece of information and it can use that information to do different things This is what's called a parameter and a parameter is basically just a value that we give to a function So let's say in this say hi function instead of just saying hi to the user We want it to say hi to someone specifically well I can actually come down here in these parentheses and I can specify a function Parameter and I can specify a parameter a lot like I would specify a variable so I could say for example char name and an open and closed square brackets and I'm basically what I'm saying down here is this say hi function is going to take as a Parameter in other words as an input a string of characters called name and what I can do is I can come down here and I can actually print that out so I could say Percent s and we could print out name now Over here when I call this say hi function because I specified that it's gonna take a parameter I have to give it a string so I could give this a string like Mike like I'll put my name in here when I call this say hi function now, I'm Passing the value Mike into this function So Mike is gonna get stored inside of this name array and we're gonna be able to print it out down here Let's go ahead and run our program and you'll see it says hello Mike. So instead of saying hello user now We're able to customize what it says The cool thing about this is I could copy this line of code Like I could call this function a bunch of times with different names. Actually. I'm gonna put in a new line down here So this is more obvious so I could say hello Mike. Hello, Tom Hello Oscar now when I run my program It's saying hello to each of these different people because we define the function and we can pass it different names and Depending on what we pass it depending on the parameter that we give it it'll perform its task a little bit differently You can also specify multiple parameters. So let's say in addition to accepting the name as a parameter. We also accepted their age So I could say char name And then down here I can specify int age. And now we're basically saying that this is going to take an age as well So I could say hello and then I could say you are and we'll say the age So now over here instead of an addition to just saying name we can also say age so now when I run this Function and when I call it I have to also give this an age so we could say like 40 23 and 70 and now this is gonna print out all that information for us. So hello Mike you are 40. Hello. Tom. You're 23 Hello, Oscar, you're 70 and you can pass as many parameters as you want into one of these functions You always just have to make sure though that you're passing a parameter When you're calling the function just like that. So that's the basics of using functions and in the next tutorial. We're going to talk about This guy over here, which is the return type and we're gonna look at how we can get information back from a function In this tutorial I'll talk to you guys about return statements in C Where a turn statement is a special line of code that we can include in our functions which will allow them to return information back to whoever called them so I can write a function and that can have that function give information back to whoever called it that could be information like the result of some operation or you know It could be it a message telling whoever called it how the function went. It could be anything like that So I'm gonna show you guys Basically how we can do that and I'll just give you an overview of like return types and all that stuff. So down here I'm actually going to create a function and we're gonna create a function that Cubes a number so when you cube a number generally you take it to the third power So I could say like 2 raised to the third power Which is the same as just saying 2 times 2 times 2 this would be cubing a number, right? You're taking it to the third power So I'm gonna make a function that's gonna do that basically This function will accept one parameter which is gonna be a number and it'll cube that number and then once it's done cubing the number, it'll give That value back to the caller and you guys will see how that will work So I do want to point out one thing whenever we are Returning values in our function. So if you're gonna write a function that's gonna return a value You always want to put it above the function that's going to be calling it So for example like this main function down here, this is the first function that executes So any code we put in here is gonna get executed first if I want to return a value with my function I'm gonna define the function here above the main method and there's a couple reasons why we want to do that essentially It'll just make sure that everything works correctly So we want to make sure that we define this function before we actually call it down here and get a value back so the first thing that I need to tell C when I want to create a function is What type of data I want to return if you've been following along with this course You'll know in the last tutorial We use the void return type which basically means we weren't going to return any information But now we want to actually return information So we want to Cuba number so why don't we return a double and you can put any datatype here you want you can also put like int or Character or character array, but let's just do double and we're gonna give this a name So I'm gonna call this cube and I'm gonna make an open and closed parenthesis and inside of this parenthesis We're gonna allow this method or we're gonna allow this function to accept one parameter as input So it's just going to be another double and call it num. So Basically, what we want to do is we want a cube num and we want to give that value back to the user so over Here, I'm just gonna create a variable It's just gonna be another double called result and I'm gonna set it equal to num Times num times num. So basically this variable result now represents or now stores the cubed value of num all we need to do to return this value now is just say return Result and this return keyword is basically gonna do a couple things The first thing it does is it breaks us out of the function. So whatever we put that return keyword It's gonna break us out of the function and it's gonna return this value back to the caller So down here we can actually call this function so actually what I want to do is I want to print out the answer that we get back so I can do printf and in here, I'm just gonna say Answer and we'll print out the answer. So percent F so I'm using % F because we're expecting to get a double back from here remembered double is a return type and Over here. I can just say cube and we'll pass in a number. Let's pass in 3.0 Essentially what's gonna happen now is we're going to call this function All this code is going to execute and this function is going to get a value back basically the value of cubing 3.0. So normally like in the past in this course what we wanted to print on a number we'd have to put like 4 or 76 or you know, whatever we'd have to put a number right there But we can just call this function because eventually this is going to get a number back anyway So this will contain a number after the functions done being called So I can actually just run my program now and this is going to print out the result of cubing 3.0 So you'll see down here we get 27 point 0 0 so 3 cubed is 27 3 times 3 is 9 9 times 3 27 so looks like our cube function works Let's try with another number. Why don't we do 7? So let's run this again and we get 343 I'm guessing that's right So basically what we did is we created this function and we use this return keyword and we returned back to the caller The value of cubing the number and that's really cool And actually we can make this a lot simpler so I could actually just get rid of this Result variable and I could just straight up return Num cubed and this is and this is gonna do the same exact thing so we should get the same answer. Yeah So this can be really awesome And one thing I want to point out is this return keyword will actually break us out of the function So if I came down here and I said like printf and I printed out like here This actually this code is actually never gonna get executed. So if I was to run my program You'll notice that it's not printing out here. So that never gets printed out in other words this line of code down here Print out here. This never gets touched because when we use this return keyword This breaks us out of the function. So whenever C sees this return keyword, it just exits the function and goes back down here to the normal program So just keep in mind that you can't really put any code after you use this return keyword because it will break you out of the function and actually want to talk to You guys about one more thing really quick you'll notice up here I'm creating my cube function above the main function. So I actually created this above here and the reason that I did that was because if I was to move this down here below the main function and I try to run my program you'll notice that we're getting an error over here and actually if we look at the error down here in the Output it says error Conflicting types for cube now Essentially what's happening is when I create this function down here below this main method When the main method over here tries to call it. It actually doesn't know About this function yet like it doesn't necessarily know like what this function is what it's going to return and what parameters it's gonna take because we Created it after we created this main method. So what I can do is actually something called Prototyping and when I prototype it'll allow me to create this function below this main function without getting this error and basically when you prototype I'm basically just going to write out the Function signature, so we would call this like the signature basically the return type and the parameters and the name So if I was to put this up here and put a semicolon now when I run my program You'll see that we're not getting this error anymore, and we're actually getting the answer back So this is a way that you can create functions below that main function without having any problems and you know basically any functions that I created I could put another prototype up there at the top and It's gonna be fine In this tutorial and talked to you guys about if statements in C an if statement is a Programming structure which we can use to help our programs to make decisions by using an if statement our programs can actually respond So in certain circumstances, they can do certain things and in other circumstances They can do other things and if statements are Extremely powerful and they're a great way to add some intelligence to our programs for the purposes of this tutorial We're gonna be building a function. So we're gonna build a function and it's gonna be called the max function Basically what this function is gonna do is it's gonna take two parameters two numbers and it's gonna tell us which number is the biggest so I could give it like four and I can give it a Ten and it'll tell us which number is bigger. Let's go ahead and do that. I'm gonna make this function up here and It's going to return an integer and I'm just gonna call it max and it's gonna take as parameters two integers so it's gonna take an integer num one and it's gonna take another integer num two and Now we'll just make the actual function body so inside this function our job is to figure out whether num one is the biggest or num two is the biggest and we're going To return the biggest to the caller so the first thing I'm gonna do is create a variable and it's gonna be an integer and I'm just gonna call it result and I'm not gonna give it a value right away What we need to do in this function is we need to figure out which one's bigger we need to figure out if num one is the biggest or We need to figure out if num two is the biggest and this is a perfect situation To use an if statement an if statement allows us to check a certain condition And if that conditions true we can do one thing and if the conditions false we can do another thing so down here I'm gonna use an if statement I'm basically just gonna say if and then we're gonna make an open and close parenthesis and I'm gonna make an open and closed curly bracket now Inside of here inside of these parentheses. We need to specify a condition This is something that's either gonna be true or false if the condition inside of these parentheses is true We're gonna execute the code inside of these curly brackets. If it's false, then we're just gonna move on so In order to tell whether or not num1 is bigger than num2 or num 2 is bigger than num 1 we can actually compare them so down here I could say if Num 1 and I can use this greater than sign Num2. I'm basically saying if num 1 is greater than num2 then I want to do something so down here we can set result equal to num 1 if This condition is true. Then we're gonna do this. So only when num, 1 is greater than num2 are we gonna set result equal to num 1 so basically by using this if statement we're able to Tell which one is bigger but here's the thing what if num 1 isn't? Greater than num - what if num 2 is greater than num 1? well I can use something called an else statement so I can come down here and I could say else and I don't actually need to use an open and close parentheses I can just make an open and close curly bracket And the code inside of this else block is going to execute if the condition up here is false so if this condition is false if num 1 is not greater than num 2 then we can just Set result equal to num 2 and then down here. Finally. We're gonna return Result so depending on whether or not this is true result is either gonna set equal to num 1 or it's gonna be set equal to num - So let's go down here into our main method and why don't we call this? So I'm actually gonna print out the result So I'm just gonna say printf and we're gonna be printing out an integer and Over here, I'm gonna call this function. So I'm just gonna say Max and I'm gonna pass in two numbers So we're passing a 4 it will pass in a 10 So I'm passing in a 4 and I'm passing in a 10 and this is going to return to us whichever one is bigger in Other words this should return 10. So let's go ahead and run our program and you'll see over here. We're returning 10 So we got the value of 10 back from this function. Let's try it with the other number So let's make the first number bigger. So now we have 40 and 10 So this should give us 40 back awesome so over here we have an awesome function, right? We're basically checking to see if num 1 is greater than num - if it is, then we're gonna return num 1 otherwise we're going to Num2 and it's important to note that even if I made these equal So if I made it forty and forty, we're still going to get back the correct answer So it's still gonna give us forty back. So this is the basics of using if statements, right? We say if we specify a condition if that conditions true We're gonna execute this code if that conditions false We're gonna execute this code down here And that is an extremely powerful structure in our programs and we can really use this to respond to different input right So depending on what the user puts in num1 and num2 we can do different things But this is just sort of the beginning. So why don't we try to make this function a little bit more complex? So down here we're allowing the user to input two parameters So we're accepting two parameters into this max function num1 and num2 and we're able to spit out which one is bigger But let's up the ante a little bit what if we wanted to accept three parameters? So instead of just passing in two numbers We wanted to be able to pass in three numbers and have the max function spit out the biggest so over here we can modify This max function, so I'm just gonna make another parameter. So I'm going to say int and we'll call this one num three So now this function is accepting three parameters num1 num2 and num three But in order to figure out which one of these is the biggest we're actually gonna need a more complex if statement So I'm gonna get rid of this and let's just start from scratch. So let's think about this How can we figure out which one of these is the biggest? well, what I would recommend is we could say if num 1 is greater than num2 and Num, 1 is greater than num 3 then we know num ones the biggest right? So if num1 is bigger than num2 and it's bigger than um 3 we know it's the biggest if num2 is greater than num1 and num2 Is greater than num 3 then we know num2 is the biggest and otherwise we can just say that num 3 is the biggest so I'm gonna show you guys how to do this and we're actually going to be introducing a couple more Concepts for if statement so I'm just gonna say if and I want to check a condition so I basically want to see if num 1 is greater than or equal to num 2 and num threes so down here I can say num one is Greater than or equal to num2 and now I also want to check to see if num1 is greater than or equal to number three Because if num1 is greater than or equal to num2 and it's greater than or equal to number three We know num1 is the biggest so I can use this special signal here. This is called a logical operator and Basically, this is going to allow us to put another condition in here so I could say num 1 is greater than or equal to num 3 basically what this and does is it allows us to put two conditions so we can check two things and This whole thing is only going to be true if this conditions true and this conditions sure So if one of these guys is false, then this if statement isn't going to get executed so down here I'm just gonna open this up and if this is the case that we want to set result equal to num 1 because if num 1 is greater than or equal to num - and Num, 1 is also greater than or equal to number 3 then we know num one's the biggest. Here's the thing We also want to check to see if num 2 is the biggest So if this is false We also want to check to see if num 2 is the biggest now we can use something called else if so down here I could type out else if and I can make another open and closed parenthesis and inside of these parentheses. We can actually check another condition So if this stuff up here at this conditions false then else if is saying let's come down here. Let's check another condition So here I want to do basically the same thing I'm gonna check to see if num 2 is greater than or equal to num 1 and Num 2 is greater than or equal to num 3 So if this condition is true if num 2 is greater than or equal to num 1 and it's greater than equal to num 3 Then we know for a fact that result is gonna be num - because that's the biggest now finally down here We can just say else and we can make another to open and close curly bracket And remember this code is going to get executed when neither of these conditions is true So here we can just set result equal to num 3 So let me walk you guys through this one more time up here We have this if statement and in addition to checking to see if num1 is greater than or equal to num2 we're also using this special operator called and and we're checking to see if num1 is greater than or equal to num 3 if That's the case then result is going to be equal to num 1 Otherwise if this condition is false Then we're gonna come down here and we're going to check this other condition So we're gonna check to see if num 2 is bigger than num 1 and if it's bigger than num 3 if that's true, then we're gonna do this Finally if neither of these conditions up here is true We're just gonna go ahead and set result equal to num 3 and then we're gonna return results. So this looks good to me Let's test this out and we're gonna see if it works. So now when we call max we need to include three numbers So why don't we just do 1? 2 & 3 and This is basically going to spit out the biggest one, which should be 3 so Let's run our program and you can see over here we get 3 so let's try to make one of the other ones the biggest so we'll make the 1 in the first slot the Biggest and let's run this again and you can see that it returns 10. So this function is working So that's how we can use these ands and we can also use else--if in order to check additional conditions so the and is used to check more than one condition inside of the if Parentheses and the else if is used to check another condition when this conditions false and so that is actually gonna work out pretty well Now I want to show you guys a couple other things There's a couple other things that we can do with these if statements so for example I'm gonna come down here and just show you guys some of these In addition to using that and we can also use something called or so I'm just gonna type out a simple if statement I could say if 3 is greater than 2 and instead of using and like this I can use or and or is basically gonna allow us to Check two conditions, and the whole thing is going to be true if only one of those conditions true so I could say if 3 is greater than 2 or 2 is greater than 5 So only one of these is true, right 3 is greater than 2 that's true but 2 is not greater than 5 so this guy's false, but when we use or One of these two conditions has to be true for the whole thing to be true. So if I like printed something out here like true Now when I run my program we should print true because we're using that or and you see that we do But if I was to make this guy false, so if I made this 3 less than 2 now It's not gonna print out true and we can just print out false. So Now we're gonna end up renting out false because both of these guys are false So you can see down here. We're saying false. So the difference between and and or when we have and like this Both of these conditions have to be true in order for the whole thing to be true and for us to execute this code When we have or only one of these guys has to be true so that's kind of the difference there and I want to show you guys a couple other things that we can use so Over here we're using like a less than sign so for example If 3 is less than 2 I can also use a greater than sign I could use the less than or equal to sign. I Could use also a greater than or equal to sign and if I wanted to check for equality I could use a double I could use a double equals So the double equals will check to see if 3 is equal to 2 we can also use one more which is this exclamation point an exclamation point basically means not equals so I could say if 3 is not equal to 2 then we'll print out true so down here. Let's run this and you'll see that we get true So yeah, there you go. One more thing. I want to show you is how we can negate an entire Operation. So for example, if I said 3 is greater than 2 this is gonna be true right so we'd end up printing out true down here because 3 is greater than 2 but I could surround this whole thing with Parentheses and I could put an exclamation point right before this and this is called the negation operator and it's basically going to negate Whatever this ends up being so this is true And we put this negation operator here this whole thing is going to be false. And so now you'll see Because this is true and we're using the negation operator. We're not going to end up printing out true And you can see we just don't print But if I put a false condition in here, like if I said three is less than two This is false. But because we're negating it now, it's gonna end up being true So when I run my program you can see that we get true So that's sort of the basics of using if statements and I cover just about everything that you can do We can use ands, we can use all these different comparison operators We can use ORS and we can use this exclamation point to negate something In this tutorial I'm going to teach you guys how to build a calculator in see have you been following along with this course? You'll know that in the beginning of the course. We actually created a calculator and it was very basic calculator basically We let the user input two numbers and we took those numbers we added them together and we printed the answer out onto the screen in this tutorial I'm gonna take some of the stuff that we've learned since then and show you guys how we can build a fully functional for function calculator So this calculator will be able to add Subtract multiply and divide and we're gonna let the user decide which one they want to do So we'll let them decide if they want to add or subtract or whatever it's gonna be pretty cool and we're gonna end up using some of the stuff that we've learned recently in the course like if statements we're also gonna use like getting input from users and I'm gonna show you guys how you can get characters as Input from a user as well. So down here. We want to start making our calculator, basically What I'm gonna do is I'm gonna ask the user to enter a number Then I'm gonna ask them to enter an operator like plus minus division or subtraction finally we're gonna ask them to enter in a third number and then we'll Depending on what operator they specify so addition and subtraction whatever we will perform the correct operation and we'll print out the number So the first thing I want to do is create Variables where we can store the numbers and the operator so I'm gonna make some double variables and I'm just gonna call this one Num 1 and then we'll make another double call it num2 finally, we'll make a variable that will store the operator that they enter so this is just gonna be a char and I'm just gonna Call it op4 Operator. All right. So now we want to actually get input from the user I want to figure out what numbers they want to use and then what operator they want to use as well So down here. Why don't we get some input? The first thing I'm going to do is I'm just gonna print out a prompt so I'm just gonna say enter a number And now what we want to do is we want to be able to Take the number they give us and store it inside of one of those doubles that we created So I'm just gonna use this scanf function Scanf and in here I'm gonna specify that we want to get a double So remember when we're using scanf if we want to scan for a double we use F and L F is going to scan for a double and now I'm going to use a comma and we'll put the Variable that we want to store in here so I'm gonna use this ampersand and I'm just gonna say num 1 So this is exactly what we need to do to be able to get the users input and store it into num 1 The next thing we want to do is get the operator. So I'm gonna type out another prompt and I'm just gonna say enter operator basically They'll be entering in plus sign minus sign asterisks or forward slash depending on what they want to do and again now we're going to scan for a Character now when we're scanning for a character and scanf and actually I don't think i've talked about this yet in this course We want to do something special. So I want to put a % and a C. But before I put % C I want to put a space so whenever we're getting a character from the user using scanf, we always want to put a space Right here before % c otherwise, it's not gonna work. And again we want to store this in that Opie variable so I'm just gonna say o P and One more time we're gonna get another number. So I'm just gonna copy this paste this down here and We're gonna get this one for num2 So now we should have all of the input for our program. So I'm getting the first number I'm getting the operator and I'm getting the second number. The last thing we have to do now is actually do the math So we're gonna have to figure out which operator they wanted to use Right, so we have this Opie variable and this is storing like a plus sign a minus sign a Asterix or a forward slash So depending on what that's storing we want to print something Different out so we can actually use an if statement to do this so I can use an if statement to check and see What operator is inside of our operator variable and depending on which one it is? We can do a different Operation so I can say if and the first thing I'm going to do is just check to see if the operator is equal to a plus sign so If the operator that the user entered is equal to a plus sign then we're gonna want to add the two numbers together So I'm just gonna print out these two numbers and I'll just say num1 plus num2 Because we're gonna add both of them together. We can also use an L safe So I'm going to want to check a few other conditions I'm going to check to see if the operator is equal to a minus Sign and if the operator is equal to a minus sign then instead of adding the numbers we're going to subtract them so over here I'll just copy this and we'll paste this guy down here So it's going to be num1 - num2 And I can actually just copy this whole thing and we'll paste it down here. So it makes more room down here We'll paste this other elsif and here we're going to check to see if it's division So we'll check for a forward slash. And again, we're just going to want to print out a Decimal number so it's going to be num1 divided by num2 Finally, we're gonna check for multiplication. So if it's multiplication, then we're going to multiply them together So now we're checking for each of the operators. We're checking for addition subtraction multiplication and division But here's the thing what happens if the user enters in an operator that we didn't want so for example if they don't enter in any of these operators We're gonna want to tell them so I'm gonna have this like print out a little error message We're just gonna say else and basically the code inside this else block will get executed If none of these conditions up here true and down here we can just make a print F and I'm just gonna print out invalid operator So now we have our if statement or if block setup and this should be everything that we need to use our little calculator Alright, so let's go ahead and run this program so you can see over here It says enter a number so we can enter a number. Let's enter in five point nine and Enter an operator we'll add numbers and now let's enter in 4.0 So we're gonna add five point nine and four so when I click enter we should go through all of those if Statements and figure out what we need to do and you'll see over here that we're adding five point nine and four together And we're getting nine point nine. So looks like the program worked. Let's try it again. We'll try another operator Why don't we try to multiply some numbers? So let's multiply six and Times I don't know five point seven so we get thirty four point two that seems about right All right, and then one more time we'll run this and I want to try to enter in an invalid operator So we'll say like 5.7 and I'm just gonna enter in a G. So that's an invalid operator That's not going to work and I'll enter my number. Let's do 8 and you can see it tells us invalid operator so basically we have a four function calculator this calculator can multiply divide subtract and Add and if you don't enter in a correct operator It's smart enough to yell at you and tell you that you have an invalid operator. So This seems to work pretty well. I'm pretty happy with it And you can see down here like this is a perfect situation for using something like an if statement, right? We have this variable opie, right? It's storing some sort of operator We don't necessarily know what's inside of there But I can use if statements and I can respond to the different scenarios So in the situation where it's a plus sign I can respond in the situation where it's a minus sign I can respond etc And this just makes our programs a lot smarter and it helps us to do different things when different situations arise In this tutorial I want to talk to you guys about using switch statements in C now a switch statement is basically a Special type of if statement which will allow us to compare one value to a bunch of different values And it's essentially doing the same thing as an if statement It's allowing us to check different conditions and if different conditions are true Then we can do certain things but a switch statement makes it really really easy for us to compare one specific value to a bunch of other values now I'm gonna show you guys how we can use that in this tutorial to make a little test app So I'm gonna build a little program that will take a letter grade and depending on the letter grade It will tell you whether or not you did good or bad on a test So if you got an a on the test it would say hey you did a good job If you got a B on its house, it'd be like hey you did Alright, if you got an F on the test to tell you that you failed the test so we're basically gonna have a variable where we'll store a grade that you would get on a test and we'll be able to Respond to that grade using a switch statement. So it's gonna be pretty cool. So down here the first thing I'm gonna do is create a variable and it's just gonna be a character and I'm just gonna call it grade and I'm gonna set it equal to a So we're gonna say that whoever took this test got an a on the test and they did really well Now what I want to do is I want to create a switch statement. So I'm just gonna say Switch and I'm gonna make an open and closed parenthesis and inside of this parenthesis I'm gonna put grade so I'm just gonna put grade and I'm gonna make an opening close curly bracket So this is sort of like the basic structure for our switch statement. Now, here's what I'm basically gonna do I'm gonna take this variable here that I pass into the parentheses grade and I'm gonna compare it to a bunch of different values So I'm gonna take this I'm gonna see if it's equal to one value then I'm gonna see if it's equal to a different value or a different value or a different value and Depending on the value that it ends up being equal to we're gonna do something different and these are what are called cases so down here I can say case and I just want to type out a Character so for example, I could put a right here and then I'm gonna put a colon and when I put a right here, basically what this is saying is the case that grade is equal to a then I'm gonna put some code down here that I want to execute so I could just put like a print statement and I could say like you did great because they did great on the test and basically what this is saying is in the case that the grade is equal to a we're gonna print this out and then I want to Put one more thing down here Which is called break and I'm gonna explain what break does in a second So I could make one of these little case statements for every possibility so for every possible grade that we could get so I'm actually just gonna copy this whole thing and I'm gonna paste it down here So now I can put a B in here. So we're saying in the case that the grade is equal to B I'm gonna say you did. All right, and Then we're also going to break and down here. I'm gonna make another one So I'll say case and this time we'll make it C in the case that they get a C. I'm gonna print out portal e and There's a couple more that we can check so we can check if they got a D or an F. So in the case they got a D and I forgot to put a break statement here and I'm gonna put one down here, too And finally, we're just gonna do one more for if they failed the test So I'm just gonna say in the case that they got an F We will print out You failed so Essentially, what I'm doing here is I'm writing out all of these different cases So I'm saying in the case that you got an F. I'm gonna print out you failed and the case they got a D I'm gonna print out you did very bad And then we also have this break statement here and this break statement basically tells our switch statement that we want to leave So whenever we're executing this switch statement whenever C is executing the switch statement It's checking to see if grade is equal to each one of these cases So for example, let's say the grade is equal to a it's gonna execute this print F But if I don't put this break statement in here, then it's just gonna keep going and keep checking these different cases So it's always important to put a break statement in there because I basically just want to break out of this switch statement I don't want to be in here anymore. So finally we can check one more thing in the case that the user Or that we get an invalid grade. So for example, if someone entered in like a t or G or something that's not a valid grade so I can use something called default and Default is basically like an else so default is going to execute whenever none of these conditions up here or Matt so then I can just put something like Invalid grade or something and this will basically be like, hey, you got an invalid grade So let's take a look at this switch statement and we'll just kind of talk a little bit more about what it's doing Basically, I'm passing this in a variable or a value or something and I'm gonna check this value against all of these different conditions so in the case the grade is equal to a We're gonna print you did great and the case that B is equal to grade will print you did. All right, etc and this is basically doing what an if statement doing so I can replicate this entire statement this entire switch statement as An if statement I can write out if etc else--if else--if else--if But this just makes it a lot easier So I don't have to constantly write out all of that if statement stuff So hopefully we did everything right and let's actually check and see how our program does So grade is equal to a let's run our program and see what our message is It says hey, you did great because we got an A if I made this an F instead of an A Now it should tell us that we failed So it says you failed if I made this a C It'll tell us that we did poorly and it says you did poorly So this is a super easy way for us to check one single value against a bunch of different conditions And that's really the point of a switch statement is you're checking one single value like grade Against a bunch of other values and that's where it can be extremely useful So consider using the switch statements They're not appropriate in every situation and there's I'd say like very select few situations where switch statements will come in handy But when they are appropriate switch statements will save you a lot of time and a lot of hassle In this tutorial I want to talk to you guys about strux in C now a struct is a data structure where we can store groups of data types so inside of a struct I could store like an integer alongside of a String alongside a character alongside a double I could sort all these different data types in one single data Structure and there's tons of uses for structs and one of the things we can do with them is model real world entities So I could basically like a model Something in the real world inside of my program and I'm gonna show you guys how to do that in this tutorial We're gonna be looking at how we can use a struct to represent a student inside of our program So imagine we were writing a piece of software that was using like students. So it was like storing records of students or something I could use a struct to represent a student in my program so over here in my program I'm just gonna come up here above the main method and I'm actually gonna create a struct you guys will see like how these work and how we can use them so I'm just gonna say struct and Over here, I'm gonna give this a name so I'm gonna call this Student and a lot of times in C when we're making structs. You're gonna give them a Capital letter just like that and I'm gonna make an open and closed curly bracket now Inside of this struct I can start specifying what types of data are gonna make up a student in my program So basically I can define like different attributes of a student and place them in here And this will kind of act as a template and you'll see how we can use this later So let's think about different attributes of a student Well one thing would be like the student's name so I could say char name and I'm gonna make this a array of characters so this is gonna represent the name and actually why don't we give this a number so I'm gonna say 50 so this String can hold 50 characters maximum and now we're gonna make another one for major So we're gonna want to store the students major and again, we'll make this 50 I also want to make an integer for the students age and a double for the students GPA So I have this struct student and in here I have a bunch of after buttes of a student right have like the students name their major An integer that stores their age and a double for their GPA Essentially what I did was I created like a type of student data type So I basically am allowing myself now to represent a student inside of my program so let's come down here to this main method and I'll show you guys how we can use this so I can basically Create an instance of this student structure so I can create like an actual student Inside of my program and the way that I do that is just by saying Struct and I want to type out the name of the struct that I want to make in our case It's gonna be a student and I want to give this a name. So I'm just gonna call it student 1 and I can just use a semicolon here basically, what I did now was I created a container called student 1 that's gonna be able to store a Name a major in age and a GPA So if you're familiar with arrays in C, you'll know an array is a special structure that can hold multiple pieces of information But all the pieces of information inside of an array needs to be of the same data type and also they don't have names with a Struct I can have a bunch of different data types like this and I can also give them names like name major age and GPA So now let me show you guys how we can assign some values to these So for this particular student student 1 I can give them a name I can give them a major a GPA and an age so I could say student 1 dot age and I can set this equal to something so I could say let's say student ones age is 22 so basically Inside of this student 1 container I'm saying the age of this particular student is gonna be 22 I can do the same for the GPA so I can say student GPA is equal to maybe they have a 3.2 So I'm saying this particular student's GPA is a 3.2. I can also do the same thing for those strings So for the name and the major now, here's the thing about working with strings Remember in C a string is actually just an array of characters and when we have an array We can't like give it a particular value So for example, if I wanted to give this students name of value, this is just an array of characters So I can't just come down here and say student name is equal to something That's not gonna work because you can't do that with an array I can use something called the string copy function and this is a function that will take a String and it'll give it a value that we specify so it'll basically do what we want to do like what we did over here with the age and the GPA but with the name so I could say like Str. C py and inside of here. I need to pass this two parameters The first thing I want to pass is the destination for the string. So we're gonna pass in student 1 dot name the second thing I want to pass in is the String that I want to store inside of student 1 name. So in our case, let's just name it him Jim So now we have the student 1 names value is equal to Jim and I can do the same thing for major so I'm just gonna copy this and we'll come down here and I could say student 1 major and we're gonna set this equal to Business, so let's say Jim is a business major Essentially what I've done here is I've created a student and that student had all the attributes that we defined up here so this particular student had a name a major in age and a GPA and I gave all of those values So I assigned this student 1 an age of 20 to a GPA of 3.2 etc So now what I can actually do is I can print out all these different values that are stored inside of this structure so if I wanted for example, I could like Print out the GPA so I could print out Student 1 GPA and now we're gonna be printing out 3.2. And actually it looks like I have a typo here. This should be student 1 not student Alright so here we're printing out 3.2. So we're printing out the value of the student's GPA I can also print out like their name. So why don't we do that student 1 name and now this is gonna print out So you see we prints out over there So a struct is a really useful structure And another cool thing we can do with structs is we could actually create another Student so I could create like another instance of that student structure. So I'm actually just gonna copy all this stuff I'll show you how we can do this. I'll just come down here and paste it and Instead of student 1 why don't we call this one student 2 and we can set this student's age to be like maybe 20 Maybe their GPA is a 2.5 Let's say their name is Pam and they're studying art So now I have a completely different student And if I wanted I could print out this students attributes so I can print out like student 2 Dot name and now we're gonna get Pam instead of Jim so you can see how that works right there So I could create as many of these students as I want and this is what's cool about Structs is I can just define the basic template for a student in my program and then I can create Individual students down here that I can work with. So now I have this like student variable here I could do whatever I want with it. I could pass it into a function I could print it out onto the screen I could use it in something like an if statement I could do basically anything I want with it It's it acts a lot like a variable or an array so remember variables and arrays or just Containers we can do just about anything we want with them and that's the same with a struct so this has just been kind of an introduction and What you could do as an exercise is think of other things you might want to model in your program So maybe something like a book or something like a phone you could model a phone using a struck basically any type of Information you could store using a struct just like this In this tutorial I want to talk to you guys about while loops in C now a while loop is basically a structure in the C programming language that we can use to loop over and Continually execute a specific block of code until a certain condition is false So I can specify a condition and then I can basically just loop over a bunch of code until that condition is no longer true and Loops can be really useful there's a lot of times in our programs or we're gonna want to let Continually do something while something else is true or while a certain condition Is met and I'm gonna show you guys a really basic while loop in this tutorial and then in the next tutorial We're actually gonna build a really cool guessing game using while loops so it should be pretty fun And you should get a pretty good idea of like how these things work The first thing I want to do And this isn't directly related to creating a while loop though is I want to create an integer So I'm just gonna create an integer called index and I'm gonna set it equal to 1 now What I want to do is I want to actually create a while loop and you guys will see where this index comes into play So in order to create a while loop I can just type out while and open and close parentheses and then an open and close curly bracket And this is basically like the outline sort of like the template for our while loop Basically how this is gonna work is up here in these parentheses I'm gonna specify a condition and as long as that condition is true I'm gonna loop through all of the code inside of these curly brackets So inside of these curly brackets, I'm gonna put a bunch of code. And as long as the condition in these parentheses is true I'm gonna execute all that code so Up here. We're going to specify a condition. I'm gonna specify a condition that index is less than or equal to 5 so while index is less than or equal to 5, I want to execute all the code inside of here So I'm gonna put some code in here. The first thing I'm going to do is I'm just gonna print out the value of index so over here I'm just gonna say we're gonna print out an integer and I'm just gonna print out index so we'll be printing out index and actually why don't we print out a new line every time to There we go. And Finally after this. I want to increment the index variable so I can just say in X is equal to index plus one Basically, I'm adding one onto the index every time and actually I want to show you guys something in C there's gonna be a lot of Situations where you're gonna want to do something like this basically add one to an existing variable and in C there's actually a shortcut we can use so I can come over here and I could just say index plus plus and This is gonna do the same thing. So this is just gonna add one to the index Essentially, what I'm doing is I'm saying I want to print out index and then I want to increment index and I'm gonna keep looping Through this code while index is less than or equal to five So let's go ahead and run this program and maybe you can predict what's going to happen You'll see over here. I'm printing out the numbers one through five So I'm printing out one two three Four and five and let me walk you guys through exactly what's happening in this loop over here we create this index variable and we set it equal to one and Basically when C goes to execute this program the first thing it's gonna do before it does anything else is it's gonna look at this condition and it's gonna check the condition and it's gonna say okay is index less than Or equal to five. Well, the first time through index is equal to one. So it's definitely less than five C's gonna go through It's gonna execute this line of code printing out one Then it's gonna execute this line of code incrementing index Then what C's gonna do is it's gonna come all the way back up here and it's gonna check this condition again so it's gonna say okay is index less than or equal to five and Index at this point is equal to two so it's less than five so it's gonna go through execute this line of code execute this line of code now index is equal to three and C's gonna go all the way back up to the top it's gonna check this condition again So before C goes through this loop It's always gonna it's always gonna check the condition first. So before it's allowed to go and execute that code again It has to check the condition to make sure that it's true Eventually, we're gonna get to a situation where index gets incremented and it gets incremented up to 6 then C's gonna come back up here and it's gonna check to see if 6 is less than or equal to 5 it's gonna be False and we're gonna move with our lives in the program and that's the basics of while loops So this seems very simple and that's because it is all we do is specify condition As long as that condition is true We keep doing all this stuff in here and there's tons of situations where while loops are gonna come in handy This is just one of them where we can print out like numbers between 1 and 5 I also do want to point out one thing That you might want to watch out for which is called an infinite loop and an infinite loop is a situation Where this condition up here never becomes false So this condition will just always stay true and there are certain circumstances where you're gonna want to use something like an infinite loop But in a lot of circumstances, you're not gonna want to go anywhere near something like that So let me just demonstrate so you guys can see so if I stopped incrementing this if I just got rid of index plus plus Now when I run my program you'll see that it's just continually printing out ones I mean look at all these ones that are getting printed out you can see like the text editor is kind of going crazy This program is just it keeps executing through that loop and it's still going right now So this would keep going forever if I let it keep going infinitely. Hence the name, so I'm just gonna terminate this but that'll kind of show you like how an infinite loop works and you definitely want to watch out for that because you'll get into situations as you work with while loops where they'll just become infinite and you you know you need to watch out for That especially if you're doing something a little bit more advanced where you're like siphoning off memory or you're creating processes or something it can Slow your computer down quite a bit. So this is a while loop and this is a really great loop there's also one other type of loop that's similar to a while loop that I want to talk to you guys about and Before I do that, I just want to do something really quick. So I'm gonna set this index equal to 6 All right I'm gonna set this equal to 6 and I'm gonna run my program and what you're gonna notice is nothing's gonna get printed out so the code inside of this while loop Isn't gonna print out because the first thing we always do when we were on this while loop is we check this condition So the first thing C's gonna do before it prints anything before it increments anything it's gonna check this condition and if it's false It's not gonna do any of this stuff So when I run this program, you'll see nothing's gonna get printed out and that's exactly what happens There's another type of a loop that's similar to a while loop Which is called a do-while loop and I'm going to show you how to make one All you have to do is take this wild thing up here and paste it down here below and then up here I'm just gonna say do and A do-while loop is similar to a while loop But the first thing a do-while loop does instead of checking this condition down here. Is it executes all of this code? So even though index is equal to 6 we're still gonna print out 6 and we're still going to increment it and Then we're gonna check the condition to see if we can keep looping So let me show you guys this and this should illustrate what this is doing. And actually I need to put a semicolon here So you can see over here We're printing out 6 so unlike before when we just use that while loop now because we're using a do-while loop We're doing whatever was inside of that loop before we're checking the condition and there's gonna be certain circumstances where a do-while loop will Come in handy. I would say by far while loops are way more common, but just you know, it's good to know about do-while Loops because there are certain circumstances where they'll come in handy Basically any time you don't want to be checking the condition first, you can use a do-while loop But those are the two basic types and there's actually another type of loop that we'll talk about later in the course Which is called a for loop, but for now We're gonna have some fun with while loops and in the next tutorial, we're gonna be building a little guessing game In this tutorial I'm gonna teach you guys how to build a little guessing game and see This is gonna be pretty cool because we're gonna use a lot of the stuff that we've learned up to this point in the course In order to build it, so let's get started basically This guessing game is going to allow the user to guess a secret number so we're gonna define a secret number and we're gonna give the user the opportunity to try and guess it so if they'll try to guess it if they get it right then they'll win the game and if They don't get it right then We'll let them guess again Right, so we'll basically let them keep guessing what the secret number is until they get it so down here We want to create a couple variables The first variable I want to create is gonna be an integer and it's gonna be the secret number So I'm just gonna call it secret number and why don't we just set it equal to five the next variable We want to create is gonna store the user's guess so this is also gonna be an integer and I'm just gonna call it guess and I'm not gonna give it a value yet because Right off the bat. The user wouldn't have guessed a number So what we want to do is we want to be able to prompt the user to enter in the number until they get it Right. So as long as the user hasn't entered in the correct number We want to keep asking them to enter it in in order to do that in order to like keep asking them to Enter it until they've guessed it and we can use something called a while loop So I'm just gonna type while and when making open and close parentheses and an open and closed curly brackets Let's think about a good condition for our game, basically We want to keep asking the user to enter an input as long as they haven't guessed the secret number So it's right there we could keep looping as long as the guess is not equal to The secret number right? and basically this is always going to be true until They guess the correct number now inside of here we want to prompt them to enter their guests and then we want to take whatever they enter and Store it inside of our guest variable. So let's do this first we'll say printf and I'm just gonna say enter a number and Then down here. I'm gonna use Scanf in order to get whatever number they enter and I'm gonna store it inside of that guest variable so I'm just gonna say at guess and this is gonna take whatever number Or whatever integer they enter and it's gonna store it inside of our guest variable So basically what's happening in this loop is every time through the loop we're getting a different number and then we're gonna come up here and we're gonna check to see if it's equal to the secret number if It's not then we're gonna go through again and we're gonna keep doing this But if the guest is equal to the secret number, then we'll break out of the loop and that means we'll be down here So I'm just gonna print out a success message and I'll basically just say like hey you win All right, awesome so you can see this isn't actually that many lines of code I mean, it's probably like ten lines of code to write out this whole thing and it looks like everything's set up So this should actually work properly. So let's try to run this and we'll see if we can guess the secret number So remember a secret number is five So I'm gonna run the program it says enter a number. So let's just kind of botch the guesses two Seven eight nine three so you'll notice I can keep entering in numbers and It'll keep prompting me to enter a number as long as I haven't guessed correctly So now let's type in five And remember five is the secret number when I click enter now that loop condition is going to end up being false, right? Because the guess is gonna be equal to the secret number and it's gonna execute and it's gonna say hey you win, right? So we broke out of that loop we executed the print statement that said you win and the program Terminated and that is a really cool little game so one of the cool things about this game and It's different from programs that we've written in this course in the past is it'll keep running Essentially infinitely until we enter in that correct guessed So it's gonna keep going and keep going keep asking us to do something and to enter in a value Until we entered correctly. So this is pretty cool now This is a pretty nice game and it's it's pretty simple But I want to maybe make it a little more complex one of the things that sucks about our game right now Is that the user gets unlimited guesses, right? So basically like they can just guess every single number and eventually they're gonna get it, right, you know So why don't we do this? We should impose a guess limit So why don't we say like the user can only guess three times and if they can't get the secret number in three tries Then they'll lose the game. So let's think about how we can go ahead and add that functionality Into this little app, I'm gonna walk you guys through how we can do that The first thing we're gonna need to do is we're gonna need to create some variables I'm going to create another variable up here and I'm just gonna call it guess count and Guess count is basically going to tell us how many times the user has tried to guess the secret number and I'm just gonna set this equal to zero because Initially, the user will have guessed zero times so remember this guest count variable is basically gonna get incremented every time they guess Right, so I'm gonna come down here. Every time we go through this loop. The user is guessing So every time we go through this loop, I'm gonna increment the guest count. So I'm just gonna say Guest count plus plus and that means every time the user guesses The guest count will get incremented and we'll have a accurate count of how many times the user has guessed All right the second variable that I want to make is Going to be another integer and I'm just gonna call it guess limit and this is gonna tell us how many times the user Can guess so this will basically determine like how many guesses the user gets So why don't we set this equal to three? I think three guesses is a pretty good limit and finally we want to create one more variable and this is also gonna be an integer and I'm just gonna call this out of guesses and I'm gonna set out of guesses equal to zero and out of guesses will basically Tell us whether or not the user has any more guesses and this might not be super clear what this is doing right now But keep following with the video and you'll see how this comes into play. This is actually gonna be really important So we're actually gonna have to modify our code a little bit so down here we have this set up So the user can keep guessing as long as the guess isn't equal to the secret number But we want to be able to limit the amount of times that the user can guess and actually down here I'm going to create an if statement and every time we go through this loop I want to check and see if the user has any more guesses left. I want to in other words I want to see if they've reached their guessed limit so I'm gonna make an if statement and I'm gonna check to see if the guest count is less than the guests limit as Long as the guest count is less than the guests limit That means they haven't guessed as many times as the limit specifies Basically, they can guess again, right so down here inside of this if statement I'm gonna put all of this code and I'm just gonna surround this with curly brackets so basically the user can only guess the number this code will only get executed when the Guest count is less than the guessed limit when the user has Additional guesses right when they haven't guessed as many times as the limit specifies Otherwise though, so if they have guessed too many times Right if the guest count is either equal to or greater than the guessed limit I'm actually gonna set this out of guesses variable equal to one so I'm gonna say out of guesses is equal to one and Basically what this is going to tell us is if out of guesses is greater than zero Then that'll tell us that the user is out of guesses. So Essentially if out of guesses has a value that's more than zero we'll know that they've run out of guesses and If you ever use any other programming languages before this is what we would refer to as like a boolean variable It's basically storing like a true or a false value, which we're representing with zero or one So if the guest counts less than the guessed limit they can guess again Otherwise we're gonna say out of guesses is equal to one. In other words. We're gonna say that they're out of guesses There's one more thing we have to do though is we have to come up here and we have to change this Condition because right now we're gonna keep looping through this code as long as the guest is not equal to the secret number But there's actually two situations where we want to break out of the loop The first is when they've guessed the number correctly Right, then. We want to come down here and print out you win But we also want to break out of the loop when they've run out of guesses So I want to check to see whether or not they're out of guesses so I'm gonna say guess is not equal to secret number and out of guesses is equal to Z So as long as out of guesses is equal to zero That means that they're not out of guesses and they still have some guesses left But if the guest counts less than the guessed limit and out of guesses gets set equal to one Then out of guesses isn't going to be equal to zero anymore. So we're gonna break out of the loop So basically, there's two scenarios now that will break us out of the loop either They get the word right or they run out of guesses So one more thing we have to do in this program is come down here and you'll see down here We're always printing out you win. So no matter what like no matter what happens We're printing out you win and that's because before if they made it down here if they made it out of that loop That means that they guessed the secret number correctly but There's actually two scenarios Down here where they would have broken out of the loop where there are they're out of guesses or they guess the secret word So I want to actually check to see which one we're dealing with. So I'm going to check to see if Of guesses and I'm gonna check to see if it's equal to one and if it's equal to one That means that they lost and they ran out of guesses. So I'm just gonna say of guesses otherwise though, it means that they won so I'm gonna say You win and this will basically be like hey you won the game awesome So this is a lot of code I know that and let's just run this see how we did And we'll see if our games working properly and then I'll kind of explain all the code to you guys And actually you know what? I just noticed the typo down here. I said out of guesses double equals one This is just have to be a single equals. So we're assigning this of value. So yeah, that was a little typo I had two equals that needs to be one. So let's go over here We'll run our program and it says enter a number. So why don't we try to lose the game? I'm just gonna say two two and now we're on our third and final guess if I don't get it this time That means we're gonna lose the game So I'm gonna enter and you'll see it says out of guesses So we weren't able to guess it within our three tries and the game told us that we lost Let's run it one more time. We'll try to win so I'll say like two one now We're on our third and final guess So if I don't get it this time we're going to I'm gonna guess of five and we guessed right so it's gonna tell us that we want so that's sort of Basically how this little program works and I'm gonna walk you guys through one more time so we have all these variables up here secret number is Just storing the number they need to guess the guest keeps track of what they're guessing each time The guest count is gonna tell us how many times they've guessed the number guess limit is gonna tell us how many times they can guess and Out of guesses is going to tell us whether or not the user is out of guesses. So down here We're checking two conditions for this while loop. We're checking to see if the guess is not equal to the secret number and We're gonna keep looping as long as out of guesses is equal to zero every time we go through this loop We check to see if the guest count is less than the guessed limit If it is then we ask them to guess we increment the guest count Otherwise we say out of guesses is equal to one and that's gonna break us out of the loop down here There's two situations the first situation is that they ran out of guesses the second situation is that they won the game and We're using this if statement to check both of those so that's basically how this works and this kind of shows you how we can use things like while loops and if statements and also, variables together in like one single program In this tutorial I'm going to teach you guys how to use for loops in C now a for loop is a special type of loop that we can use in C which allows us to use something called an indexing variable and this indexing variable will basically tell us what iteration of the loop we're currently on and we can use that indexing variable to do a bunch of stuff like we could Loop through an array of items we could do all sorts of stuff inside of a loop. So down here in my program You'll see that I have a while loop Set up and I have just sort of a basic while loop and I want to show you guys what it's doing. So Essentially up here I'm saying int I is equal to 1 so I'm giving this integer I the value of 1 and I'm saying wow I is less than or equal to 5 I'm gonna print out the value of I and then I'm gonna increment I so this is a very simple wire loop basically, we have this variable I and every time we go through the loop, we're printing it out until it's Greater than 5 so let's run this program and we'll see what it does So you'll see over here. We're basically printing out values between 1 and 5 and I want to point out what's actually happening so the first time that we go through this while loop we're printing out 1 The second time we're printing out to the third time. We're printing out 3 in other words this variable I is Basically telling us what iteration of the loop are currently on so on the first iteration of the loop I is telling us that we're on the first iteration of the loop, right the first time we go through the loop I is equal to 1 Second time we go through the loop I is equal to 2 third time I is equal to 3 So this variable I over here is basically telling us how many times we've gone through the loop So on the third time, it's telling us 3 fourth time for etc, and believe it or not This is actually a very useful thing for us to have when we're looping It's there's a lot of situations where you're gonna want to know what iteration of the loop you're currently on when you're looping so with a normal while loop like I can basically just specify a condition up here and I can do whatever I Want right? It's very open You can kind of just do whatever you want with it but in a lot of situations and there's tons of these situations we're going to have a variable just like I That will tell us what iteration of the loop that we're currently on or you know Basically just a variable that's gonna keep changing every time we go through the loop and this is such a common situation in see that there's actually something called a for loop and a for loop allows us to take all of this code and Condense it into its own single loop. So it's taking this situation where we want to have a variable like I and It's basically allowing us to do it a lot easier and a lot cleaner So I'm gonna show you guys how we can create a for loop I'm gonna come down here below this while loop and I'm gonna create a basic for loop structure So I'm just gonna say for I'm gonna make an open and closed parenthesis and an open and closed curly bracket now the difference between a for loop in a while loop is Basically gonna happen inside of these parentheses. So in the parentheses of a while loop, we have our loop condition or our loop guard Right. This is specifying whether or not we can keep looping through inside this for loop We're gonna have three different things. So instead of just one loop guard We're actually gonna have three different things that we want to put inside of here. So with a for loop the first thing I'm gonna put in here is This variable I so you'll notice in the while loop we have our variable I up here and this is basically Allowing us to loop through and keep track of how many times we've gone through the loop So what I can do down here is I can do something similar I could say like int I and I'm not gonna give this a value inside of here I'm gonna say I is equal to 1 so basically I'm gonna take my variable I and I'm gonna give it an initial value Of 1 and now I have my I variable I have my variable that's gonna change every time we go through the loop The next thing I want to do is include my looping condition so up here We have I as less than or equal to 5, that's our condition. I can put the same thing down here So I'm gonna say I is less than or equal to 5 The third thing I want to do is increment I so you'll notice down here Every time we go through this loop, we're incrementing that variable. I I'm gonna do the same thing over here in this little third quadrant So you'll notice I have these little like sections. Here's the first section We're initializing the very I were saying I is equal to one Here we're specifying our looping conditions so i'm saying we're gonna keep looping while I is less than or equal to five and Over here is a little line of code. That's gonna get executed. Every time we go through the loop So here I'm saying I plus plus I could also say like I minus minus and that would decrement I I could say like I is equal to I plus two Etc. Like I could do basically anything I wanted over here. Let's just keep it at I plus plus for now so you'll see I was basically able to take all of this code and condense it into its own little type of loop and now Instead of having to like print this out and create this variable up here. I can do all of that Inside of this for loop now, I can basically take this line of code I can paste it down into here and we essentially have the same thing So right now this block of code and this block of code are 100% equivalent They're doing exactly the same thing So let's go ahead and get rid of all this code and let's test out our shiny new for loop So I'm just gonna run my program and you'll see over here. We're getting exactly the same thing that we got before We're printing out one two, three, four five. So it's the same exact program doing the same exact thing and that's why for loops are great because we can take that little Structure like where we have an indexing variable and we can use it with something like this so I want to show you another situation where these four loops can come in handy and We can use them to loop through all the elements inside of an array so actually up here I have this array that I created. It's called lucky numbers and I'm just gonna grab this and bring it down here. So We have this lucky numbers array. It has 4 8 15 16 23 42 and What I could actually do is I could loop through all of the elements inside of this array From this for loop so I'm gonna show you guys how we can do that now Let's first off see how many elements we have we have 1 2 3 4 5 6 so we have 6 elements in this an array so what I'm gonna do is I'm actually gonna say I is equal to 0 and you'll see why we need to do this in a Second but basically array indexes start at 0 So this first element in the array is at index position zero and just for a little refresher if I wanted to access one of these elements from inside the array I could say like lucky numbers Was zero and this is going to give me access to this element If I said lucky numbers two, then I'll get access to this element this 15 So this is basically how we can access an element inside the array so I'm gonna set I equal to zero and I'm gonna say I want to loop while I is less than six and Six was how how many elements we had in the array? So I want to keep looping as long as we're less than six and I'm gonna say I plus plus Now down here. I'm gonna do this same exact thing but instead of printing out I I'm gonna print out lucky numbers I So I'm gonna print out the array element at index position. I and lucky numbers Basically the first time we go through this loop We're gonna be printing out lucky numbers zero Because I is going to be equal to zero the second time we go through the loop We're gonna be printing out lucky numbers one because I is gonna be equal to one etc And we're gonna keep doing that Until we get all the way up to five which is gonna be the last element, which is this 42 So let's go ahead and do that. So I need to put this back to I and Let's run this program. So you'll see over here We're basically doing exactly what I said. So the first time through the loop were printing out four We're printing out that first element the second time through the loop are printing out eight, which is the second element 15 16 23 and 40 - so we're printing out all of those elements in turn as we go through this for loop and There's a lot of situations where for loops are gonna come in handy but this is a very very very very common situation where we want to loop through all the elements in an array and either print them out or You know do something to them, whatever so that's the basics of working with for loops and I do just want to say like Anything that you do with a for loop you could do with a while loop, you know I basically showed you guys how we transform that while loop into a for loop the thing with for loops though is that they're very Convenient so it makes it really easy to do something like this without having to go through all the trouble of creating a while loop In this tutorial I'm gonna talk to you guys about two topics in C the first topic we're to talk about our two-dimensional arrays a Two-dimensional array is basically a situation where we have an array where all of the elements in the array are actually arrays themselves The second thing I want to talk to you guys about is nested. Loops So we're gonna look at how we can use a looping structure where we have a loop Inside of loops. It's gonna be pretty cool and actually these two topics can go together really well and I'm gonna show you guys how we can use nested loops and Two-dimensional arrays in order to make an awesome program. So let's get started the first thing I want to show you guys is two-dimensional arrays so down here we can create a 2d array and actually the concepts that I'm showing you in this video can apply to Multi-dimensional arrays so not only two dimensions but three four or five really as many dimensions as your heart desires So in order to create a two-dimensional array, I'm just gonna make an array of numbers So I'm just gonna say int and we'll give this a name. So why don't we just say nums and Normally when we create an array after we'd say the name of the array we'd make an open and close square bracket But when we create a two-dimensional array we're gonna make two open and closed square brackets just like that these two open and closed square brackets will basically represent like the Width and the height of our array so you guys will see what I mean in a second but basically we're gonna have like Elements in the array and then each of those elements is gonna be an array and we'll have elements inside of it So these two squares will allow us to like manipulate all that stuff. So what I'm gonna do is I'm gonna set this equal to An open and closed curly bracket now, this is normally how we would create an array So normally I would just say like 1 2 3 4 whatever I could put all my numbers in here But with a two-dimensional array all of the elements are gonna be arrays. So instead of just saying like 1 2 3 We're actually gonna create Arrays inside of here. So for example, I'll put an array right here and this text editor is so annoying with making these so for example the first element in this array, I could just make an array so I'm gonna say this is like 1 So you can see here. I have two elements Inside of this array which is itself the first element of the nums array. So let's make another one We'll come down here and we'll make another one and this keeps trying to format differently So now we're gonna say three four So this thing right here this whole array, that's the first element in the nums array Right and that array has two elements inside of it. Same thing with this one This is the second element in the numbers array and it has two elements inside of it. So it's basically an array within an array let's make one more and So this is going to be five six So now we have three array elements right one two Three and each of them has two elements inside of them So whenever we create a two dimensional array like this We always have to specify the number of elements and then the number of elements inside each array so in our case, we're going to have one two three elements in the array and Each array has two elements inside of it So it's gonna look like that so we would say three and then two and that's basically how we created So now let's talk about accessing these elements. So I'm gonna create a little print statement here and We're gonna print out some of this stuff. So I'm gonna Say % D and I'm gonna show you guys how we can access individual elements So basically I'm gonna say nums and let's say that I wanted to access this top-left element this one the first thing I want to do is specify the index where The value that I want to access is stored So like this would be index position 0 this would be index position 1 this would be index position 2 so we're gonna say 0 and then I want to specify the index position of the individual element inside of 0 so I could say like this is element 0 this is element 1 so why don't we access element 0 So I print out nums 0 0 and this is gonna print out that Numbers coming out. It's gonna print out that one for us. You can see over here we get that one So let's try a different one. Let's try to grab this for right here. So this is going to be an index position One and then one so this would be at 1 1 and now we should get that for let's see Yeah, cool. So we get the 4 All right so that's basically how we can access elements inside of these arrays and also just want to point out that if I didn't want to give this an initial value I could just like put a semicolon here and I could just like Manually define each index location so I could say like 0 0 is equal to 7 or something like I don't have to give it a Value like right up front although in our case. Let's just do that because it's a lot easier All right, so we have our numbers array and we figured out how we can print out the elements So now I want to talk to you guys about another Concept which is called a nested for loop and you guys will see in a second why I'm teaching this alongside 2d arrays but a nested for loop is a situation where we have a for loop and inside of that for loop we have Another loop so I'm gonna show you guys this really quick. Let's say we create a for loop I'm gonna create two variables over here in I and int J and I don't know if I showed you guys this in the course yet But if I want to just like declare two variables I can just say I comma J and that will declare both the variables. I'm not giving them actual values yet We're gonna do that inside these four loops So I want to show you guys how we can use a nested for loop in order to print out all of the elements inside of this two-dimensional array, so I'm gonna say for I Is equal to zero? and we're gonna keep looping as long as I is less than Three and the reason I'm saying three here is because that's how many elements are inside of this nums array so I'm gonna keep looping as long as I is less than three and then I'm gonna say I plus plus Now inside of these curly brackets, I want to create another loop, so every time we go through this one iteration of this top loop, we're gonna fully execute through another loop so I'm gonna say, for J is equal to 0 J is less than 2 and the reason I'm saying 2 here is because that's how many elements Are inside each array Inside of the nums array and you guys will see in a second. Why? this is gonna work and then I'm gonna say J plus plus so now I'm gonna make some more open and close curly brackets and Down here. I'm gonna actually be able to print out all of the elements inside of this array So I'm gonna go ahead and do that I'm just gonna say printf and we're gonna be printing out an integer because these are all integers and we're gonna be printing out nums and I'm gonna print out nums I and J So whatever the value of I is and whatever the value of J is that's the index position inside of nums that we're gonna print out and now just so this is Formatted a little bit better. Why don't we put a comma? Right there and then I'm also gonna put another printf over here that's just gonna print out a new line And you'll notice that this printf is outside of this inner for loop so I'm gonna run this program We're gonna see what it does. You guys will see exactly what's happening and then I'll sort of walk you through a little bit more What is going on, so? Let's run this program And you'll see over here when we ran the program We're basically getting this entire 2d array printed out. So I'm printed out 1 2 3 4 5 6 So let's talk about why that happens Over here I'm saying for I is equal to 0 I is less than 3 I plus plus So I'm going through this particular four loop three times I'm gonna execute all the code inside of this four loop right here Three times that's as many times as there are elements inside of our 2d array Now every single time I go through this top loop every single time I go through it I'm going to execute this loop in its entirety. So I will loop through this loop all the way through I'll go through all of its iterations and this loop says J is equal to 0 J is less than 2 and 2 over here is how many elements are inside of each one of these raise? So this element in the noms array has one two elements inside of it This element in the numbers array has one two elements inside of it, right? That's where this two is coming from Then I'm incrementing J. And I'm gonna print out so I'm saying print F and I'm printing out nums at index position I and index position J so the first time we go through this for loop I is going to be equal to zero and Remember that first time we're actually going to be going through this loop two times So we're going to be printing out nums 0 0 and nums 0 1 and then we're coming down here and printing a new line the next time we go through this I loop we're gonna come down here and print nums 1 0 and nums 1 1 Finally the third and final time. We're gonna print out nums to 0 and nums to 1 That's basically how this is working. So two-dimensional arrays and Nested for loops are a match made in heaven and there's a lot of situations Where you know besides just looping through 2d arrays that we're going to use nested loops But hopefully this gives you a little bit of a introduction into what they are and why they're useful In this tutorial I'm gonna talk to you guys about accessing memory addresses in C Now in the C programming language a lot of times we're gonna want to store different information Right, and there's a bunch of different ways that we can store information We can use things like variables we could use arrays we could use structs but the basic point is that whenever we're using c we're gonna want to keep track of and Maintain a bunch of different pieces of information and one of the easiest ways to do that is by creating Variables so down here you'll notice that I have a bunch of variables I have this integer called age and it's just has the value of 30 We have this double GPA 3.4 and we have this character grade and it has the value of the a character I have a bunch of different variables in my program and it allows me to keep track of data really easily and I can use these to You know maintain and I can even modify these different informations variables are great But I want to talk to you guys a little bit about how these work in the actual physical memory of our computer So whenever I create a variable for example when I create a variable called age And I give it a value of 30 this value 30 actually gets stored on our computer so all computers have Memory, so a lot of times you'll hear people refer to this as RAM It's called random access memory and basically RAM is the memory that your computer is going to use when it's running programs So for example, if I was to run this C program my computer would use Ram It would use that memory in order to store and keep track of all this different information Right. So when I create a variable like int age C is actually gonna store this value 30 at a specific memory Location so it's gonna take that value 30 and it's gonna store it somewhere in RAM right when I create this double called GPA C's gonna take this value 3.4 and it's gonna store it inside of the physical memory on our computer when I create this grade C is gonna store this character on the physical memory in our computer Now here's the thing when I create these variables I give them descriptive names, right? So I gave this a descriptive name So I know what's stored inside of it, right? And when I want to access this value, I can just refer to the name, right? I can just refer to age I could modify and I could print it out I could do something else to it right I can do whatever I want with it and the way that I can access this variable in other words the way that I can access the value 30 is Just by referring to the name of the variable same goes down here if I wanted to access the grade Right if I wanted to access this capital a I could just refer to the name of the variable But again, here's the thing All of this information is stored in our physical memory somewhere And so whenever C needs to access that information whenever our computer needs to access this value 30 or this value 3.4. It's actually going to refer to a specific memory address Right. So when I want to access this variable age, I can just type out age and then I can use this variable But when C wants to refer to this value, it's not actually using age It's gonna use the memory address where this value is stored inside of our physical memory So all of these values are stored inside of a physical address in our memory So all of these values have an address where they're stored in memory and when C needs to access them it can use that address So I'm gonna show you guys how we can access that address how we can print out the address on to the screen And basically just wanted to give you guys an introduction into what memory addresses were. So down here I'm gonna say printf and I'm gonna show you guys how I can actually print out the physical memory address Where each one of these values is stored on our computer? So down here if I want to print out a memory address. I need to use a % and a P so normally if I was just gonna print out like a number I could say like % D or I can say % C for a Character when we want to print out a physical memory address We're gonna say % P and this actually stands for pointer. We're not gonna talk about pointers in this tutorial We're just gonna talk about memory addresses But just know that you need to say % P and now I'm going to type out the name of the variable whose memory I want to access so I'm gonna access the memory address of age and Before the variable. I'm just gonna say ampersand so I'm gonna make an ampersand age and I'm gonna say percent P and Now we should be able to print out the memory address where the variable age is stored In other words, we can print out the memory address where this value 30 is stored. So let's go ahead and run our program and You'll see over here. We're getting this number So I'm getting this number 0 0 6 0 FF 2 0 0 so this would be like a hexadecimal number I guess it's not technically a number. It's like I think it's hexadecimal But basically this is the physical memory address where the value of 30 is stored in other words It's the place where C stored the value inside of this age variable I could do the same thing for these other variables so actually why don't I format this a little bit I'm gonna stay here age and then I'm gonna make a new line and we're just gonna say GPA and again, I'm gonna print out another one of these addresses and we'll do one for grade and We'll print out the address so now I can just come over here and I can sort of modify this a little bit so I can just say GPA and Grade and you'll notice for each of these I'm using this ampersand before I type out the name of the variable So now when we run our program we should get a nice little list of all of these variables and their corresponding addresses so you'll see over here we have age and it's stored up memory address 0 0 6 0 FF OC GPA is stored at this memory address and grade is stored at this memory address so if you were to like go into our computer or go into my computer and Go to memory address is 0 0 6 0 FF 0 C that's actually going to store the value of 30 it's gonna store the value that's inside of that age variable If you were to go to this memory address you would see the value Inside of GPA if you're going to this memory address you would see the letter which was a capital A so basically all of these variables are store at Different memory addresses when I want to access the information in the variable I can just refer to the variables name so I could say a age or I could say GPA But when C wants to access the information and the variable it has to use these Addresses so C would use this address C would use this address and this address? I can just use the variable names and that's why C is awesome because it allows us to do stuff like that But when C and when our computer wants to access these values it has to use these memory addresses and so that's why these are useful and When we're programming and C it can actually be pretty useful to be able to know the Addresses like the actual physical addresses of these certain variables in memory So a lot of times in C when we want to work with these Variables we can just refer to them by name and we can do all sorts of stuff with them We can access them we can modify them. But in other circumstances, we're not just gonna want to be able to access the value We're also gonna want to be able to access their physical memory address in other words we're gonna want to be able to know like what that address is and we can use that for a bunch of stuff and In future tutorials, I'm gonna talk about why that can be useful, but for now I really just wanted to kind of give you guys an overview of what memory addresses are How we're using them in our program and how we can print them out so again I just use this ampersand and then I type the name of the variable and I use this percent P to print it out In this tutorial I want to talk to you guys about pointers in C Now a pointer is basically just a type of data that we can use inside of our programs So up until this point in the course, we've been using a bunch of different types of data, right? we've been using things like integers and an integer is just a whole number we've been using things like doubles and a double is a decimal number we've been using Chars and a char is basically just a character and now I want to introduce you guys to another type of data which is called a pointer and a pointer is basically just a memory address and a memory address it basically refers to a you know a physical address inside of the memory of our computer where we're Storing a value right and I think pointers Tend to confuse a lot of people who learn about them and this is sort of one of those topics and see that everybody dreads Because it's you know, traditionally very confusing to understand but actually pointers are Extremely simple and the problem is that they just get over complicated So what I want you guys to do is I just want you guys to think of a pointer as a type of data That's all it is. It's just a type of data that we can work with in our programs It doesn't have to be any more complicated than that, right? I can work with an integer in my program an integer is a whole number, right? I can work with a double in my program a double is a decimal number I can work with a char in my program a char is just a character I can work with a pointer in my program a pointer is just a memory address That's it. A pointer is just another type of data that I can use and I can work with Inside of my programs and it just happens to be a memory address inside of the memory or the RAM in our computers That's that's all it is It's very simple people overcomplicate pointers and they don't have to be over complicated because it's just another type of data Just like an integer or a double but instead of being like a whole number or a decimal number. It's a memory address So hopefully that makes sense and I hope you think you guys don't Try to over complicate this too much because it doesn't have to be so down here in my program I have an integer that I've created and it's called age. So I just said in age and Inside of this integer. I'm storing a whole number I'm storing an integer right third And if you've been following along with this course in the last tutorial We talked about memory addresses and we talked about how all of these variables are actually storing these values at Physical addresses on our computer's memory so down here I have this print statement and I'm just printing out ages memory address and I'm using this percent P and I'm just printing out Ampersand age and we talked about how this ampersand when I put this in front of the variable basically It's going to give me the physical address in memory where this variable in other words where this Thermia value is stored. So if I run this program you guys will see over here. It says ages memory address and it's this Hexadecimal number right? This is the physical memory address of eight the age variable What did we just talk about before remember what I told you pointers are? Memory addresses that's what they are it's a type of data a pointer is a type of data in our program and It's a memory address right when I use this ampersand and I type out the name of a variable This is giving me the memory address right I'm able to print out the memory address so really this is a pointer This is a type of data in our program. That is a memory address just like an integer is a type of data That's a whole number and a double is a type of data. That's a decimal number and a char is a type of data That's a character a pointer is a type of data. That's a memory address and this right here This guy right here is a pointer. So it's just the memory address of the age Variable that's it and when I want to print out a pointer I can use this percent P and It'll allow me to print it out onto the screen and everyone's happy So essentially what I'm doing here is I'm printing out a pointer when I say percent P I'm telling this print F function that I want to put on a pointer and then I give it a pointer over here I give It a physical memory address. That's what a pointer is now We understand and hopefully understand that a pointer is just a type of data, right? It's just a memory address and We can access the memory addresses of specific variables by using this ampersand and then type The name of the variable and that can be pretty useful Now that we understand that I want to show you guys how we can work with these pointers so over here I have an integer variable called age and this integer variable is storing an integer value 30 I'm gonna say that one more time. I have an integer variable and this integer variable is storing an integer value 30 I could also create a double variable so I could create a double called GPA and Inside of this double variable. I could store a double value like 3.4. I could also create a char variable Called grade and inside of this char variable. I can store a character a I could also create a pointer variable and Inside of that pointer variable I could store a pointer Okay So just like I stored a character inside of this character variable and I stored a double inside of this double variable I could store a pointer inside of a pointer variable so we can actually store these pointers inside of a pointer variable Now here's where this is going to get a little bit tricky When we create a pointer variable we actually need a physical memory address right so when I create this Integer variable like I can just come up with a number right off the top of my head, right? I can just say like 30 when I create a Double variable I can just store whatever number I want to store but when we create a pointer variable remember We're gonna be storing a memory address and I don't necessarily Know any memory addresses like right off the top of my head at least. I don't know any meaningful memory addresses So when I create a pointer variable, what I want to do is store the memory address of a variable That's already in a program. So I'm gonna say that one more time when I'm creating a pointer variable inside of that pointer variable I'm gonna store the memory address of a another variable inside of our program so let's say that I wanted to create a pointer that would store the memory address of This variable over here so thats or the memory adjusts of age, right? I can create a pointer just like this so I could say int and asterisks and now I'm gonna give this a name so I'm gonna give this Pointer variable a name and remember this pointer variable is gonna store the memory address of the age variable and generally when you're naming a pointer, you're gonna start with a lowercase P and Then you're gonna type the name of the variable whose memory address you're storing in the pointer variable So I'm gonna say P age right because this pointer variable is storing the physical memory address of the age variable Now what I want to do is set this equal to the memory address of the age variable Which we can access using this ampersand so I could say ampersand age so now this pointer variable is Storing the memory address of the age variable I'm gonna do the same thing down here for this GPA So if I wanted to create a pointer variable that would store the memory address of the GPA variable. I could say double asterisks and I'm just gonna say P GPA just like that and I'm gonna set this equal to the memory address of This double variable which we can access using this ampersand and then the name of the variable like that I'm gonna do the same thing down here for this character variable So I want to create a pointer variable which is going to store the memory address of The character variable so I'm just gonna say char Asterix a grade, and I'm gonna set this equal to Ampersand grade. Okay, so that's all I'm doing, right? So now I have this integer variable and I have this pointer variable Which is storing the memory location or the memory address of the age variable I have this double variable which is storing a double and I have this pointer variable which is storing a pointer and The pointer just happens to be the memory address of the GPA variable I have this char variable down here And then I have this pointer variable, which is storing a pointer which just happens to be the physical memory address of the great variable in our memory Okay, and that's basically all you need to know about pointers to get started a pointer is just a type of data It's just a type of information that we can work with in our programs it just happens to be a memory addresses write an integer is a type of data that we can work with in our programs and it just happens to be a whole number a double is a type of data that we can work with in our programs and it's a decimal number a char is a type of data that we can work with in our programs and it's a character a Pointer is a type of data that we can work with in our programs and it's a memory address Hopefully that makes sense and just like we create integer variables and double variables we can also create Pointer variables the only difference is whenever we create a pointer variable We're gonna store the memory address of another variable in our program So so you'll see over here when I create this pointer variable. I'm using the data type of The variable whose address I'm storing so over here when I'm storing the address of an integer variable I'm saying int here When I'm storing the address of a double variable I'm saying double here when I'm storing the address of a char variable I'm saying char here and that's the basics of using pointers and Also creating pointer variables and storing memory addresses inside of variables In this tutorial I want to talk to you guys about dereferencing pointers in C Now a pointer is basically just a type of data that we can work with in our programs. That is a memory address So sometimes in our programs we're gonna want to work with memory addresses We're gonna want to use them in our programs and do certain things with them and those memory addresses are basically called pointers in our programs and when you dereference a pointer Essentially what you're doing is you're going to the memory address of that Pointer and you're grabbing the piece of information from there. All right So all points are as our memory addresses and when we dereference a pointer We go to that physical memory address and we get the information or we get the value that's stored inside of there so I'm gonna show you guys how we can do that and see it's actually really easy and can be pretty useful so Down here. I have this integer and It's an integer variable and it's storing the integer value 30 and over here. I have a pointer variable which is storing the memory address of 30 so it's storing the memory address of our age variable as you can see right here and Down here. I'm just printing out this pointer So I'm printing out pH I'm using this percent P and I'm just printing it out to the screen So why don't we take a look and see what we get. So you'll see over here We just get this memory address probably exactly what you expected to get so over here I want to show you guys how we can dereference a pointer So this pointer variable P age is storing the memory address of this age variable, right? So when we printed it out We basically were given the physical memory address where this numbered 30 was stored So P age is storing the memory address of 30. It's storing the memory address where this value is Inside of our computer's memory if I want to dereference a pointer Basically, what I'm gonna be doing is I'm going to be going to that actual physical memory address and grabbing that piece of information So the way that I can dereference a pointer is by using the asterisks So when I'm printing this point, I can dereference it by using this asterisk But now over here since I'm dereferencing this pointer I need to print out a number instead of a pointer because when I dereference the pointer This is no longer a pointer. This is no longer a memory address now This is gonna be whatever was stored at the memory address. The pointer was pointing to so this is going to be an integer because this pointer Variable was storing the address of an integer Right, so when I dereference it This is actually gonna be an integer So let me show you guys I'm gonna print out or I'm gonna run our program and it should print out 30 So you'll see that we get a 30. So basically what's happening here is we're dereferencing the pH Pointer variable we're dereferencing the pH pointer which basically means we're going and grabbing the actual, you know Physical value that's stored at the memory address that the pointer was pointing to and that's why we get 30 And that's basically what dereferencing is, you know, you're you have a pointer You can dereference it and you basically just get the value that it was pointing to It's essentially all we're doing so I want to show you guys how we could do this Just normally so for example I have this age variable if I just typed this out If I wanted to get the address of this age variable I could use this ampersand, right? That's basically what we did up here but what I could do is I could just dereference this so I can say ampersand age and then I can put a asterisks here and this is actually gonna dereference that entire thing So now when I run my program it's gonna print out 30, so it's not gonna print out the memory address It's just gonna print out 30 so you can see over here. We get 30 so I could actually do this like as many times as I want and this will maybe just illustrate what's happening so I could put An ampersand here and now I'm gonna get that memory address again So you'll see we're getting this memory address and I can do the same thing so I could dereference this one more time and You'll see now we're getting that 30 value again so I mean basically what I'm doing I'm constantly like like here I'm getting the memory address where the age variables stored Then I'm dereferencing and getting the value then. I'm getting the memory address again, and then I'm dereferencing it again So, you know, hopefully that makes sense when you dereference a pointer you're basically just getting the value that that pointer is pointing to or you're getting the value that's stored in the memory address of the pointer In this tutorial I want to teach you guys how to write two files in C One of the cool things we can do in C is we can actually modify and change and also create new files So in this tutorial, I'll just basically show you guys how to do that We'll talk about creating files writing files and appending on to files So let's go ahead and do that down here in my main function We're actually gonna create a file. So I'm going to create a file and I'm gonna store some information inside of that file and the way that we can do that is we can just type out file all in caps just like that and essentially what we're doing here is we're creating a pointer to a physical file on our machine, so I'm just gonna say file and I'm gonna make an Asterix and I'm just gonna give this a name So I'm just gonna call this F Pointer and that'll just stand for file pointer, and I want to set this equal to F Open an F open is actually a function that's gonna open a file for us And so inside of these parentheses, I'm gonna give this a couple of different parameters The first parameter is gonna be the name of the file that I want to open. So I'm just gonna call this employees dot text and the second parameter is gonna be a File mode and a file mode is basically just going to tell C What we want to do with the file that we're gonna open And there's a bunch of different file modes. The three most basic types are R and R stands for read So if I put R in there, I mean we want to read the file W stands for right so if I put right in there it means I want to either create a new file or write over an existing file and There's also a which stands for append and that means I want to append information on the file in this tutorial we're gonna look at W and a so what I want to do over here is put in W and Right now employees dot text. This file doesn't actually exist So what I'm gonna do is I'm actually going to create that file We're gonna put some information inside that file and then that file gets saved onto my machine and in order to do that We're gonna need this write mode So it's just going to be a W Anytime that we're open file in see you always want to make sure that you close the file so over here I'm saying F open And down here I want to use another function called F close and Basically what this is going to do is it's going to close off the file so it's gonna remove the file from the memory on our computer and if we do make any changes it'll kind of like save the file and Everything will just be kind of like locked in so you always want to make sure whenever you're working with a file that you close It and inside of this F close function. We just want to pass in this F pointer So I'm just gonna say F Pointer right there. So let's talk about what's happening here file is basically just like Essentially like a data type I guess and we're creating a pointer to a physical file on our computer So if you remember a pointer is basically just a memory address Right a pointer is a type of data that we can use in our programs. It's essentially just a memory address and this pointer is storing the memory address of this new employees dot text file that we're gonna create so Essentially what's going to happen is we're gonna create this employee's dot txt file and F Pointer is going to be pointing to the memory address of that file on our computer So that's essentially what's happening here. And what I want to do is I actually want to write some information To this file. So actually let me just show you guys really quick. What's happening here So without doing anything else just using this line right here when I run my program a new file called Employees dot text is going to get created so over here I'm in my little directory and If you don't specify an absolute path or like a relative path for this It's just gonna create it in the directory that your C files in So if I wanted to create this on my C Drive, I could say like C Whatever and then like employees out text and this will get created in my C Drive, but if I don't specify like an absolute path Or even if I don't specify a relative path then this is just going to get created in the same directory as our C file and You'll see over here My main dot C file is right here. It's in this folder So when I run this program, let's watch over here and we can see this employees doubt text file get created So I'm gonna run my program and you'll see over here that this employees file got created So if I actually bring this out a little bit you'll see it's a text document Its employees dot tax and there's zero kilobytes inside of this so we haven't put any information in there But this file got created. So now let's talk about putting some info into that file inside this file I'm basically just gonna list out a bunch of like employees that might work in an office and The easiest way to write information to a file is just to say F Print F and this is a special Function in C, which actually works a lot like the printf function So if you've used the printf function and if you're following along with this course We've been using it throughout the entire course the printf function basically allows you to like print information out onto that console window F Printf is it can allow us to essentially just write information to a file? I need to give this a couple parameters. The first one is gonna be this file pointer in other words I need to tell this function, you know what it should be writing to so I'm just gonna say f pointer and Then over here we can just give this some information so I can just use like after we put in this file pointer. I Can just use this like I normally would the printf function so I could just print out like some employees So I could say like let's say one of the employees is Jim And we'll put like what they do. So like I can say sales men and then we could say actually let's make a new line and we'll create another employee Pam and she is the receptionist and then we can make some other employees so So now I have three employees that I'm going to basically write out to my employees file. So now when I run this program and We can go over here to this employees file You'll see when I open this up that we have these three lines in here So we have Jim salesman Pam receptionist and Oscar accounting So basically I was able to create this file and write all this Information in the file from down here just from down here in this program. So that's pretty cool And I want to show you guys with right so when I'm using this, right? This is actually going to overwrite a file. So for example if I change this to from all this text and I just put like Overridden in here now when I run this program and We open this employee's dot text file. So let me reopen this You'll see it just says overridden so I've actually overridden all the text that was in there And that's basically what happens when we just write to a file. So let's go back to what we had before I'm just gonna run this again. So we have this text And you'll see now we're back with the employees so let's say that um inside of this file We wanted to add an employee on to the end of it So we wanted to add like a new employee on to this file, right? Well, I can use something called append and what append is gonna do is it's going to allow us to append a new line on To the end of this file or actually just append text on to the end of the file instead of saying w I'm just gonna say a and you'll notice I'm still keeping employees text here and down here inside of this F printf I'm just gonna print out another employee into the file The first thing I'm gonna do is print out a new line because and actually let me show you guys this in this employees filed There actually isn't a line after this last line so the last like position in this file is over here I can't actually get on to the line below Oscar So I'm gonna want to go ahead and print a new line and this will move the next employee on to the next line we could say like Kelly and she's gonna work in customer Service. So now when I run my program instead of overriding what was in the employees text file now? This is just gonna append this new employee onto the file. So I'll come up here run the program And let's open this employee's file and you'll see now we have another entry Kelly from customer service. So when we write a file We either create a new file and add content onto it or we just override the content in the file At least using this F printf function and when we append, we're sub pending information onto the end of the file so just using those two things, I mean you can essentially write out any you want and it's important to note that you can write more than just text file so I could write like an HTML file or I could write like a CSS file you could write really I mean any type of file that you'd want and That's basically how we can write to a file and append it to the end of a file In this tutorial I will not teach you guys how to read the Information from a file in C in the last tutorial we talked about writing to files So we looked at how we could create a file overwrite a file and append information on to a file in this tutorial I'm gonna show you guys how you can read Information from a file so down here you'll notice that I have my file pointer setup So I just say file and the asterisks and I'd called it FP and remember this is just a pointer which you know and a pointer is just a Memory address and it's the memory address of this file in Memory, so it's the memory address of this employees dot txt file which we created in the last tutorial and I'm using this F open function which basically Opens this file and then I'm specifying a file mode We talked about the W file mode and the a file mode now I want to talk to you guys about the our file mode and this stands for read So when I use the our file mode I'm actually able to read information from the file and this can be pretty cool So in order to read the information in the file I'm actually going to show you guys how we can read like the individual lines of the file So actually first let's look at the file This is employees dot txt. It just has you know, basically a bunch of employees their names and then what they do and We're gonna be able to read this file inside of our program so the first thing I want to do is create a variable and I'm gonna create this variable up here before we create the file pointer and it's just gonna be an array of characters and I'm just gonna call it line and I'm gonna give this an initial size of 255 so be able to store 255 characters and then down here in Order to read this file. I can actually read the file one line at a time and We can use a special function Called F Gets and F gets basically is going to allow us to read the lines of the file one by one so I could say F Gets and I need to give this a few different parameters the first parameter I need to give it is a place to store the information that it gets So this function is going to go and it's going to read the information from the file and it's going to store it somewhere So the first parameter we need to give this function is where it should store that information so in our case We're going to store it inside of that align variable that we created up there. The second thing we need to give this is a size and this is basically just gonna be like The maximum size that we can read in so I'm just gonna say 255. Basically this number should match this number up here and Then we also want to give it that file pointer so I'm just gonna say f Pointer and basically what this is gonna do is it's going to read the first line of the file and it's going to store it Inside of this line variable. So if I was to come down here and say print F and print it out a string and Just print out line now when I print out a line It's actually going to be have the value of the first line in the file. So let's run this program and You'll see over here. We're printing out Jim salesman And if you didn't remember that was the first line inside of that employees dot text So basically what F gets is doing is it's reading the first line. And another thing that it's doing is it's moving this pointer so remember this pointer is Essentially pointing to where the file is in our memory and whenever we use F gets this pointer is gonna get incremented So it's basically gonna move past the first line in the file So if I was to use this again? For example if I said f gets again when I use this It's gonna store the value of the first line of the file inside of line And when I use it again, it's now gonna grab the second line in the file. So if I print this out now This should actually be storing the second line in the file. So when I run my program you'll see it's storing the second line Pam Receptionist I can basically do this for all of the lines in the file So that's basically how we can use F gets to read the individual lines inside of a file