C Programming Crash Course all-in-one Tutorial (3 HOURS!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody it's Caleb welcome to your C programming crash course this is going to give you everything you need from writing your very first program all the way up to memory management structs and pointers and everything in between now I did break it up into chapters and sections so you can get the timestamps for those in the description below if you enjoy the content please be sure to subscribe and leave some comments below let me know what you think and what you'd like to build with C programming now right before we jump in I want to give a special thank you to our sponsor that's doing a lot to change the world using technology call for code call for code is a global challenge for developers to create open source technology to help in times of natural disaster created by the David Clarke cause with founding partner IBM call for code is bringing hundreds of thousands of developers together to build solutions using technologies such as blockchain AI and IOT with winning projects receiving cash prizes projects support from the Linux Foundation and more natural disasters are among the world's greatest challenges with 2.5 billion people directly affected since 2000 and you have the power to help will you answer the call learn more by following the link in the description all right so let's get into C programming welcome everybody to your C program and crash course my name is Caleb and let me tell you I am super excited to go through this course with you guys I think there's gonna be lots of valuable information and it's definitely not gonna be a waste of your time so thank you for giving me the chance to teach you guys C programming let's try to make C programming fun and exciting some of the things we're gonna be covering in this course are these things down here and I'm going to basically be doing this in a crash-course manner so I'm going to go over a topic and basically teach you what you need to know and then there will be a syntax reference guide for each of the sections that you can use for future projects I'm not gonna waste your time with fluff but I'm also not going to just teach you something really quick and not really explain it my goal is to explain it but not overdo it and get to the point right away and hopefully by the end of this course you have lots of practical examples that you can use for future projects or at least the bare minimum I hope you guys can understand all these concepts and understand and the syntax to make things work so we're gonna start with the basics of how to compile how to make a C program run and then we're gonna quickly get up into arrays and strings move on to functions and creating our own functions and then we're gonna go into some other more intermediate and advanced topics of structs pointers and memory management it's gonna be pretty super awesome and I'm really excited okay so you guys can't tell so the very first thing you need to do is you need to get a text editor or a code editor I'm using Visual Studio code I think it works great but you're free to use whatever you want you can look up and you can find people talking about which ones are best the next thing you're going to need is a compiler now inside a Visual Studio code you can actually access the terminal which is where I'm gonna be doing all of our maura all of our compilation and stuff but if you don't have Visual Studio code you can just open the normal terminal app by going to your apps now this is being ran on a Mac things are going to be minorly different between windows and linux mainly difference it mainly different if you're using windows but if you're on linux it's gonna be very very similar if you're on Windows you might want to look up how to get C programming going in Windows and then once you got that down come back and start applying these principles taught in this course but basically what we're gonna do is around type GCC and that is the command and there were the program used to compile code now when you get this no input files that's a good thing that means GCC is installed if you type in GCC and nothing happens or you get a a warning or you get basically instructions on how to download it you're going to want to follow those in order to get GCC installed very simple you guys shouldn't have any issues but I'm sure if you do you can easily find solutions online to the problems once you have GCC installed you are good to go and you can move on to the next video where we're gonna be talking about some of the main linux commands that are good to know for this course now if if you want some more the background of C I'm gonna be talking about some of these key things that I put put in this note here and basically just trying to explain C programming at M higher conceptual level so you understand the upcoming content the very first thing is that C is is a compiled language that means you take your code and you compile it down to an executable that means all of the syntax in your code has to be legal it gets parsed for to see if your code is a legal code and if it's not then it's not going to be able to compile and you'll get a compilation air-sea is one of the oldest programming languages and is actually still actively used today it inspired many of the more modern languages of C++ c-sharp Java and so forth now you may be more familiar with some of these languages because of school or maybe you're more familiar with Python and a lot of these are object oriented well c programming is not object oriented the other thing is that c is statically typed that means whenever you create a variable and you're gonna work with some data you have to choose a data type at compile time and that variable is basically tied to only storing that type of data and the thing you guys need to know is that C is not forgiving what that means is that it's very easy to make mistakes and half the time the compilers not going to say anything are you gonna get some crazy error like segmentation fault and you're not gonna have any idea what to do so this is something you're going to have to get used to and I'm gonna try and give you some good tips throughout this video series next thing I wanted to talk about is that C is very hard to pick up at least it was for me but it's definitely not too hard once you get the hang of it it just takes a little bit more time to wrap your brain around some of the potential issues and challenges that usually you don't have to worry about in languages like Java for example once you get into strings well strings are a huge pain in the butt and C and you'll see why when we get there but it's definitely not to the point where it's too hard and if you're struggling I just want to encourage you to keep at it and you will get it especially once you have the the syntax reference guide because you can literally just copy and paste a lot of those examples and fill in some different variables that you're working with rather than using mine so other than that see is basically very similar to the modern languages it has all of these capabilities which many of the modern languages have all of these so it's not going to be radically different and you're gonna see a lot of syntax similarities between C and some of these other languages like C++ and so forth so with that be sure to check out the next video I promise you guys is gonna be awesome and I really hope you guys aren't disappointed as you go through this if there's anything you think is confusing please be sure to let me know and I'll consider it for future iterations of this course or particular videos so thank you guys and I'll just I'll see you in the next video hey Oh welcome back everybody this is going to give you what you need to start working in the terminal on Linux or Mac now if you're on Windows I'm sorry but basically we're going to learn how to do anything you could do clicking on the finder or in a folder structure we're gonna learn how to do that in the terminal and it's going to be the same on Mac and on Linux because Mac is actually a UNIX operating system so very similar in nature so all the commands are going to work on both of those so the first thing you might want to do is basically figure out where you are in the file structure so basically try to think of the terminal as a command based way to navigate folders also known as directories and copy things and do everything like that so basically anything you could do with the mouse you should be able to do in the terminal so the first thing we can do PWD and that's going to give us the absolute path to where we are this basically defines the folder structure so if you were clicking you would go into users you would select Caleb go to the desktop and then open the C folder the the technical name is directory so I'll try not to say folder too much but you can use the terms interchangeably so when you get into this folder the first thing you might want to do is actually see what's in it so you can use LS for list and this is going to basically just spit out all of the files in this folder and these are the things we're going to go through in this course now obviously you're not going to have all of these unless you downloaded the source code in that case you may have a similar structure to this now there's something in the terminal known as a flag and that's when you put a hyphen and a letter so for example LS dash L that's lists long that's going to give a long version of basically the same thing but now we have permissions some file sizes and dates and a bunch of other crap another one is LS LS dash a which is all and it's going to include a little bit more specifically this dot and these double dots the single dot refers to the current directory so if we need to reference the current directory that's how we do it if we need to reference the parent directory one folder up then we use the double dot so for example if we do PWD desktop would be dot dot right now and that's relative so if we went to a different directory the parent is always going to be relative to where we are so it's not like these double dots is permanently referencing desktop just in this case so what you can actually do is you can combine these flags so for example you can do it LS Ella LS dash L a and that's going to do both of them so now it's going to give the long version and it's going to include these two here now that a the only thing that really really does is it will include all files so if you have hidden files that normally don't show up when you're when you're going through the folders using a Finder window well the a is going to expose those here now if you want to change directories there's a command CD and just to show you how you can reference the parent I could go CD dot dot and that's going to move up to the desktop so now when I get our path we're just in desktop so that was an example of giving a relative path in the sense that the the location that it goes to depends on where we are you could also do a complete path or an absolute path like this CD - users - Kayla or slash yeah that would not be a - that would be a forward slash desktop / see so those are some of the basics now there is another shortcut you guys should probably know and that is this tilde that always references the home directory and the home directory is the folder you're in when you open the terminal so that's going to be I'll just show you CD home that's going to take me to my user folder so Caleb you can also change directories using relative paths more than just up a parent so for example if I go back to that a/c folder let's say let's go into the C folder there I could say CD parent parent and what that's gonna do is that's going to go up to the desktop up to Caleb and then put another slash and if you hit tab twice it's going to list the possibilities and we could go into docker for example and now we're in a docker folder so basically we were able to go up to folders and then look at the contents to choose a new folder in that one - change directory - basically you just have to understand that you can do more complex change directories it doesn't have to be the same simple ones all of the time moving on to some other commands there's an important one you might want to know you can use clear to clear the window touch and this is going to create a file and you're gonna give it a name like so file name dot C before I do this though I do want to change back to the C folders so I'm not polluting my docker folder here so what I'm gonna do is I'm gonna go to users slash Caleb and then I'm going to go into desktop and then see now I'm going to touch file name dot C and when I list you should be able to find file da see right there awesome so once you create a file you can always verify that you created it using the LS command now what if we wanted to move a file so let's say this file we didn't mean to put it in the C folder we just wanted to put it on the desktop what do we do then well what we can do is we just say move mV and we say the original one named Aussie and then we say where we wanted to go so we could say move it to the parent now there should no longer be that file name here but if we list the contents of the parent directory it should be in there so it lists the parent directory and we have file name dot C awesome now what if we want to create a directory so we have this C folder what if what if we want to make a new one well you use the mcdr command make directory and you can just give it any name and then that should show up now as a directory when we when we list and we are actually still in I forgot to change directory so we're actually still in the the C directory so we created this inside of the the C folder so if you wanted to delete that it's really easy you just say RM awesome and you're going to actually have to pass a flag when you're deleting a folder and that is - are but just to show you when I try it says awesome is a directory but that - are we'll get rid of it alright so we're currently in that C folder we can make that directory in the Desktop folder by going up one and then just saying mcdr awesome and now it should be in the desktop as awesome right there and we could of course change we could navigate into that CD awesome and you can see that there's nothing in there you can also make multiple folders at once so for example you could say mcdr folder 1 folder 2 and they will both be created now there's a useful command echo that's basically going to print whatever you put in here back into the console which obviously seems kind of useless right now but you can do things with this so for example you could say echo hello world and you can direct that output into a file such as output and that's going to create a file output and you can see the contents of a file using the cat command I'll put hello world so that's pretty cool now if you do it again so for example if I say hi put that into output now if we look at the contents of that file you can see that it replaced hello world with high if you want to append to the file you need to use 2 greater than signs so you can say like this now let me say cat output it says hi there which obviously I messed up because when you do this it also has the newline characters so this one plus a new line and then the space there so obviously I failed but you get you guys get the point you can actually output any command to a file so for example if you don't want the output to be spit here you can put it to a file so I could say LS - la and I could direct that to a file LS output for example now if I say cat LS output it puts all that information there now there's actually another cool command you might want to know and that is less so this is another way to look at files but it doesn't give you everything at once and when you have a huge file it's it's going to allow you to basically scroll through this and when you're done you can press Q so that basically gives you the absolute basics of Linux obviously there's not a strong order to any of this I just kind of spit a bunch of commands at you but honestly just use the the reference guide because that's going to give you what you need when you're trying to do something specific so thank you guys hopefully that was helpful for you guys and please be sure to check out the next video because we're going to be creating our first C program and talk about basic input and output so it's definitely gonna be fun I'll see you guys then hey everyone welcome back this is the video where we're going to create our first C program by the end of this video you should be able to tell everyone that you're a C programmer put that on your job apps and we're not going to get into anything too crazy but this is gonna really help you out if you're a beginner if you're not a beginner some of this might be pretty simple but I still think it's good to go through it so when we're creating a C file we need to give it a name and then ended with dot C and that is that we're going to reference this file name when we're compiling so the very first thing you should do is worry about the shell of the C program so what I mean by that is anytime we create a C program we're going to have a basic structure that is pretty similar for all of our C programs and it looks something like this we're going to have pound to include and then we're going to use standard IOH so an include statement just allows us to use functions from this library here then we're going to create a function and it looks like this and you don't have to worry about all of this if you're a beginner but essentially the main function is going to execute when our program is ran and the return basically just says everything went okay which is what the zero means in this case okay so we got our shell now what we need to do is we need to compile so to compile you use GCC and then you put the file name 1.3 and then once you've got enough to use put tab and it'll fill out the rest okay so what now well what happens when you do that is it actually creates a file a dot out in your folder and you can execute this by typing dot forward slash a dot out and you see it does nothing which is fine because our program actually doesn't really do anything it just starts and then ends so it's working so that's great now this a dot out is kind of a funny name you can change that so for example you could say GCC and then use this o flag and give it a name such as tacos and then we could say 1.3 put the filename and now we have this file tacos so you can call that the same way dot ford slash tacos and you can see it still does nothing so that's good but I'm gonna get rid of tacos and we're just gonna use a dot out for this okay so let's actually make a program that does something typically the first program you're going to make is a hello world and you're going to do that with the printf function which is just a simple function to print things to the console and in here you're going to pass a string which is indicated with a double quote at the beginning in a double quote at the end all right so just how it is now it's going to print hello world and then immediately print this right after it without even a space so what I would like to do is put a backslash n so this is going to be rendered as a newline and it's just gonna make things a lot prettier so we can compile now and anytime you make a change that you want to see in the output of your code you need to save the file so command S and then you need to compile so every change you have to compile so don't forget that or what you're going to be doing is you're going to be execute your gonna be executing old code and you're not going to see any of your changes so you need to compile and if you have to continually execute that you can just press up to get some of your previous commands which makes it a lot easier for you you can see a printed hello world whoop-whoop so there is your official program you guys are all now certified C programming experts ok let's do some more complicated stuff let's talk about variables and we're gonna get a lot into variables in future videos so I'm not gonna really explain a ton of this or getting a ton of detail but creating a variable looks like this and essentially what that does is it allows us to use this this name X throughout our program to reference the value 50 so this X is known as the identifier it's the name so an identifier is literally just a fancy name for name so when we when we reference X or basically when we're when we're using X it's going to give us the value 50 it just makes our life a lot easier being able to store data in these variables and get access of access to them using the identifiers so we'll see that some upcoming this here is actually a two-step process though we're doing two things at once we just don't see it here so let's create another variable int Y and what this is this is the Declaration we're basically just saying hey here's a variable Y the next part is the initialization and this is done with the assignment operator so assignment operator it just takes a value and stores it in a variable super super simple and there are a lot of other operators we're going to talk about in the upcoming videos so that is your basics with variables now what I want to do is I want to talk a little bit more about print statements and just how to make them a little bit more complicated using these variables so for example we can say printf and the very first thing you need to know is that you can use format strings to basically parameterize your strings and allow you to input different values in different spots so if that makes absolutely no sense let's just recreate this string using a format string so it's gonna look like this percent s yeah and then we'll say world okay so what in the world is going on here so this S here can basically take any string and print it right here and all you have to do is put that value after this entire string with a comma so we could put in here hello so this is kind of confusing but basically what's happening is this is going to be put here with this s is it's going to get rid of this percent s and put hello and then world is going to show up there so let's just see this in action make sure you save you see a prints hello world it looks exactly the same but we can actually use this concept to our advantage and print variables so for example I can print the you of X and to do this we actually need to use a percent D and then a comma and then we just put X and always try to remember to put a new line it just makes it a lot easier the value of x is 50 so now you can kind of see we can reference the value 50 throughout our program even if we had this line 20 times and we had X 20 times well we could change the value of x in one location and it would update throughout our entire program so it makes our lives a lot easier and you can expand this concept to multiple variables so for example I could say X is percent D Y is percent D and then we can pass in those two variables and we're on that we get X is 50 Y is 10 awesome so those those are like the most basic things that you're going to do and you might be wondering what is up with this percent s and this percent D how do you know that it's an S or a D well this is known as the conversion characters so s is going to do strings D is going to do integer data so you can actually check out the syntax reference there's going to be a list of conversion characters that you can do but the most common ones are going to use are s and D so that is that now that you are a printing expert the next thing you're likely going to want to do is get user input so this is going to allow us to make our applications more dynamic so we could ask the user questions so to do this we actually need to use a function scan F and it's similar to printf but there's a a little bit of a difference I'm going to be talking about so this is going to take to two things by the way these two things are known as arguments when we're passing data into one of these functions they're known as arguments so the first one is going to be that string the format string and then the next one is going to be a variable the variable that we want to store the value in so we should actually make a variable ahead of time before calling the scanf so for example let's say we wanted to get a ver a value for a radius or something it doesn't matter you can you can name it whatever you want this name is just for our use here so what we can do now is we can put radius here but we can't just leave it like this we actually have to use this ampersand symbol which is a capital seven the and sign and this is a little funky and it takes some time to get used to but basically what's going on here is that anytime we need to change the value of a variable we have to use this + sign this is known as the address of operator and it basically gives a pointer and we're going to get into pointers later so don't get overwhelmed I'm trying I'm trying to balance out the content here for beginners as well as people who do know some of this stuff so if you're a beginner and you're and you're getting swamped just just go through this a couple times and if you know all this then just watch it and go on to the next video so anytime we want to change the value of a variable we need to have the address of operator and that's because inside of this function the only way for it to be able to change the value of radius is if it is actually a pointer to radius and that'll make a lot more sense once you're taught once we talk about pointers the exception of this is when we're working with arrays you don't need to put the + sign there you don't need to use the address so operator but in this case we're just working with an integer variable so it should be good and as a general rule anytime you ask the user for input you should tell them so they know what to do just like this and then what we can do is we can actually print this value out so you could say something like you chose the value percent D for the format and then we can just say radius all right let's compile see if we messed anything up looks good prints hello world twice print some other stuff give me a radius and I could say 734 press ENTER and it says you chose the value 734 so this is pretty cool because this gives us the opportunity to make our program do different things based on the input from the user and that'll make more sense once we chop talk about branching and control flow but for now just being able to get user input and output and output that input is is going is very true very helpful to us the next thing I wanted to talk about is strings so I'm just gonna get a little bit into this strings in C can be a huge pain in the butt when you're starting and even even after using them for a while they're they're just kind of a pain and it takes a lot of time to get used to so a string looks like this this is an example of a string it's just a sequence of characters and encapsulated in double quotes so if you wanted to store something like this in C you actually have to use a character array and an array is just a collection of data points so something like this so what we're doing is we're making a character array of 20 characters now the important thing to know with C you raise is that you need to reserve one character for what's known as the null Terminator and it looks like this backslash 0 and what this does is it indicates the end of the string and this always needs to be there or else things can go very bad because it's used to know that the string is done or that the string is over and if by chance you over over wrote this on accident and put a character there well then some of the functions are not going to be able to tell that your string is over and it's going to start accessing areas of memory that you're not supposed to access so that that's bad you don't want to do that so what that means is we need to leave one spot that means we can only store 19 characters in this string so when we say scanf to get someone's name we're going to use the percent s to say hey they're going to pass us a string but you're going to want to specify that you want only 19 characters and that is how you do that here and then we're going to store that inside of name now if you remember the previous scanf we use the address of operator we don't have to do that here because it's a character array and anytime we're using an array it's automatically going to be editable inside of the function because arrays decay to pointers and we'll talk talk about that a ton when we get into pointers maybe not a ton but enough enough to be enough all right so let's try this and then let's just print out the name here so % s all right give him a radius now it's asking for the name I forgot to do a prompt Caleb and it says your name is Caleb great so it's working and if you want you can you can add a prompt ask for a name but I'm just gonna go on so this video should have helped you get the basics of input and output as well as working with variables and operators and even some strings Oh hopefully this video has been helpful let me know what you guys think of the course so far in the next video we're gonna be talking about shoot totally just forgot we're gonna be talking about variables and datatypes and just get some more hands-on experience with those so be sure to check that out thank you guys welcome back everybody this video we are going to be talking about variables and data types now the very first thing you're going to want to know is that C programming is what's known as a statically typed programming language and that is in contrast to what's known as a dynamically typed programming language so what are the differences so a statically typed programming language like C you have to say what type a variable is when you declare it so for example a is of type integer and that means a can only ever store an integer in a dynamically typed language variables are not tied to a datatype so you can assign it one type and then later switch the type that is stored in the variable each Swan has its ups and downs so I'm not gonna get an argument of which is better and they're just different and that's what you really need to understand the one of the benefits of statically typed variables is that you get more errors when you compile verses logical errors when you run the program so in a dynamically typed language and maybe this is getting a little bit into my opinion personally I find that I get a lot of issues with types and not getting the values that I was expected because during the program's execution the type of the variable is able to change whereas for a statically typed language I'm not able to do any kind of changing of types and if I do I'll get a compiling error so I am able to catch more of the airs early on before executing the code but different people have different preferences so yeah that's all I'm gonna say about that so we have integer here but what are some of the other types well there's double and that's literally just basically the same thing but now you can have a decimal afterwards there's also float and this is slightly different than a double and I'll explain that and the main thing is that a double is a 64-bit number and a float is a 32-bit number so when you're representing numbers in binary the the more bits you have the the more precise you're able to represent a number so for example you can do this you can actually show that it's a 64-bit number by I'm going to use percent D here but I'll show you that you're not actually gonna want to use percent D here but you'll see in a second you're gonna you want to use % Lu but if you use the wrong one it'll still it'll it'll let you know and when you're compiling so you can use this sizeof function and put in B and when you compile you get this warning and it says hey you need to use % Lu I just wanted to call that out just because it's kind of cool so you don't have to worry too much about the conversion characters because the the compiler can help you out with that but in this situation you definitely want to use Lu because it's a on the signed long and unsigned just means that it's only positive so now we can execute this and we get the value 8 so what does 8 mean well it's 8 bytes and in in a byte is 8 bits so 8 times 8 is 64 now if we do the same exact thing but we use C instead now we're gonna pass and C this is going to give us the value 4 there we go we get the value for so that is the the difference between flow and double generally I use double all the time the only other time the only time you'd want to use float is if you're really constrained in memory size which I only ever program on a desktop computer that has plenty of memory so I never run into those kinds of issues but it is kind of important with C programming because often C programs will be executed on much weaker devices so definitely understand the difference between double and float all right so what other types are there well there's character so this is going to look like single quote a and this is kind of confusing because the name is D and we're giving it the value a but a character just stores one letter the next one we have is an array string I guess a character array and is this aka string and then the last one I have for you guys is bool and this is either true or false now when you do this you're going to have to use this standard bull H include otherwise it's not gonna recognize this alright so those are the the main data types that you should know about the next thing I wanted to talk about is just legal identifiers so once again an identifier is just the name of the variable and you can see that these all are legal because when we compile we don't get any issues but if I did something like int I don't know 5 cats this is not going to work and that's because we're starting our variable name with a number which you can't do so in general you want to use just characters you can put a number inside of the variable name or at the end and that works fine too so that's okay and when it comes to capitalization it's important to note to know that cats and Katz is not the same thing so same for this that's different as well any kind of capitalization differences is a different variable because C is case-sensitive meaning that when we change two key words or words to different capitalisation it makes a different word so for example int in all capitals is not the same thing as int in lowercase so I think you could even do this which you wouldn't want to do but I'm just showing you that this is not the same thing as the lowercase version if you did the lowercase version it wouldn't work because you're not allowed to use keywords as identifier names you can start them with identifier names but you're not you're not able to use the full keyword so all of these are legal legal identifiers but they're not all exactly the most recommended so what I recommend when it comes to just pure convention like what you should do I like to start with a lowercase and use camel case which is where the first word is lowercase and then all the other words start with an uppercase letter so cats are okay that would work let's guys say cats are awesome but then I was like dogs are kind of cooler yeah all right so that's just a little bit on identifier names and next thing I want to talk about is just what's known as implicit type conversion and it's just important to know really some of the basics of this and if you really need to get into the details you can but the thing is that it's possible for C to implicitly meaning you don't have to tell it to change the type of values to to meet some expectations so for example if I did int X actually let's go with in 0 equals 0.99 9 9 9 9 this is interesting because we're storing a decimal value but it's being stored into an integer variable so what exactly is the end result well I hope you got the hint that it's going to be 0 trip on correct and then we just pass in the zero variable make sure you put the return at the bottom you should still be able to run this because it's only a warning and you can see that zero is zero so that is an example of implicit type conversion the next thing I wanted to talk to you about was typecasting and this is more explicit in that you tell the compiler that you want to do a caste versus the implicit where it just does it for you automatically and honestly the only way to truly understand anything in computer science is to use the example of ordering a pizza and splitting it amongst all of your friend so let's say we get a pizza and it happens to have 17 slices I know it's such a weird way to cut a pizza button you know and we're gonna split that between me and someone else now if we do something like this slices per person the way you would figure out the answers you know you take 17 and you split that between the people and you know you would expect getting hmm and what you would expect it's not what you actually get just so you guys know so you would expect to get nine eight freek can do math sorry guys I was put on the spot because I'm making videos and therefore my brain just shuts off and I can't think about normal simple math problems but basically what what you would expect to get in real life if you had 17 pieces of pizza and you split it between two people you would get eight and a half slices each but it's not going to work that way in C programming because these are both integers and when you're dividing an integer by an integer and C you're actually only capable of getting an integer so the result is actually going to be 8 so if we took that and we printed it out LF for a double I had to think about that one for a minute we still have that this warning is from that earlier implicit type conversion I'm going to comment that out so it doesn't bother me every five seconds alright so we get the value eight point zero zero zero zero zero so you can see that it's still stored as a double but the actual value would be capable of being stored in an integer because all it is is eight so in order to do this we have to do explicit typecasting so the way we do this is we need to in parentheses to put the keyword double and this will cast slices to a double so now it's not going to be seventeen it's going to be seventeen point zero and the important thing to realize here is that we're changing the value but we're not changing the variable slices sorry that's kind of confusing way to explain it the type of slices is not possible to change so slices is going to remain a 17 integer and this anytime we do typecasting like this it only affects the value in the expression there we go and we get eight point five so that is how you do explicit type conversion and the important thing to realize is that this is what's known as a unary operator so once we get into operators it'll make more sense but it's only going to work on one thing so if we put it next to slices it works on slices if we were to do something like let me get rid of this comment like this that's still not going to give us the value because double is only going to work on one thing and whatever's in parentheses is evaluated first so this is going to evaluate to eight and then it's going to be typecast it to double and give you 8.0 which is what its gonna do anyways because it's being stored as a double so we're gonna cast slices to double and those parentheses there are totally fine yep there we go this expression is using double division because slices was cast into a double but the thing it to know is that if we had a large expression with lots of integers it's not going to use integer division for all of it only the parts where one of the operands of this division operator is a double so if that doesn't make any extent any sense by me explaining it let's just go through a simple example and we'll see what we get so let's say we have test1 and we take the values 25 divided by 2 times 2 and I want you guys to think about what this is going to output now test2 is going to be 25 divided by 2 times 2.0 aha got you there ok so I'm gonna give you the answer both of these are going to evaluate to 24.0 and this kind of just shows that just because we have a double in here it doesn't mean that all of the expression is done using double arithmetic so let's go through how this would be evaluated it's going to look like this when it's evaluated so 25 divided by 2 is going to in because these are our integers it's going to give us 12 and then we take 12 multiplied that by to get 24 and then that's stored into a double so we get that point 0 the other one it's going to do the same exact thing 25 divided by 2 is 12 multiply that by 2.0 we get 24 point zero and that's stored in test2 so what exactly am I trying to show you here the main thing that I'm trying to show is that when there is a double in the expression like 2.0 it only affects the multiplication that it's attached to so basically this times this using double arithmetic so hopefully that is a clear as mud if you wanted to fix this you would do something like this so that's create some new variables so you could do 25 point 0 divided by 2 and then multiply that by two that's going to give us 25 alternatively you could use the the typecasting like shown in the the previous example right here so you could say double 25 either one works fine whatever you prefer divide that by two and then multiply that by two or 2.0 so now the the important part the part that is giving us a funny result this right here is now using double division thus giving us a double result so these should both evaluate to 25.0 not integers because they're being stored as doubles so just to prove my point I'm going to put these print statements in here and just see what the values are test for I'm sorry test 3 25 test for 25 and to prove that these two were actually 24 we could switch this to 1 & 2 and we get 24 so hopefully I didn't go too much into detail there the the main thing you guys got to get from this is that you got to be super super careful when we're dealing with expressions and the mixture of doubles and integers if you ever want to get a double result you probably want to make sure that all of the the operands and your giant expression are of type double all right so that's all I got for you guys in this video hopefully it was clear and helpful and I'll see you in the next one welcome back everybody this video we're going to be talking about operators now I figured the best way to explain this would actually just be to go through the code examples and just go through the examples and explain why we get the results that are given rather than me trying to type everything out because it says a lot of print statements so the first thing you're going to notice and this is that there is a ton of operators if you're new to operators it can be slightly overwhelming but what I'm just going to tell you is that none of the operators are extremely complex or difficult it just takes time to go through the entire list so give it time and it'll come with with a little bit of time and effort so the very first thing is that you can actually categorize operators by the number of things that the operators work on so for example if you have something like 5 plus 5 well and like you can assign this to something in X equals 5 plus 5 for example well this is the example is an example of an expression so operators are used to create expressions and the operators work on what are known as operands so these 5s are operands so you can see that the plus has two operands that means it is a binary operator now there's actually unary operators binary operators and ternary operators which work on three there's one of those and when are you talking about that later which that one is actually the last one on this list the conditional operator which uses three operands so that's one way to categorize it but you can also categorize it sort of by use so plus a minus arithmetic increment decrement assignment and so forth so ever it helps you guys to think about it just understand that usually grouping things into groups helps you to organize it in your brain and to truly understand now the plus and minus here these are unary meaning it works on one operand this is not the plus and minus in the sense of addition and subtraction this is plus and minus in the sense of positive and negative the next we have the arithmetic which is used to do math we have the increment and decrement which increases or decreases the value of a variable by one we have the assignment operators we've talked about this assignment operator here on line 25 but we haven't gone through all of these which just allows us to do some more complex examples which we'll be talking about later in this video now the comparison and logical and conditional we're going to be talking about in the future once we start getting into if statements and other more complex programming concepts but for now we're just going to start with the basics and then getting these will be very simple but just to give you the basics of these these comparison are going to compare two values and give you true or false as a result so this one's going to compare if something's equal this one's gonna see if something is less than something else and so forth the logical operators allow us to make more complex comparisons and lastly the conditional operator is kind of like an if statement which we'll be talking about here shortly alright so that was kind of like my preamble now on to the actual good stuff the the first thing you should understand is the concept of precedence now precedence is taught in school when we're talking about math so for example typically if you have multiplication and all week wrap my head I or conditioner just came on and scared me you have parentheses I totally lost my train of thought so if you have multiplication and addition in the same expression well the multiplication typically happens for in like normal math well the same thing happens in C programming and you can force different precedence using parentheses so that's the whole concept behind precedence if you want a good reference here is one for you guys let me show you this real quick so you can see that all of these operators have precedence one meaning they happen first these ones have precedence two and so forth so I have an example of that here where we have two times and then we have these parentheses so we're basically using the parentheses to decide which way we want this expression to be evaluated if we didn't use parentheses it would default to something so like let's try this two times three plus three the default is that because multiplication has the highest precedence compared to the addition these two will happen first so it will evaluate like this one down here now in this scenario these parentheses are not actually doing anything the only thing it's doing is that is helping it be more clear to the programmer so if you want to be more explicit in showing how the expression is going to be evaluated even if the parentheses are not needed they can still be extremely helpful just to prevent bugs or incorrect evaluation of expressions so I definitely recommend using parentheses whenever you can you can use them to both force precedents such as in this case we're forcing the three plus three to happen first or you can use it just to indicate how the expression is going to be evaluated so that is up to you how you want to use those but I recommend using parentheses anytime you can so in this case X what is the value going to be three plus three is going to happen first which is going to give us six and we're going to multiply that by two which is going to give us 12 so X will be 12 and then Y is going to be 2 times 3 which is 6 plus 3 9 and you can see that when we print these out those are the exact values we get 12 and 9 the next thing I wanted to talk about is the modulus operator and if you scroll up in our reference here that is actually in the arithmetic the first four in here are pretty simple and I just decided not to cover them so this is addition subtraction multiplication or division sorry and multiplication those are very commonly understood so I decided to pass the only thing is you might need to get used to these characters if you're used to the more mathematical symbols so this is the forward slash and the asterisk capitulate the only one we haven't really familiarize ourself with in school is this modulus which is the remainder of some integer division so let's look at that so what happens is in this example we take 10 you can consider whatever you want it to be ten slices of pizza divide that amongst three people and whatever is left is what is assigned to see and you can't split the slices of pizza so this is integer division so we're going to evenly be able to divide nine slices of pizza amongst three people and we're gonna have one left over so ten modulus three is equal to one you might think how is this gonna be useful but the modulus operator can actually do a lot of cool stuff it's one of these magical operators now the unary minus is pretty easy to understand if we have a variable a equal to five and then we said be equal to negative a B is going to have the value negative five which is exactly what we get when we print it up here and the important thing to see here is that a doesn't change so even though we use this negative a the original value of a stays the same and that's important because once we start talking about increment and decrement it doesn't work that way so now let's talk about increment and decrement this is one of the most useful and most common operators seen in programming because it makes our lives a lot easier so to understand an increment if you do something like a plus plus it's going to take the value of a and increase it want and it's going to affect the variable so the value stored in the variable a is going to be one higher than it used to be so this is basically the same as doing a equals a plus one which would work but it's just been condensed to this a plus plus now the interesting part is that you can actually assign this to a variable like I'm doing here on line 75 right now and what exactly happens when we do that well the thing to know is that B gets assigned to first so it's going to be B is equal to 5 and then we increment a and a is equal to 6 so when we do this post increment it's called post increment because the double plus is the increment operator is on the the right hand side of the a it comes last or after a so B is going to be 5 and a is going to be 6 this is different than the pre increment so the pre increment works differently so we started fresh by setting these both equal to 5 again and then we did plus plus a and now what happens is this plus plus gets evaluated first and then we assign that value to B so now they're both going to be 6 that's just important to understand the difference and usually you'll see this in the real world but this is still a thing and honestly if you just do it by itself like a plus plus or plus plus a it makes no difference the only time it really matters is if you're passing this value into a function or assigning it to a variable all right so now let's talk about some of these assignment operators well we've been using this original assignment operator a ton so a equals 5 a is assigned the value 5 is essentially what we're doing but there's actually some of these other ones which have these math operators before the equals sign so going back to what we had earlier we had a is equal to a plus 1 this could alternatively be written with the increment operator like so a plus plus but even another way to write this would be a plus equal one and that is what we're showing here so when you do plus equals something it's going to change the variable a by that amount using this operator so in this case it's going to increment it by one but we can increment by ten we can increment it by a hundred and we can use these other operators too so we could we could multiply the value by a hundred we could divide the value by 100 and so forth so these alter the original variable value and that's important to understand so let's look at the example we have we set a and B both equal to five and what we do is we do B plus equals a so what that's going to do is it's going to increase the value of B by however much a is so B should be ten and a is going to remain five cool now what we do is we do a multiplication so this is just going to change the value of a by multiplying it by 30 and we get 150 lastly we have 150 and we basically take the modulus of 140 which would resolve in ten because this is going to be ten left over so that is kind of your crash course on operator precedence now I wanted to talk not just precedence but just how to use the different operators and the precedence is with the parentheses I showed you up here but the other thing I wanted to show you is the associativity now what is associativity if we bring up this chart again let me get my chrome up it's not working all right so the associativity is when we have operators that are of the same precedence the associativity is how they get evaluated either left to right or right to left typically you're going to see left to right and that's really what most people are most comfortable with but on occasion there are right to left so for example the the assignment operators are right to left and the only time this really matters is if you have multiple operators in the same expression that have the same precedence level so for example this is actually a perfect example if we have x equals 2 times 3 plus 3 or not 3 Z divided by 3 let's just go through this as a theoretical example well you know that this is going to be grouped like so because we evaluate left to right and the reason that makes sense is because the the multiplication and the division have the same precedence so then we go to the associativity to define how they are evaluated which is from left to right so we do this one first and then we get that result and divide it by 3 so that is the basics of precedence and associativity and operators I hope that was super super helpful for you guys a lot of these other ones are gonna make a lot more sense once once we talk about if statements so thank you guys we'll see you in the next video welcome everybody this video we are gonna be talking about logic so this includes if statements and the variations as well as switch statements and the conditional also known as ternary operator so we're gonna talk about all of these things and just help hope you guys understand how we can do branching and conditional output in our code so this stuff is pretty cool because this is where our code starts to become like more useful beyond just calculating things like we're able to take input and do different things based on that input and so forth so this is like the basis for more complex programming yeah it's gonna be pretty sweet we're also gonna be talking about some of the other operators we didn't talk about in the previous video so the very first thing is an if statement and the way an if statement works is you put the if keyword and then you put parentheses and then you put a code block with these curly braces now essentially what happens is whatever you put inside of these parentheses evaluates to either true or false so you can put any kind of expression in here and if it evaluates to true this code block will be executed if it evaluates to false it'll continue on past the if statement so the easiest way to show an if statement is literally to just type true here or you can use one if you if you don't want to include standard bull H you could use one there so we'll try true and then inside of here you could say anything you want and when we compile this it will execute this is true but as you can probably tell this is kind of pointless because it's going to say that's every single time so you might as well just not even put this F statement at all and in fact I don't know if the C compilers do this and rot but I know compilers are capable of looking at if statements such as this one and realizing hey this is gonna execute every single time so we're just gonna get rid of this part and this part down here and just keep that I'm not sure if that's happening here or not though okay so the other scenario or the other variation of the if statement is the if-else and what happens here is if it's evaluated as false this path will execute all right so when compiled the same things gonna happen this is true but if I go in here and put false and now says this is false so that is an if-else statement so now that you understand the basics of the if statement let's turn this false into something more useful because right now this doesn't offer us any benefit so we're gonna create a variable and say int age equals 17 so now we can do comparisons against this age and this brings in this other operators we were talking about so if your age is greater than 17 you could say this is adult and this is no adult for the false case so now what's going to happen well age is equal to 17 and this is going to be compared against 17 is 17 greater than 17 the answer is gonna be false so we should get this is no adult awesome so now you can kind of see that it's a little bit more useful obviously it's still hard coded so it's not super useful but eventually you're going to be able to get this value from a database from a file from user input and make your application more dynamic the next thing we're going to talk about is logical operators the first one you should know about is the not operator so what this will do is it will flip any expression if it evaluates to true it'll make it evaluate to false if it evaluates to false it'll make it evaluate to true so to do it you can basically take an expression wrap it in parentheses and put an exclamation mark right before it and now whatever this evaluates to it'll flip it because we have this exclamation or the not operator right before it so this once evaluated to false here but now it'll evaluate to true and you can see it says this is a dolt even though H is not greater than 17 so this evaluates to false we flip it to true and then this is executed so that's the first one the next logical operator is the and operator and it looks like this two ampersands or capital sevens so for example we could have another variable like money equal to 30,000 and notice there's no comma here that's important and what we could say money is greater than 25,000 which is true because we have 30,000 monies but this is still not going to evaluate to true because the way the and operator works is that both parts need to evaluate to true so H has to be greater than 17 and money has to be greater than 25,000 so you can see we still get false and I'm just gonna get rid of these adult things and just make it general true because we're gonna go through a bunch of examples okay so now it should say nothing because we don't have an else statement the other one is the or operator and this will evaluate to true if either this evaluates true or this evaluates to true and as you know we have 30,000 money so this will evaluate to true and we'll get that print statement there you go so those are the logical operators now there is one warning I have for you guys and that is if you mix and and/or operators in the same expression you introduce a level of ambiguity and in fact the compiler will give you a warning about it if you do it so that is definitely helpful so to illustrate this I'm going to switch that back to and and let's say we have a boolean a boolean variable is graduated and we'll set it equal to true and then we'll append to this or is graduated okay now it's really not clear unless you really know what's going on because you can't tell where the expression would essentially put parentheses if it did that so like let me let me just draw off the possibilities to make it really clear for you guys one possibility is is that these get evaluated first so this and and then that whole expression is ordered with is graduated the other possibility is that these get grouped together so these get evaluated first and that's ANDed with the this expression over here so which one happens actually is the first one so this is the default behavior where it is evaluated left to right and that keyword is associativity which we talked about in the previous video so this is going to evaluate and then if this is true or false that will be evaluated with is graduated so in this scenario because is graduated is true it doesn't even matter what's over here this will print this is true and you can see we get this is true the other one is a little bit different because this one because we forced the parentheses on here money is greater than 25,000 this is true so this entire thing is evaluated as true but the thing is age is not greater than 17 so when this is evaluated the first thing is au age is not greater than 17 boom that's enough to know that this is not true and we can move on we don't even have to evaluate this here so that's why this one is not executed and just to be sure I'm gonna put a 1 here because they both have the same output so this is true 1 the next thing I wanted to talk about is the if else if statement and the way this works is well first I'm gonna clean this up because we don't need all this let's just go back to age and we'll just get rid of all this here so we can create a normal if statement and let's say if age is I don't know less than 12 we could do something well instead of saying age is less than 12 or anybody who's 12 or older you can actually subdivide the group of population a little bit more by doing an else--if so you could say if age is greater than or equal to 12 and age is less than 20 for example then we're gonna do something else and lastly we can still have an else statement at the bottom now the next thing i wanted to talk about is single line if statements and these are dangerous but also very cool okay so let me get rid of all this stuff basically what we can do is say hey if age is less than 150 then I want to do something so let's say printf you might be alive okay so this is a one-line if statement you don't have to use the curly braces and it works just the same so this will print you might be alive but if I'm actually 170 it won't do anything cool so the danger here is that it's very easy to forget that you can only do one statement oftentimes people put a second statement in here to be evaluated if this is true and that's not what you want to do you can only do one and that's it so for example if I said yep this is going to print every single time no matter what so you might be alive yep and if I make my age 170 it's still prints yep and you'll often see this when the the statements to execute or on the next line like so because it's really easy to just go in here and add a new line because it's indented it looks like it's part of the if statement but it's not so that's the only warning I have for that my recommendation is if you're going to use a single line if make sure you just put it on one line because it's a lot easier to realize hey dude there's no curly braces I'm just doing one statement here at the end the next thing I wanted to talk about is the switch statement so the switch statement can do a lot of similar things to the if statement it's actually a little less useful in my opinion but it's still kind of cool I think it's easier to read for certain situations but not always so to do a switch you'll need a an integer based data type so that can be integer or it could be character and then you say switch and then in parenthesis you put the variable name there and then everything goes inside of the curly braces so that all matches the if structure but this is not going to be evaluated to true and false it's going to be evaluated to different possible values or cases so if it's zero we're going to do something if it's a one we're gonna do something if it's a two we're gonna do something and so forth and then lastly you can have a default case now before you go coding things in here you want to put a keyword and just trust me break you'll need to put this after every single one I'll explain what that does in just a minute now before the break you can put any code you want k zero and I'm gonna do that for all the cases and then lastly if the default case so now let's execute this and see what happens you can see that it hits case two so it comes down here what's the value zero no one nope case two yes it is the value two thus this is what happens and you can put multiple statements in here it doesn't just have to be one but just for illustration here this is nice and simple now you might be wondering what's up with this break keyword and it's kind of silly honestly but you have to put this break keyword to prevent it from falling through and executing these other statements it's kind of stupid I know but if you don't put it in there bad things will happen so for example let's get rid of these breaks and let's say the value of menu choice is zero well now when I execute this it hits case zero prints this and then it falls through and does the print statements for all of these cases so it's really really super important to put the break I would even put it in the default case as well now there is one other thing you can do for the switch statement and that is you can have multiple cases execute the same sorry guys my dog is interrupting so as I was saying you can have multiple cases execute the same statement and to do that you just put them in the line like this and I could say zero one or two so if you want the same thing to happen for multiple cases that's how you would do that and let's say our menu choice is one you can see that it hit this case right here and then it fell through and hit the others because I forgot to put the breaks back in see I told you they were important so that is the switch statement the last thing I wanted to show you guys in this video is the conditional operator so I'm just going to get rid of everything here let's say we have a variable balance like a bank balance and you know pretty typical amount negative 5,000 and what the ternary operator allows is to do is it allows us to basically do an if statement to start with and if if it's true something happens if it's false something else happens so the structure is you put an expression balance greater than zero then you put a question mark and the question mark is what happens if balance is greater than zero so it evaluates to true and you can print something here for example and you don't put a semicolon that's important it's odd I know but it goes at the end then what you do is you put a colon and what the colon does is it says hey this is what I want to happen if it's false so this is an example of a ternary operator let's execute it and see what happens no money yo for some reason this is the hardest thing for me to remember this syntax - it's really simple but I always end up having to look it up but you can actually do some other cool stuff with this so for example we could have a boolean variable has money for example and we could say has money is equal to and then we could use a ternary operator here so we could say balance greater than zero and it's kind of cool now that I think about it just kind of like it's kind of like a question balance greater than zero if so yes we do have money one evaluates the true if not no we don't have money zero evaluates to false then we can just do a print statement there we go has money nope we don't oh man alright so this is basically giving you the introduction to if statements switch statements and the conditional operator this is basically the basis for more complex applications and hopefully this will give you what you need to know to start making some cool stuff so thank you guys the next video we're gonna start talking about loops and that's gonna be pretty sweet so check it out welcome back everybody this video we are gonna be talking about loops now loops are actually really cool because they allow us to execute some line of code or lines of code numerous times and there are three types of loops there is the for loop the while loop and the do-while loop and by the end of this video I expect that you're a master in all three well maybe not a master but at least how to use them so first let's just talk about the fundamentals of all loops there's things they had they all have in common so there's three pieces that each loop needs to be functional and that is an initialization of some variable a comparison against that variable and then an update of this variable I guess I could just say update so I remember this I see you basically you're going to for a pretty standard loop you're going to have a variable such as I it's typically called I and you're going to say it starts at 0 so that's initializing and then you're gonna say hey as long as I is less than a million we're going to keep running this loop and then each time we're done with this loop we're going to add 1 to I so that's kind of like the basic structure but it's a lot easier to see with just going through an example so here's like a very simple loop like the basic beginner loop int I equals 0 that's the initialization I is less than 10 I plus plus and then in here we can print something specifically will typically use I to say something like to change the value within within the loop if that makes any sense at all basically we don't we just want to say the same thing each time we want the the print statement to depend on what loop iteration we're on so the first iteration I is going to be zero so we're gonna print zero the next one it's gonna be one two and it's gonna count up until nine so let's let's print this out and let's put a space here my dogs barking on me and stuff gosh should probably go see if there's an ax-murderer it would appear that my house is axe-murderer for me so that's good so let's print this let's run this you can see it starts at zero and counts up to nine so each loop iteration is the word it's going to print the value I which starts at zero and every time we go through the loop it increases one until it hits ten as long as this is true it's gonna keep running but once it hits ten this is no longer true because 10 is not less than ten and the loop stops now you can also use you know we could use less than or equal to if you wanted to include ten or we could start I of one there's all kinds of different things we could do we could increase I by two each time so you can say I plus two or we can multiply it there's a bunch of different varieties of the for loop and it's just important to understand that as long as you have these three pieces you should be pretty good to go now there's one other thing I wanted to mention I'm not positive which versions of C require this but it might be that let me get this back to normal real quick it might be that you have to declare I up here and then just say I is equal to zero either way works it's exactly the same thing you can also make a for loop to decrement so for example we could start at nine and say hey as long as I is less than or equal to zero we're going to decrement the value and now it counts down from nine to zero so the thing here is that I changes each iteration so you can use this to effectively iterate through an array and print all of the values so for example let me change this back to this here what we could do is we could create an array let's say we have a group of ages so we're just collecting the ages of all of our neighbors all right we got a pretty good variety and then some super old guy now what you can do is you can use a variable to keep track of the length so you could say int length or in size whatever you prefer in size equals 10 and what you can do is say I is equal to 0 and as long as I is less than size increment I okay so now what I wanted to mention here is that the size thing is kind of unnecessary because you could just put less than 10 but it's often helpful to put the size of the array in a variable so if you need to reference it throughout the program you can just write it here and then for example if we added an element here we could change size to 11 and anytime we reference size in our program it would automatically update to this value 11 so it's not going to break our program so we're going to keep that at 10 and also in other languages you'll often have something like ages dot length or in ages dot size unfortunately there's nothing quite like that in --see I will tell you the closest thing to it in just a minute but for now let's just print this array so what we can do is we could say ages of I and say that is equal to % D and then what we need to pass in is not i but ages of i there we go let's see if this works cool so we get every single value printed out all the way from 12 to 12 so we covered the whole basis if you wanted to calculate the size of the array and not have to have a variable to store it you can do that like this using the sizeof function you can pass in the array and then what you need to do is you need to divide it by the size of one of the elements usually the first so what exactly is this doing well because each elements going to take up multiple bytes we have to have that division in here because this is just going to give us our total bytes but let's say each element takes up four bytes well then we need to divide it by four to get these size so in here I could replace this with calculated size like this and you can see it still works perfectly fine the one warning I have for you is that if this is done inside of a function where you pass in an array like ages it's not going to work and that's because when you pass an array to a function it decays to a pointer and when you do something like size of ages on let's say a pointer ages it's not going to give you the size of the entire right here it's only going to give you the size of the pointer so it's not going to work here it works because we created the array in the same block and that we're calculating it in so it works just be warned you don't want to try and do this inside of a function where you're passing an array as an argument all right enough on that you can also have nested for-loops so for example I could create a for loop here and I equals 0 I is less than 10 I plus plus and then inside of this for loop we could have another loop so this loop is going to execute for every iteration of this outer for loop and you can actually reference this variable inside of this for loop if you wanted to do something like that so we could say int J is equal to I for and then say hey as long as J is equal or greater than or equal to zero we're going to count down so basically what was gonna happen this loops going to count upwards and then for every iteration it's gonna take that iteration number and count down to zero kind of pointless but kind of cool it's totally useless I'm just gonna say percent D and then the value we're gonna pass in is J and then down here we're going to want to print a new line oops just like so all right let me scroll this up a little bit so you can see we're counting up from zero to nine and every single time we count down from the number we're on all the way down to zero so that's some cool practice with for loops you can also use nested for loops to iterate through 2d arrays if you want to look into that and so forth the next thing is while loops and these work exactly the same way so I'm just going to get rid of these four loops here let's just start fresh we're gonna have those same pieces though so we need to initialize a variable and then we're going to make the structure of our while loop which looks like so and then our comparison goes here so we could say as long as I is less than 10 we're going to increment I and then the the code part goes here so we could print I and this will do the same thing as our original for loop count from 0 to 9 awesome the last thing is a do-while loop which is a little bit different because it's going to look like this we're gonna have a do and then we're going to have our code block and then we're going to have a while with the condition in it followed by a semicolon so this is our structure so this is always going to be executed at least one time and that is the difference between a do a do-while loop and just a while loop I usually use these four menus so if I want the user to basically open the application and type one for something or type two for something else and let's say they put in the number 645 well I could basically say hey do display the menu while they're not giving a valid input a very simple example of this is let's say we want them to choose a number between 0 & 9 and then what we do is we scanf that number and we're going to need a variable to store it in it's gonna be an integer and we're going to store that inside of input and we're going to keep asking them as long as input is less than 0 or input is greater than 9 so let's try this out so when we run this if we put anything that's not in that range it's just gonna keep asking us until we put a correct value and then it ends it probably makes sense to put this printf inside of the do loop actually so that way asks every time otherwise it just kind of looks like it breaks there we go now let's try 60 nope negative 10 7 once we put in a valid number it stops awesome so that is how you do for loops while loops and do-while loops hopefully you guys understand the differences the for loop and the while loop can do exactly the same thing and it's even possible to take this do-while loop and convert it to a while loop you basically just have this print once beforehand and then you do the for loop after or the while loop after so you can basically do the same thing with all of these loops it's just slightly variation in style and preference generally if I have a list where I know the length I'll use a for loop if I want something to do something indefinitely I'll use a while loop and if I want something to execute at least once I'll use a do-while loop so thank you guys hopefully that was helpful in the next video we're gonna be talking about arrays so check that out guys welcome back everybody to your video dedicated to arrays we've touched on them a little bit in the series so far but we haven't gone in depth and just did a video dedicated to them so hopefully by the end of this video you have a pretty good understanding now an array is just a collection of data elements that are all of the same type so to declare one you do this in ages and then you put the square brackets where ages is the identifier you can name it whatever you like and you have to give it a datatype meaning that each element has to be of this type you can't mix types you say that this is an array by putting these square brackets here now this is just a piece of you know extra information some other programming languages have you put these square brackets with the data type and not the variable so for c and c++ it goes with the variable name the identifier other languages such as C sharp you put the square brackets with the data type so just keep that in mind try not to get too confused and just understand that difference if you go into other programming languages now if you're going to declare an array you have to say what size this array is going to be so you could say hey I want to make an array with ten elements and these elements each have an index it's like the the number that they get when they get put into the array the first element gets the index zero so it's zero based indexing so it goes from zero to nine where nine is the tenth element that is one of the most confusing things to get started with if you're new to arrays you know it kind of takes some time to wrap your brain around it and eventually it'll be like the default way of thinking about it okay so this is a statically sized array and what that means is that the the size is determined at compile time it's given that much space in memory no more and you can't go beyond that without accessing areas of memory you're not supposed to be touching it's not very simple to create a dynamic array without using memory allocation dynamic memory and pointers so in the upcoming videos we're gonna be talking about dynamic dynamic memory but until then all of our arrays are going to be statically sized meaning if you don't know how many elements are going to be in this array like let's say we want to have the user put in these ages by hand we have to be sure that we give a number big enough to hold all of those elements or once they hit the max we have to say yo dude that's the highest number of ages we can contain that's one of the big down sizes with this and other programming languages you'll have things like array lists which are dynamically sized but not so and see without using some more complicated stuff which we'll be talking about soon so enough blabbering the way you add elements to this is you just say ages and then inside of these square brackets you can reference the index of that element so if I wanted to get the fifth element I would put four and then I could give it a value such as 65 now this is one way to do it you could literally go through and make a an assignment for every single index but that's kind of stupid because there's an easier way to do it and it looks like this and you're actually going to get rid of the size here I'm gonna say equal and use two curly braces and you can just put the values in here so now it's still a statically sized array but now the compiler is going to be able to look at this and be like whoa there's six elements so the size of this array must be six so it's fairly smart you can count now you can still leave that six there and if you compile it's not gonna it's not gonna gripe at you but the thing is if you go in here and and add stuff well that's that's not really good because you're only allocating at size six and you can see you get a warning so definitely make sure if you can just leave that number out but it is always good to have a an array a variable to contain the size anyways because when we're passing arrays to functions you have to have that size attached as another argument know so in this case I got one two three four five six seven eight elements so we might say size is equal to eight and then you might do something like call a function print array just as an example and you might pass in ages in the size without that size it's not gonna work out so well and we talked about that in the previous video so be sure to check that out okay so one of the other things we talked about in the previous video is how to print an array and I just wanted to make sure that was here just for completeness of this video so we could say in I is equal to 0 as long as I is less than size we want to keep running this loop and each time we want to increment that that iterator by one and then we can print that element just like so so let's compile and run and you can see it prints out every single element all the way up to 21 you can actually change the value of these array elements just like it you would any other integer so you could say ages of 3 is let's say 60 and if we print this again and also let's just print a new line here all right let's save and compile the first time we print it this value is 43 the next time it becomes 60 and the index is 3 so it goes 0 1 2 3 60 there you go so that is how you work with a raise the next thing I wanted to talk about is multi-dimensional arrays which is a little bit more complicated for that I'm going to make two variables I'm gonna have intros so this is kind of like keeping keeping sense of the sizes but instead of naming them size we're gonna have it columns and rows just for clarity then we can make a multi-dimensional array like so student grades and we put two square brackets and you actually need to put the columns here just for it to work otherwise it gives you a funky error and then you can put numerous arrays inside of this initialization so I like to just kind of build this shell first so I don't forget anything so this each one of these is a row so you could do 1 3 4 6 3 2 4 5 and so forth okay so now we have three rows one two three and each one of these is a column so I got 1 3 2 1 3 32 2 or 3 2 2 4 4 4 & 6 5 9 so we have 4 columns so that is how you create a multi-dimensional array let's just compile make sure I got all my syntax right okay so we actually have to make this Const in order for this to work there we go now let's print this so to do that we need a for loop inside of a for loop and where you put this here doesn't really matter it's up to you if you would prefer to have it back here that's totally fine with me so this one is going to print the rows and then for each row we're going to print each number which is the columns so we say I is less than rows I plus plus and then inside of here we're going to say int J is equal to 0 and we're going to make it less than the columns J plus plus then we just print the value and what we're going to do is we're going to print student grades the first box is going to be the row which is I and the second box is going to be the column which is J and then we'll probably want to do some formatting here so let's say % D space like so and then let's say after each row we just want to print a new line so this inner for loop is going to print an entire row and we're gonna do that for every single row in this thing using the outer for loop so it's gonna go this one and then this one and then this one alright let's compile and run and you can see it prints out the values just fine that is the basics of arrays the main things you guys need to know is how to create arrays and how to create multi-dimensional arrays and how to print all of the values and not necessarily printing them I just choose to print them you just need to know how to reference each one of these elements in some order so that's usually done with mall nested for-loops so thank you guys please be sure to check out the next video where we're gonna be talking about strings welcome back everybody this video we're gonna be talking about strings now a string and C is just a character array so you can make one like this and then inside the square brackets you give the size of the array and the important thing to know here is that you need to preserve one character for the back slash zero which is known as the null terminator so what this is and this is put in there automatically if you assign a string to this I'll show you guys that in just a in a minute but what this what this is is the null terminator and it's basically a way to indicate the end of the string so let's say you put the name Caleb in here the characters are going to be C a le b null terminator so that way when we write programs that go through a string or we're able to tell where the string stops now the important thing is that you don't want to overwrite this character because then our code could go beyond the end of the string and go into areas of memory we're not supposed to access and then bad things are gonna happen like computer can explode your bank accounts gonna get hacked etc so you don't want to do that all right so now the first thing we're going to do is scan a value from the user and the conversion is going to be s for string and we can actually limit the number of characters by passing in a number right here so we could say 19 so that's going to give one spot for the null terminating character and that is exactly what we need so then we just store it a name now we don't need to use the address of operator which looks like this because when we pass an array to a function it's automatically going to be converted or technical term is it's going to decay to a pointer so you don't need that all right the next thing we're going to do is we're basically going to rate write code to calculate the length of the string and the way we're going to do this is we're going to go through each character look at it and see if it's the null character if it is we're gonna stop if it isn't we're going to basically increment a counter so we're going to start a counter add 0 and we're just going to loop through this string so because I this is in my mind I use a while loop because this is an indefinite kind of thing we don't know how long we're going to go it makes more sense to me in my brain to use a while loop so then what we do is we access the character so letter starting at 0 we're going to grab the first was the first character then we're gonna go to the second character and we're gonna keep doing that as long as the character is not the null character and then what we're gonna do is we're going to increment the counter every time that it's not the null character at the end we should be able to print the size just like that I'm going to put a prompt in here so so the user knows what to do and then now it's just Ryan to see how the program would work what is your name Caleb the size of name is five awesome so there's actually some functions that will do this for you so in order to use these you need to include string.h and then everything should work just fine so the first thing is that there's a string length function that will basically do all the work we just did but I'll make it a lot easier so we can say the same thing size of name is and then what we're going to pass in here is the string length of name so the function is sterling and then we in parentheses we pass the the variable that we want to calculate the string length of so let's see if this works all right we do get a warning and basically it wants us to use Lu because it's an unsigned long different data type so we can just go in here and change that D to an Lu and it should work just fine what is your name Caleb besides a name is five size of name is five so basically what I'm telling you here is you don't need to reinvent the wheel some of this stuff already exists awesome so what else don't want to show you guys well there's actually a compare function so if you have two strings or if you have one string and you want to compare it to a certain value like guessing a secret passcode or something like that I don't know you can do that pretty easily and the way you do that is you use the stirred pump for string compare and you're gonna pass in two things here the first one is going to be the variable and then the next thing is what you're comparing it against so Caleb and then you need to do this when you say equal equal zero it's kind of odd here but if if these are equal this string compare returns zero which with this entire thing will evaluate to true so hopefully that makes good sense and if it doesn't just know that you need to put equal equal zero so now you have to guess the secret name if I put Caleb you get access but if I put something else we'll see if it works if I put tacos it's my code name we get nothing the next thing I wanted to show you guys is how we can copy a string so for example if you wanted to copy the value from name into another variable you might think to do this which is not going to work by the way so you could say oh I'm going to create another one give it a size 20 and set that equal to name you can't do this and see you actually have to use a function for this so you can declare that variable like this but then the function you want to use is called sterk copy with no no vowels and then you put the the copy here and then what you want to copy from in the second argument so the destination comes first and then where you're getting it from is second we should be able to print the value of copy and we're gonna use percent s cuz it's a string and then we're gonna pass and copy what is your name Caleb copy of name is Caleb so that's how you make a copy of a string which might come in useful the last thing I wanted to show you guys is how to do string concatenation and see so in order to do that let's say we have a variable last name and we're gonna set this equal to curry and like I said you don't need to put a size when you're assigning directly so this should work and what the code is doing here is we're going to basically append to this copy here and what we're going to append to it is the last name curry so then when we print it it should come out as Caleb curry which is my name in case you guys didn't know ok I got an issue I need to put a semicolon what is your name Caleb full name Caleb curry awesome so those are just some basic string functions I'm sure you could make some custom string functions and make a pretty sweet library in the upcoming videos we're going to be talking about creating our own functions and how to put those in a library and do multi file compilation so we've got a lot of cool stuff coming up let me know if you guys like this video and if you have any questions at all thank you and I will see you in the next one hey what is up everyone this video we're gonna be talking about functions and how to create our own functions now what exactly is a function well a function is just a section of code that you can basically assign a name to it and give it an input and it'll give you an output or it'll do something for you and you don't have to keep writing the same code over and over again you can just call that function so we're actually inside of a function main and all of our code goes inside a main but we can create other functions so we're gonna start off just going through various examples of functions and they're gonna start off very simple to where they almost seem kind of pointless but once you get into more complex functions it makes it makes a lot more to use functions versus the code inside of the functions and you'll see what I mean so let's just get started let's say we wanted to create a function that will square a value so the easiest way to square a value is if you if you have a value like X all you have to do is say X multiplied equal X and when you print that in compile and run you'll get 25 so squaring a value is just the number multiplied by itself you could also do equals X multiplied by X same thing now if we wanted to extract this functionality into a function what we will do is go outside of main so go up here and we would say hey what type of data are we trying to get well we're going to get an integer and then we give it a name which we can call it square as an example and then you give parenthesis and then a code body inside of the parenthesis is where you make parameters and parameters are going to store the data passed into this function as arguments so we're going to have basically an input variable and this this value is going to come from main so you would call this like square and then you would pass in X and that's going to return an integer so you would store it in something like so all right then we can actually print x squared so just take a couple minutes to digest all of this if you've if you're new to functions it can be as some more syntax you have to become familiar with or ways of doing things but basically now what we're doing is we are just calling this function and it's going to calculate the square of this input this when you pass in the data it's no an argument inside of the function where it's stored is called a parameter so it's going to calculate the square of this parameter inside of this and return that value which is going to be stored in X squared so inside of the function you could say input times input and then all you have to do right before it is say return and that should work so let's compile and you can see we still get the same value 25 now you this is where it's kind of like oh what's really the point of that why don't we just keep it how how we used to have it which you can in this situation but as you get more complex more complex functions it's a lot easier to do something like this than to take all of the code inside of the function and continually repeat it throughout your program it's it's based off of the principle of only doing things one time or the DRI principle don't repeat yourself and it will allow you to execute the same functionality numerous times by just calling the name of the function and not having to repeat yourself by putting the same code because if you wanted to later go change an algorithm or you found a bug you might have to go throughout your entire program and change every time you use that those lines of code if you have a function you only have to change it in one place and anywhere calling that function is automatically going to use that new code so that is why you should use functions but obviously this is a very simple example so I want to go into a more complex example let's say we wanted to make one you can you can basically create multiple functions and build a library so you can do multiple things by just calling various functions so you could have one such as cube and it's still going to take an input and in this body it's gonna basically do the same thing but it's going to do it three times in return so this is basically another function in our library alternatively you can create local variables so basically variables that only exist inside of this function so I could say int X and we could set that equal to the value that's calculated and then we could return X that works exactly the same way and when you go outside of this function cube this variable no longer exists so this X here is different than this X here just so you guys are super clear that has to do with the variable scope anytime you declare a variable it only exists inside of the curly braces that it's defined in that goes for all things so even if you did an if statement if you declared a variable in here well outside of this if statement it's not going to exist all right so that that's really not that complicated either let's try to make a more complicated one let's say we wanted to create a function to instead of having a function to square something a function to cube something let's just make a more general one that can raise any number to a power of some any any input so it looks something like this so we're going to need the input just like always but then we're going to have another input and work it we call it exponent names don't matter it's just for your use here and what what we're gonna do in here is we could actually basically go through this multiply input by itself exponent number of times all right so we could do basically there's numerous ways you could do this what I'm gonna do is I'm going to basically have a total number it's just gonna keep track of everything set that equal to one and then I'm just going to do a for loop to basically multiply 1 by input until we do it exponent times hopefully that makes some sense if not just just watch okay so if we passed any value for for exponent this is basically going to go in here and it's going to multiply one by four which will give you the value 4 and then again by four again by four and again by four so you end up getting the correct value at the end you just need to make sure that you use the return keyword to give the value back to the caller and that's inside a main we could call that function so we could say I know you just make up some variable X - 5 we could say int yeah we need to declare it int X - 5 and we could say hey we need to call power and we're gonna pass in X and we're going to raise it to the fifth power then what we need to do is we need to print that out like so awesome so if you take five raise it to the fifth power you should get this number here so now we're making our functions a little bit more useful they don't just do one thing but you're able to change the functionality by changing the inputs like so and this can also replace that squared and cube function because we could just pass in two or three here and also the squared so there you go you can see that this this function here power has much more use than these functions up here so you could if you wanted you could get rid of these and just use that power function so the next thing I wanted to do is a recursive function and recursion is outside of the scope of this course and if you've never used recursion this is going to look some like like some black magic I tell you but if you have used recursion hopefully this would be a good example of what you could do so we could basically create the same function using recursion and recursion is when a function calls itself so I'm just gonna give it two name recursive power and it's gonna do the same exact thing in fact it's also going to take an input and an exponent but the the way we calculate this is fundamentally different basically what we want to do is we want to have the input and multiply that and then we need to call the function itself with a subset of the input so what we would do is say recursive power passing input and exponent minus one and then we're going to return this so let me explain how this works if you think of input being the value five when when this function is hit it's going to return five multiplied by something and this something is going to start at the beginning which is going to return input five and it's going to keep doing that and eventually it's going to stop when it hits what's known as the base case so we could say if exponent is less than one we're just going to return one we'll just multiply it by one keep the original value so this is basically just going to multiply it by five for every value of the exponent in each time the exponent number is going to go down until it hits one or zero once it hits zero it's just going to multiply everything by one which keeps it the same and we should get that same value so hopefully that made a little bit similar sense than it sounded like it made sense in my brain so then we could say recursive power passing five raise it to the fifth power awesome let's try it okay we have an issue okay I forgot to get rid of the references to these square functions so I'm just going to get rid of this one here as well as this here awesome and it printed the value of what the original power function did when we passed in five so it seems to be working just fine one thing I wanted to mention to you guys is that when you pass a variable into a function it's creating a copy of that value so you can't change the original inside of the function and just to see that let's say we have a function and by the way we're going to talk about void functions void functions are not required to return a value so we could say change Vatel and we could take an input and let's just say input is equal to some number okay then in here let's say we call this and pass in X and then after it we print X so if if you could change the value you would think that hey we pass in X inside of here it gets changed to 900,000 and then we print it and it prints 900,000 but the reality is the value of X is copied into this new variable input so the local variable input is changed but X has never changed so when we run this it should print the value five and you can see it does now you can change the value of a variable passed in but you have to make one small change because it's not going to do this by default and that is instead of using a normal variable like this we're actually going to use a pointer and we're gonna be talking about pointers here soon but just follow the syntax for right now in the parameter you're going to put an asterisk right before the identifier of the variable and then when you need to change the value of it you're going to use an asterisk and when you pass it in you're going to use the ampersand address of operator now when we compile we should get the value 900,000 so X itself is changed and that's because when you pass in a pointer we were able to change the value at that pointer this is now pointing to the same data this is pointing to so hopefully that makes some basic sense and once you get into pointers you'll understand this a lot more but this should give you a pretty good foundation the next thing I wanted to talk about is functions that take arrays so let's clear out our main and let's just get some space here let's this pretend we have a fresh start and we're going to create a a function that goes through an algorithm to get the largest value inside of an array and let's just say we have an array ages and this is going to basically contain a list of all the ages of our friends and family and we're going to create a function to get the largest value in here so whenever we pass an array to a function we have to have the size so we can have a size variable here and set that equal to 7 because there's 7 elements in this array and the way you could print this out would be something like this then you're going to call the function inside of the print F argument which works just fine because this function is going to return a value and then that value is going to be passed into printf you don't always have to reassign it to a variable and then use that variable although you can do that if that makes it easier to read we're going to pass in ages and then we're going to pass in the variable size so that is how we call it but now we need to define it so it's going to be called oldest value it's going to return an integer it's going to take an int array we're gonna call it the same thing which is allowed and then it takes an into size the way I like to do this algorithm is you just go through the entire list starting at the beginning and just keep track of which one's the largest so we could say int largest equals then we're going to get the value ages of 0 which is going to get the first element so to begin we're just going to assume that the first element is the largest then we're going to iterate through the entire array and just see if any of them are bigger so for int I equals 0 I is less than sighs I plus plus and we can actually start at 1 because 0 is already gone through assigned to largest so we need to only compare after that point so inside here we can just do a comparison if ages of I is greater than largest the largest is going to become ages so I we're just going to replace the largest value then when we are done we need to return the largest all right let's compile and see if this works it prints 53 which is in fact the largest of the ages so that is awesome one thing you also need to remember is that you need to have a variable size because anything done to calculate the size of an array inside of a function is not going to work when we're working with a passed in value that's because this is really just a pointer it decays to a pointer upcoming we're gonna be talking about some pointers and memory management and some of the more advanced stuff in C so be sure to check that out guys hopefully that gives you a pretty good craps course of functions obviously we can't cover everything the main thing you need to know is that arguments are the things passed in parameters are the variables created in the function declaration that store the data passed in we have the return type here we have the return statement which returns that value and then we also have void functions which don't have to have a return you can if you want use the return keyword in here but you're not going to put anything like a value here so that's basically it guys hopefully I was helpful and I'll see you in the next video welcome back everybody this video is where we're going to get into some pretty cool stuff because we're going to be talking about multi file compilation and how you can take the previous video content where we created all these functions and we're going to turn that into a function library that we can include in our code so that's pretty cool because then you can take a bunch of these functions and basically create a library that you could open source or give out to your employees or whatever you want to do with minore ghen eyes your program so you don't have a bunch of these functions you can put them all in a different file so yes this is a little complicated when you start honestly I usually just reference syntax just to make sure I got everything correctly but it's really not too complicated plus once you got it down you can actually take all of the commands that you use to compile your multi file program and you can put them in what's known as a make file we're not gonna cover that in this video if I create an intermediate C programming series which I'm intending on doing at this point I'm probably gonna talk about make files there but that's just a way once you have all the commands you can put it in one file and use that to do everything for you and just automate the process for you a bit but that's really not what I wanted to talk about in this video so let's get back to the point so this is the same exact file from the previous video the only difference is that I renamed it for the appropriate section and then I indicated that it was the main now you can name it whatever you want the only thing is you got to remember which one has the main function in it then the next thing you're going to do is you're going to create a new file so I'm just gonna call it five point two libraries for the section title and then I'm just gonna leave it at that dot C okay and what I'm gonna do is I'm gonna take all the functions we created in the previous video cut those and paste them in the libraries dot C cool so that's step one step two is I'm going to create another folder and I'm going to call it five point two slash libraries dot H for a header file so when you include things if you go back to the main you can see this includes standard IOH well this header file it's really just a list of all of the function declarations what input types they need and what the return type is so we're going to create a similar thing with our program so what we're gonna do is we need to copy all these functions and paste them in here as well but we're going to get rid of the bodies sounds like very morbid gonna get rid of the bodies all right delete all these bodies and just have these signatures alright so this is our header file and we're going to go back into this but for now this is good so what we need to do now that we've split these files into three inside of the main program we need to say include and instead of using the lesser sign we're going to use quotes which will search in the same directory for this header file and we're going to say the name of it which is 5.2 libraries H we're going to take this include and we're going to put that at the top of the C file as well the the one without main and the one that has all the function definitions in here cool so that should be good but just as a protection kind of thing inside of this libraries file we're going to put a condition that says hey if you've already tried to include this we don't want to include it again just so we don't re declare this header file and that's going to look like this it's kind of funky but if and def header file so if it's not defined we're going to define it and then at the end of it we're going to say and if so yeah the compiler can use these commands to better understand what we're trying to do all right so we should be good now all we need to do is we need to learn how to compile these things so this part is a little bit more of a process than we're used to so we're going to take the C files and we're going to compile those and then we're going to take the the output of that called object files and we're going to link those together using the linker alright so what we're going to do is we're going to say GCC - C 5.2 libraries C so that's the first file and when we compile this we're going to get an error oh oh that's because I was working around earlier okay I need to fix that get rid of that this should've been that in there anyways all right let's try this again all right so we compile and what happens is we get this new dot o file so this is an object file it's the compiled version of this code alright so the next step that we need to do is GCC - C 5.2 libraries are - main so now we're compiling our main program and this will give us an overrun of that the next thing we need to do is we need to combine these into one executable using GCC and then we just put the name of the object files so we do 5.2 libraries main dot o and 5.2 libraries dot o so that'll take this file and this file compile those we didn't get any errors so when we do a dot out you can see that the program is still executing and it still works even though inside of our our main program we don't have those functions defined here another thing I wanted to mention to you guys is that object files are like an intermediate step between compiling and the final product where they're basically the compiled code but you still have to put them together using linking now you can basically skip this process where you don't want to make this output file you just want to go straight and that would look basically like this so you get the main file dossie you compile that as well as the one without the main which just says the function code so 5.2 libraries dot C and you can see that will give us the correct output so these are only kind of beneficial if you want to give the code out as as a library but you don't want to have the source code there you just want to have the compiled version there and then the header file acts as like the interface to work with that file so you go into the header file and you see oh this will take an input and an exponent and it will return an integer so you should basically know how to use it and you can also put all your comments in here for more instructions so that is the entire process for creating multiple file compilation quite the process as you can see but once you do it a couple times it's not it's not too terrible you just got to remember all those steps fortunately in the syntax reference I'll have all the notes for how to compile those and you can use that just so you you know what to do for the headers and so forth alright so that's all I got for you guys thank you and please be sure to check out the next video because this is where we're gonna go into some of the more intermediate stuff of pointers structs and memory so it's gonna be sweet see you guys there hey what's up guys welcome back this video is going to start talking about pointers and my dogs bugging me high cotton hey there would you like to help point these people in the right direction see what I did there so the way you create a pointer is well first let's say we have a variable a and we're just gonna set equal to 100 now what we can do is we can make a pointer to this variable you the address of operator so that is the ampersand seven capital seven that is a pointer to a but you don't just want to leave it out there you actually want to assign it to something so in order to assign it to something you create a pointer by giving it the type it's going to point to an integer and then you use an asterisk and then the name of the pointer so our pointer is named B and it points to a so now these stores the location of a so that means we can basically alter the value of a by going through a directly so we could say a is 200 or we could go through the pointer indirectly and you'll be seeing that in just a moment so I'm gonna go through some examples so to do that I'm just going to print a and the value at B just to show you guys that they're connected we're gonna copy this and we're gonna change it to be but we're not going to just print B by itself like B we're going to use the asterisk B this is known as the indirection operator you may also hear dereferencing a pointer so when people are saying dereferencing a pointer they're just saying hey give me the value that the pointer points to so hopefully that makes good sense you'll understand though once we go through some examples the important thing to know here is that when you declare a pointer you use the asterisk and then when you get the value at the pointers destination you use the asterisk but these two things mean two different things up here it means we're creating a pointer up here means were dereferencing the pointer that's one of the most confusing things and that really got me confused when I first started so let's print this and see what happens all right so they both give us the value 100 and the cool thing is is that this is the same value in memory so that means if we go in here and we change a to 200 these are both going to spit out 200 so I'm gonna print these both again so the first time they both equal 100 we change the value of a they both equal 200 all right so now we can do the same thing by going through the pointer using the indirection operator so we could say the value that B points to is now set to 300 print these again and you can see that both B and a change so you can see they are the exact same value it's just we're able to use two different variables to change and use that value the other thing I wanted to talk about is that you can change the value that the pointer points to or you can change the value of B itself so for example if I made another variable and what I could do is I could change B to point to the address of C so now B no longer points to a it points to C so the main thing you need to understand is that there's a difference between B and the value that B points to there's a clear distinction B itself is just an address of memory the value at that B points to is an integer in this case the same one that is stored in C 6000 so now if we want and changed a and then we print them both out again well now they should not be the same thing and they're not that's because B no longer points to 2a it points to C so we could update C and we would see this changes reflected when we were referencing the value at B so to show you some of the cool things we can do with pointers the first thing is you can change the value of parameters inside of functions so for example we created a square function and in the earlier video that would basically just take a variable square it but we had to take that value and store it back into something either replacing the original variable value or creating variable so what it ended up looking like is in x equals five and then you could say X is the square of X and obviously this is a very simple function but the the concept stays the same but what if we just wanted to say square X and that's it we didn't want to have to reassign that to something well you can do that with pointers so what instead of passing X we can pass the address of X using the address of operator and now we're passing a pointer into square then when we declare square we can make it a void function because we don't actually have to return a value because all we're doing is changing the input rather than creating a new output so the input is going to be an int pointer so you just call it input and then inside of here we could change the value of input and to change the value you always have to dereference it using the asterisk so we could say input and we could just multiply that by the same value so this should square it and it's going to change this so no return all right let's compile let's see if it works make sure I save it okay I didn't print it so obviously I'm not going to know all right let's try now and it prints 25 so you can see it works that is the power of pointers it allows us to change the value of variables inside of functions the other thing is that if we're working with arrays pointers are going to come in because when you pass an array to a function it decays to a pointer and I've talked about that I mentioned that throughout this but I never really explained it or went through any examples so now I'm finally going to do that so let's just create an array of ages and like I said you always keep track of a size so we could say in size is equal to 1 2 3 4 5 6 like that so we're gonna pass that size in any function we end up using so just to show you that this actually does decay to a pointer and what exactly that means it means that when we pass it to a function the identifier ages is no longer considered an array it's considered a pointer so we can see this by using the sizeof function so just looking at the memory size of it memory size of ages we're gonna it's gonna be % Lu just like that so let's run that I need to put a semicolon alright so the memory size of Ages is 24 and that's because each integer takes up 4 bytes now if we call a function let's just say size example it really doesn't matter and we pass in ages let's go create this function in here we can all we have to take it as an argument so we can say it's an int array and we're going to print the same exact thing so let me just copy it from down here awesome and it says it will return the size of an int pointer instead of int array so even the compiler is telling us that but just in case you did want to see what would happen when we run this you can see that the memory size becomes 8 which is the size of a pointer so whenever we create a function like if we create a function to print an array you're going to want to include that size which in this case is 6 ok so that kind of sums up the use of pointers there's really only three things you need to know the first is how to create a pointer you use the address of operator how to declare a pointer using this syntax here and then how to dereference a pointer by using the asterisk followed by the identifier name so those are the main things pointers are used to change the values of arguments inside of functions and once we get into memory management they are going to be used as well so check out the upcoming videos guys it's gonna be a lot of fun thank you guys we'll see you then welcome back everybody this video we're going to be talking about Struck's and let me tell you structs are one of my favorite things in C programming they're really super cool they basically allow us to encapsulate numerous pieces of data or values inside of one variable so to go through an example let's say we wanted to create a rectangle or something well we might have a length and we might have a width and that's not too crazy but if you had numerous rectangles you'd have to have two variables for every single rectangle and that's just a very simple thing we're trying to create what if we had a more complex structure and as a result we just had numerous and numerous variables we had to keep track of well with the struct we could basically combine these into one data type called rectangle for example so to do that what we need to do is outside of main we can say struct rectangle give it a name and then inside of these curly braces put these two variables so that is the simplest way to make a struct so now when we create a variable instead of saying int length or int with what we do is we say the type is actually rectangle and then we give it a name my rectangle whatever you want to call it and before the rectangle type we actually have to say struct so this is how we declare a struct and we can give it values by setting it equal to and then using curly braces and putting two values in here so we could say 5 and 10 so it's basically a way to group a length and a width together inside of one variable my rectangle okay so how do we actually go and access the values well if we wanted to say print these for example well we could say the length is equal to and the width is there's some D and the value we're going to pass in is my rectangle dot length and that's for the first one and then we're going to pass in my rectangle dot width so this dot operator allows us to access the members of this variable my rectangle which correlate to the length and the width now the only thing that's different about creating one of these things is this struct keyword now there's actually a way to get around this and to do that it's a little funky but you have to put type death and then down here you say rectangle so that is how you would get rid of this requirement to put this struct here it basically allows us to use this rectangle as any other data type and then this variable here this value here is actually optional you could get rid of that if you wanted to so this is a good start but we can actually make more complicated things so for example let's say we created a another struct for a position so we would say type def struct position and this would have an int X and int Y now together these things are kind of useless by themselves so a position is just a location and a rectangle is just a rectangle but if you combine them maybe it could be something like the the building plans for a house so this is the size of the house and this is the location of the house for example so what you could actually do is you can make a struct of these other structs so we could say type def struct building plan and inside of here we could have the first one be the rectangle and you can actually name this the same thing as the type so we could say rectangle rectangle and then position position just like that we could also store other things in here for example we could have an owner so this could be a string like so and that could where that's where we could store the name of the owner so we can start building more complex structures using Struck's if we wanted to create a building plan down here it would look like this we would give it the type building plan give it a name and then inside of these curly braces we could just assign a value for each one of the things caleb curry for the owner and then for the rectangle we're actually going to use another set of curly braces and we could say this is at a 10 by 5 house or let's go 5 by 10 just to be consistent with this here and this is at position 30 to 48 which i know means absolutely nothing here but just just follow along pretend these are some kind of coordinates of some sort and we can actually print out this entire house and we only have to use this my house variable rather than keeping variables for all of this information so I'm going to write out a printf statement it's gonna be a little bit more complex than usual so I'm just gonna skip to when it's done all right so here's the format string the house at position position size of site size is owned by name then we just have to make a list of all the arguments also passed in for these formats alright so this is the entire printf statement you can see we can traverse through these structures in the dot operator so we can say dot position and then inside that dot X all right so let's compile and see what happens when we print this alright I have an issue all right I have a spelling mistake in position so I'm gonna scroll up and fix that there we go I also have a spelling mistake here cuz I can't type I also forgot to put the type here cuz I'm a failure apparently okay hopefully that was everything all right finally let's run the thing the house at 32 48 size 510 is owned by Caleb curry awesome so that shows how to use structs in your program the next thing i wanted to show is that you can actually make an array of structs so for example if we wanted to make an array to store a bunch of positions for example it could be a path we could say path and set that equal to numerous positions so inside here each one's going to be inside of curly braces and we could say where you need to go for this path so what what each point is we could then iterate through this entire array with a for loop typically if I'm going to use a number here I like to assign it to a variable and then we can basically print these values like so obviously you don't want to return beforehand you want to do that after I always forget to do that for some reason all right so there it will print out the path awesome another thing I wanted to talk about is how to make a pointer to a struct it's fairly simple so if we have a struct so scroll up we have these this my house this is a struct well we can make a pointer to that just like we would make any other pointer we use the asterisk symbol and then give it a name just like so and then we can set that equal to the address of my house okay so when you want to access the values at this pointer it's a little bit different so typically you use this dot operator such as right here but now you're going to use an arrow so you could say struct pointer arrow and then you would be able to go through here and say position or rectangle or owner so let's just say position dot X and let's print this and you can see there it prints 32 which if we scroll up we can see that we did define it as 32 so those are how to do the main things with structs inside of the syntax reference there's some more details if you need them such as how to pass trucks to functions how to return them from functions how to create struck with pointers in them and just stuff like that so please be sure to check that out if you need more practice with structs but more than likely this gentle introduction will be enough just so you understand the concepts and you can start building your own so thank you guys for watching please be sure to check out the last video which is the next one the last instructional video which is going to be talking about memory which is another really important part of C programming so don't miss out on that one thank you guys I'll see you then welcome back everybody in this video we are going to be talking about memory management inside of C and specifically we're gonna be going into dynamic memory allocation which is a really important thing to know so hopefully by the end of this video you have a pretty good understanding of the basics and you can use dynamic memory effectively alright so the very first thing is this concept of memory is very closely tied to variable scope and we've talked about it a little bit throughout this series but I never had a dedicated video to it but basically if you declare a variable here such as this well it only exists within these curly braces so if we had a function up here we'll just call it fun and we said hey we're gonna increment X this isn't gonna work and you can see we get we get an error because this X doesn't exist inside of this function it only exists inside of this main function the same thing goes if we do something like this an if statement and I'll just say true and we create a variable in here in y equals 10 well if we said hey we're gonna increment Y out here and let me get rid of this error just so that doesn't come up again the same thing happens because Y is only in existence inside of this code block so what does this have to do with memory management well essentially these variables will die and go away at the end of the scope that they're declared in this is the way we've been creating variables and we have really talked much about different types of variables so one other way of creating variables is statically so for example I could declare a variable static in here so I could say in static x equals 0 and what I could do then is increment X and just just follow with me for a second what I'm gonna do is I'm going to call the fun function numerous times and at the end of this fun function I'm going to print it all right let's compile and run so you see it gives you 1 2 3 which is interesting because at the beginning of this function it's assigned the value 0 so how is it keeping the value it's kind of odd right the reason this is happening is because when we create a static variable we're creating a variable that exists for the lifetime of the program now that doesn't mean it's available everywhere for example if we tried to access X out here it's not going to work we still get the error so the scope stays the same but the lifetime of the variable now persists and it only gets initialized one time to 0 so that is the second type of memory management the first one was automatically the second one is statically and this video is going to be about that third one which is dynamically with dynamic memory we are in charge we ask for memory and when we're done we give it back so it's a little bit more work but it's overall not too complicated another benefit of dynamic memory is that the content in this memory is accessible anywhere as long as we have the address to this memory so that means we can basically create something inside of this function and we can access it inside of this main function even though typically that's not possible if I was to say ok we have this variable and you well we wouldn't be able to do something like you plus plus down here in main as I showed you guys earlier but with dynamic memory that becomes possible because the the area of memory is going to stay allocated until we manually free it so it takes a little bit more work but it definitely gives us more capabilities especially if we want to great dynamic things such as an array where we can change the maximum number of elements those kinds of things are possible with dynamic memory now what I'm gonna do is I'm going to clean up all this crap because we have a bunch of junk floating around in here and I'm gonna clear this all right so we're just kind of a fresh state so if we create an array we have to statically size the thing meaning the size is determined at compile time and we can't change it so it looks something like this and in this situation we either have to know how many elements are up front right here and hard-code them or if we're going to get the elements from a user we just have to put a hard cap and say hey dude you can't put anything more than 50 elements in at that point you're done both of those are not really useful because the first one typically we're not going to be hard coding values into an array most of the time we're going to get that data from a database or from a user input the the other way of just giving a huge number the thing with that is that if if the person only ends up using a fraction of that we're wasting a lot of memory and the other thing is if they wanted to use more than that they can't so what we're going to be doing is we're going to be creating a dynamically sized array by asking the user hey dude how many elements do you think you're going to need so let's just get rid of this array we're not going to need it what we're going to do is we're going to create an integer size and we're going to get that from the user so let's ask him how many elements they need and then we can scan a value into size just like that now the way we allocate memory is calling a function called malloc for memory allocation and then inside of here it's going to take a size how many bytes the way you decide how much memory you need is you think of what you're going to be storing let's say we're going to be storing integers well an integer is four bytes and the person needs a certain amount so what we could do is take four times the size so how that's going to look is something like this size times and you could either put four or you could put sizeof int like that either one it's fine so this is going to allocate the memory but now we want to be able to reference that memory location so this malloc function is actually going to return a pointer so we can do int and we can just call it whatever we want let's call it R for array okay so we are basically making a pointer that we're going to be using as if it's an array let's just make sure we don't have any compiling errors yet and we do let's check okay I just got to put a semicolon we good we good all right all good so now what we can do is we can actually ask the user for each of one of the data points and it'll keep asking them until they fill up all the elements that they need so we can do that with a for loop so we're gonna go all the way from zero to size and we're just going to scan and a value as an integer and we can actually store this inside of R as if it's an array so we can use the address of operator because we have to use the address of operator with scanf because it needs a pointer and then we can just use I so yes this array like syntax does work for pointers and that makes sense because when you pass an array to a function it decays to a pointer but you can still use array like syntax so at this point it should ask the user for every single element and then what we could do is we could actually go back and print those elements just to make sure it worked so we're going to do this with another for loop and we're just gonna say our and then we're gonna say % D we're gonna substitute I in for that and we're gonna say that is set to another percent D so the first one is going to be I and then the next one's going to be R of I awesome all right so let's run this and see if it works how many elements you need bro let's say we want six now it's going to take all those elements so you could say three five seven eight four one and then it prints all of those values so it says three five seven eight four one awesome so we basically just created a dynamic array now here's the gotcha when you do this you have to remember to free the memory because if we just keep allocating memory we're going to be wasting that memory and we run at risk of either running out of memory or basically creating what's known as a memory leak where we just keep creating memory and we're not freeing it so we need to use the free function and pass in that variable the pointer alright so that should be good you are good to go you can start using this dynamic array for all kinds of fun things now when you do this memory allocation up here it's going to return a pointer of type int but there is a chance that it might return a null pointer and what this means is that it wasn't able to give you a pointer because there's not enough memory to give so in that situation the value of that pointer would be zero so you might want to check to see if the memory allocation was successful before you go and do everything with the the supposed memory you've just gained which you actually didn't so to do that before we free it let's just try it we can say hey if R is equal to zero invalid pointer error allocating memory else you're good to go now obviously this is kind of a flawed example because we're using we're using the array up here which kind of defeats the purpose but if you wanted to you could put this at the top so we could cut this paste it here and then say hey if this is the case we're just going to return zero or you could even return negative one which means something went wrong okay so let's compile and run how many elements you need bro we could put in three and it said you're good to go so now I can start putting in those values there you go now we can go back down here and free the variable you're free go I don't want you any more I never understood that movies when like people are like leave me I love you bye it's it's not you it's me it's like dude if you love the person why are you making them leave like it's come come on grow up dynamic memory is very important if you want to create a variable inside of a function and make it exist outside of those blocks so for example let's go up to the top and create a struct I'm gonna get rid of this fun function it's not very fun anymore and we're going to create a user and let's say they have a name and age and let's say they can verify their account so we have a boolean is verified which means we're going to need to include standard IOH and lastly I'm going to actually put this down here there we go it should be good go now let's say we wanted to create a function which is basically going to take these values and it's going to return a struct well let's do that let's call it create user and this is actually going to return a pointer to a user like that so this is the syntax when you want to return a pointer and then inside of here we're just going to take those values so we're going to have a variable for name and age and bool is verified all right so what in the heck are we supposed to do in this code well what we can do is we can allocate some memory and then that's going to be a pointer to a user which is why we're returning a user pointer so let's say user and give it a variable name so this is a pointer to a user and then we can call malloc and the size of this is going to be size of user then we can take the value of name and store that in the the name of the struct so we need to use the stur copy function which is part of string dot H I think that's what it's called yeah definitely what it's called and then the the first argument as you guys learned and have remembered of course is the destination new user and we need to use the arrow because it's a pointer which if you don't understand that make sure you watch the video on pointers when you have a pointer to a struct you use an arrow instead of a dot so we're going to store Nate into name and the value we want to store there is name all right now we can set other elements the other members awesome and then we need a semicolon right there okay so let's call this inside a main we might say create user we're gonna pass in the name Caleb curry age 72 surprise guys and let's say this person is not verified okay and then what we need to do is we need to store this in a pointer of type user and we'll just call it me okay so that should work and now what we can do is we can use the variable meat inside of this main function so let's focus let's just print one of the data elements we're gonna print H okay let's try it figured I'd get some kind of warning okay we need to return the user just like that how many in elements do you need zero Caleb is 72 years old awesome so that is how you can use memory management to your benefit the one thing you gotta remember though is you have to free the memory so we could say free me I'm free and just to show you guys something cool let me just print this in and make sure it's still going good cool so just to show you guys something cool which you can try on your own you could say while true and then you could just malloc and you could just pass in some huge number in here and just run this and you'll you'll see that if you're not freeing the memory your computer will click quickly run out of memory all right that's all I got for you guys for this video please be sure to check out the the same tax references for some clear reference material and some other good information that you might find useful thank you guys so much for watching this course it's an honor that you've you've made it this far with my pathetic instruction skills and my my boring attitude but I really am thankful and I'm hoping that the content of this course has given you what you need please I just ask that if you have any feedback let me know you guys can you guys can help me make better content in the future especially if I decide in a couple years to revamp this course it would really be helpful to have a list of things that people found confusing or things people would want to know more about and so forth additionally I'm thinking about creating an intermediate C programming course which is going to go into a lot more detail and some of this memory management instructs and make files and all kinds of other cool advanced topics so let me know if that be something of interest to you and if so what kind of topics would you like me to talk about thank you guys and I'll see you in the conclusion video if there is one I haven't decided if I'm gonna do it or not but if not thank you guys I'll see you welcome back everybody this video is just a conclusion to say thank you so much for watching this course I wanted to say that it really means a lot to me and if there is anything you have suggestions on questions on or things like for me to elaborate on in future videos or courses please let me know your feedback is tremendously valuable to me and I definitely want to hear what you guys have to think I didn't want to leave you hanging with what to study next and what where to go from here so I put together just a couple of topics you could study on that would help you continue your studying in C programming as well as software development in general so the first thing I'd recommend is take a look at some of some of the topics in this list so for example you can figure out more about data validation and input and output so making sure people are submitting the correct stuff when you're asking them questions how to connect to a database and work with data and then going into some more complex data structures such as linked lists stacks queue and so forth and then I would recommend you just learn more about general database sorry excuse me general software development principles such as source control make files deploying software to customers software testing and debugging obviously this is a lot of information and honestly this course could be forever long and so I didn't want to talk about everything and I just wanted to give you the essentials needed to get started I am considering releasing an intermediate Kushi course so if that's something you'd be interested in please let me know and let me know what topics you'd like to see there thank you guys and please stay in touch alright I'll see you the next time that is all for your C programming crash course let me know what you guys thought in the comments section below and what you'd like to see next oh and don't forget to subscribe peace out [Music]
Info
Channel: Caleb Curry
Views: 82,094
Rating: 4.9448161 out of 5
Keywords: c programming, c programming crash course, introduction to c programming, c programming language, intro to C, c (programming language), c tutorial, programming language (software genre), gcc, pointers, memory management, structs, linux, c basics, linux commands for c programming, variables, data types, io, input output, operators, logic, loops, arrays, strings, functions, software library, header files, multifile compilation, coding, software development, software engineering, c library
Id: 1uR4tL-OSNI
Channel Id: undefined
Length: 173min 18sec (10398 seconds)
Published: Mon Jun 17 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.