Hey there folks it's mike here and welcome to your first day in the c programming language in this series i'm going to show you a bunch of small examples in the c programming language and what i'd like you to do is follow along you're likely not going to understand every single detail of the c programming language and in fact that's not the purpose of this video the purpose of this video rather Is to just introduce you to the language syntax the features and some of the basic things that you can do and see the good
thing is c itself is a relatively small programming language so in fact what i'm going to show you is well most of the language and then i'll leave you at the end with a few of the more advanced topics that you can go on and think about or watch other future tutorial lessons anyways with that said let's go ahead And start our rapid dive into the c programming language again you don't need to do every thing or understand everything but i would like you to at least try to follow along in the examples try to
complete them the best you can and even play around a little bit so let's go ahead and start having some fun all right so where we're going to start is just in a text editor i'm going to go ahead and open a file called hello dot c Here and the first c program i'm going to write is a hello world program so in order to do so we're going to need to start off by just creating the entry point into a c program that's this main function here every program in c generally starts with this
main function so here is where code execution starts and then line by line each of these commands will be executed here All right so what are we going to do here well we're going to want to print something off so i'm going to need a library to print something off and i'm going to bring in the stdio which stands for standard that's abbreviation std here and i for input and output so input to the computer and output to the screen from here so let's go ahead and use a function i'm going to do puts which
stands for put in the s for string Hello world which is sort of the classic first program that you write in most programming languages a backslash n to indicate that we want a new line character to be printed out i'll close the quotations and put a semicolon let's go ahead and save this program which i've done below here by just hitting colon and then w in my text editor and then i'm going to quit and now if i look here i'll have this Hello.c program and in fact a lot of people may not be using
a terminal to follow along so it might be helpful to just see in the folder that i have again this hello.c program so let's go ahead and take a look at that and see what happens with hello.c now c itself is a compiled language so that means that i need to take this source code or this hello.c program and compile it into machine language and our computers understand the machine Language and thus can execute that machine language with the given instructions that we've given it in other words to print out a string that says hello
world so let's go ahead and try that now in order to do this i'm going to need a tool called a compiler that's called gcc i'm going to feed it in this source file and then in return it's going to give me the machine code as output so dash o That's a lowercase o for output and my program name which i'll usually abbreviate as prog short for program i'm going to hit enter and if successful you'll see i now have an executable program here if you're executing or following along on a linux system like i
am or on a terminal you can then write dot slash prog to execute the program if you're on a windows machine or so you might get something with A dot exe that you can execute okay let's go ahead and hit enter and we'll see our program here hello world all right so it really is as simple as that in that c programming language now in the next segment we'll go ahead and take a little bit of a closer look at c and what is going on in that simple hello world program all right recall that
we have this hello.c program and in fact i don't care What application you're editing your source code in it could be as i'm going to open up just a plain old text editor like this where you're just writing each of the commands in or you could use something that's more popular these days like atom sublime visual code and so on for me i like using the tool vim which is a text editor that you can use on the terminal and this will allow you to work anywhere so that's what i'm going to be using in
This tutorial but don't feel overwhelmed to learn that tool now let's go ahead anyways and open up our hello.c program and see what's going on here now there's actually quite a bit going on in this first program the good news is once you understand what's going on here you're pretty much all set to go with all the other c programs that you write and for this explanation i'm going to go Ahead and be ready to draw a little bit of just what's going on here so let's go ahead and just look at this source code
here this is our hello dot c program and the very first thing we do or the very first line of code we have is we're including that standard io library and what i mean including a library i literally mean somewhere going to bookshelf here's my bookshelf with a bunch of books here and one of Them let's say this one is the standard io library here and we're asking in our c program to bring it in that's essentially copying in all the different functions that we might want to use so for example this function put s
or puts for put a string here is an example of a function that belongs to this standard i o library so again that gives us access and then We have our entry point which is a function itself and i'm going to talk about functions a little bit and how they work in c but basically it has a function name some arguments in this case i haven't specified any and a return type for the function what are we returning some integer value and that's what this part is at the bottom of our code all right and
then of course since we're Entering our program we're starting execution here at line five and then moving down and one line at a time executing our program until we return from this function and then finally finish our program so again that's the body of our code in the main function here and i'll just indicate that there's a bunch of code here all right now i also did talk about this other step of compilation so i need to Save this file and usually you'll get some indication that it's been saved and then what i'm going to
do is pass this file into a tool called a compiler that was a tool we saw called gcc you might see some other ones like clang for instance which i'll write out here it's not pronounced c lang it's actually clang and basically what that does is it turns our source code here after going through gcc Into a bunch of ones and zeros and that's the machine language that our computer actually understands and then our computer essentially takes this also gluing in any library code so there's a special step called the linker which we'll eventually talk
about at some point in a tutorial series and then we get a executable file or a binary file here and that's what we execute here with dot slash Prog on our linux system and then it prints out hello world so as you can see there's quite a bit going on here and i think it's something that's going to be a little bit different than for folks who come from say python or maybe javascript as your first programming language so you might not be used to doing this step here which i'm going to label as compilation
And that means that every time that we make a change to our source code that's what's on the left here that we have to recompile our software so i'm always going to save this file run it through gcc and then test out the output i call this the sort of save compile and run loop so we'll want to remember to save compile and run over and over and over and over Again and that's going to be fine and that's a good thing to do as you make small incremental changes in your c programs all right
so with that said let's go ahead and start getting ready for our next topic all right for this lesson let's go ahead and return to the code here for a moment and let's talk about variables now remember when we're programming our machines are actually Going to need to store some data at some point and in fact let me go ahead and bring back our drawing board just so we can have a visual here of our machine executing here and here's a little keyboard and a mouse and we might want to store some data here so
let's say i have some variable here like x an x somewhere in memory is going to store some value Such as the number 42. and again that's what we call a variable and these variables allow us to programmatically store information now there is a certain type here int as you might imagine stands for integer and that means that x can always hold integer values so things like negative 1 0 7 8 1 million etc those are the types of values that Can be stored and we call these primitive types and they're built into the c
language so we don't really have to do anything with them so let's just go ahead and see an example with the code here so i'm going to get rid of our hello world type in index and whenever we create a variable it's best if we also assign it a value that way we know what's stored in it Now if you're a mathematician you're probably screaming here when you see this assignment because we can actually reassign the value to something else later on in our program but that's okay that's how programming works again variables are just
boxes where we can store a specific type of data like an integer number 42 in this named symbol x all right now our program at this point Isn't very good though so let's actually just print out that x value so in order to do this i'm going to teach you another print command here and this one's called printf for printing formatted text and it starts off by saying a string so x is i end the quotations and then the value that we want to print off but in order to know where we want to place
x in this string in printout we Use a format specifier which i'll label percent d and there's specific ones for specific data types such as x being an integer so it uses percent d and it'll essentially substitute x into this position so let's go ahead and run this now i'm going to go ahead and do something with my terminal a little trick where i can split the screen that way you can see the code in one window Here while i compile in the other window so let's go ahead and do that again gcc our compiler
the program that we've written hello dot c dash o and and it compiled fine i'll run it and we can see our value here x is 34. so great we've successfully allocated some memory that is somewhere on our machine here in the c programming language we've created this variable x stored a value and we can change that value if we like and then finally print It off now there's lots of other types of variables that we can work with things like floating point values so let's go ahead and create another one float i'm going to
call this one pi as it might be something familiar and we can assign it some value here and usually we put a prefix for some of these types here like a float of f after it so let's go ahead and do another print Statement and this time substituting in pi a different format specifier because it's a different type percent f and what we want to substitute into percent f which is pi and let's go ahead and rerun this by recompiling it and re-running and we can see the appropriate output here now you'll notice that a
floating value Has several significant digits so it added a few zeros here when i printed that off and just to make things a little bit more concrete let me go ahead and show you how i can comment out that is avoid these commands from being executed or compiled and let's just go ahead and do the same thing but with just one printf statement so printf x is Percent d then i'll do an n line and pi is and i'll do percent f for floating point value n line comma x comes first then pi and then
a semicolon and i'm going to extend this screen just a little bit so you can see everything on one line because i've made some changes i'm gonna make sure i save My file has been written recompile rerun and we'll see our output here now there's extra space here because it literally put in a space here but i'll leave that for you to fix all right let's move on to our next topic so something that can happen is if you want to store a lot of data and you have to sort of think about how much
data there is in the world well there's a lot There's well more than megabytes then there's gigabytes terabytes petabytes exabytes and more but let's just start with something small here what if i want to store say five values well we've learned how to use variables and we can actually just create five variables so i could have x1 and then i could have x2 x3 X4 and yet another x5 and we could store data like this one at a time now you could imagine though if i have hundreds of thousands of variables that this would get
very very tedious and honestly it's kind of a waste of space to store all this here so instead what we have in c is a data structure built in and that data structure is known as an array other programming languages again if You're coming from python don't really have a native array data type so allow me to explain how this works so again let's try to store 5 integer values so what are we going to do instead what we're going to do is go int and i'm going to abbreviate this as array or that's the
variable name and then how much data we want to reserve five here between the brackets and this gives me five consecutive Locations in memory to store my data so i'm just going to represent this as such and i have my array here so array with five elements in it and in c we index from zero so the first element will be one two three and four as follows now let's actually populate this array with some data and i'll go ahead and do that one at a Time so zero and i'm just going to give a
random number 23 and oops a few too many random numbers let me just do uh one at a time here array 20 at zero equals 23. array 1 equals 0 and these can be any integer values negative 7 array 3 equals 45 And array 4 equals 76 here so now i've successfully stored a bunch of values one after the other here and again i'll show 23 23 here zero negative 7 45 and 76 and an array is what we call a data structure And it's a data structure because well we're storing data and we have
it structured one item after the other and this is a nice way to collect all of our data in a contiguous block of memory that's what this is here because remember we're eventually storing this all in our memory and this is getting to the point of where we're starting to think like a c programmer we're thinking about these Data structures like arrays and how things are stored in memory that's one of the reasons to be learning c by the way because it makes us think about memory in some of these lower level concepts and it's
a great way to learn about how to optimize your code and write really really efficient data structures alright so let's go ahead and print off these values here and then we'll move on to the next lesson Now i'm just going to print these one at a time like we did before array 0 and that value equals some integer value and n line and we'll do array 0 here and repeat and we need to update the indexes in each of our strings and the actual variable that we're substituting make that 2 1 And let's go ahead
and recompile and rerun this and we can see our array printed out and i'll make it just a little bit smaller so you can see everything on the screen here at once all right so now we know how to collect all of our data in a contiguous block now some other notes about arrays is all of these values have to be integers because remember we declared this array as a Int array here so they can't have floating point values or other types of values in them they're all guaranteed to be integers all right in the
next lesson we'll look at a little bit of a more efficient way to print off some of these array values all right so previously we've looked at arrays but typing out all these printf statements became a little bit tedious in fact initializing all these array Values was also a little bit tedious now it's always good to initialize our values so it's nice that we initialize them to something but let's go ahead and take care of printing out all these values and we can use a programming construct known as a loop so let me go ahead
and demonstrate what we're going to learn here with a 4 loop and a for loop allows us to Do some condition over a period of time or a series of iterations so let's just go ahead and look at an example and see how it's used and then we'll formalize it a little bit so what i want to do is just say 4. and i'm going to create a integer here i equals 0 and while i is less than 5 5 because well we have 5 elements in our array that we want to loop through I
plus plus or i can just write i equals i plus 1. and what i want to do is printf the array the iteration that's i here or the index in our array and then print off what that value equals that'll be our second integer here and then an end line so again we'll have two different parameters here for our Printf the first being i or the index into our array and the second being the array at index i and let's go ahead and compile rerun and we'll see that we get the same output so we
can see that this for loop made our code a lot more efficient and it also expressed clearly what our intent is to do in the programming language that is that we want to loop through all The elements in this array so how does a for loop work well we have the for statement and then we have sum initial statement here that we want and that was setting i equal to zero so this is essentially creating some variable that's going to exist within these curly braces here for this for loop and then we have a semicolon
And then i say i less than five which is in other words just some conditional statement so as long as that conditional statement is true that is i less than five execute this code and at the end of our code we have some work that we can do here and this work that we can do is just some expression That's being evaluated as soon as we hit this curly brace at the end then we're saying increment i i equals i plus 1 or you can do what i previously did as a shortcut and just say
i plus plus which also just increments i by one so i'll go ahead and make that change rerun compile to show you that it does just the same thing so that's an example of a for loop we'll go ahead and look at some examples of while loops next So in this example let's go ahead and look at another type of loop in c known as a while loop so we'll learn by example while some condition is true so i need to create some sort of integer here and i'm just going to call this one countdown
set it equal to 10 and some condition while countdown is greater than let's say zero then let's go ahead and print out the countdown Variable here and i'll actually just put a percent d here and then the countdown variable and then i will subtract or shrink from countdown you can use minus minus or countdown equals countdown minus one here so let's go ahead and see what this program does since i've made some changes i need to recompile and rerun and we can see the appropriated code Here doing 10 98 and so on until we reach
one or that is rather as long as countdown is positive then this loop will continue executing so just to break out the anatomy of a while loop i have while some condition and then the associated curly braces and whatever my code is in between that will execute every iteration now there's also do while loops which will flip the execution that is you'll have a do Statement curly brace and then the while portion will evaluate the condition at the end so it's guaranteed to run once so you might see both in the c language but more
commonly you'll probably be working with while loops so with that said we'll move on to our next example so maybe this is your first programming language that you've used or maybe it's Your 10th but inevitably you are going to make a mistake and create some bugs so i want to go ahead and show you a bug in c so let's say we have our code here and i'm gonna go ahead and just make this a little bit bigger so that you can see everything um and let's say that we do something like forget the semicolons
now the semicolons in the c programming language terminate A statement so here's an example of a statement countdown equals 10. it's very firm in what we're saying here so we terminate it with a semicolon so that we can move on to the next line but let's say we forgot that so the c programming language doesn't know when we've ended our statement so i'm going to go ahead and try to recompile this program and you'll get an error message here and sometimes the c programming language And the compilers like gcc can be pretty good at telling
you the message it says expected a semicolon before this bracket here so we can go ahead and just put a semicolon there and then we'll go ahead and recompile rerun and our program is happy now sometimes when we run into bugs like let's say i forgot a curly brace here after main and again i'll try to recompile and you'll notice i only really made one Mistake but i'm getting all these errors here because they sort of cascade from this top air and then those bugs cause other bugs which cause other bugs and my warning to
folks is when you see this not to panic sometimes it's just a small little bug that you made in fact instead of seeing all these multiple bugs here you probably only really made one that you have to fix and oftentimes that will fix the other bugs so If i were to debug this i would start from here and say okay the main error happens around line six here that's the information that it's giving me from the compiler and i would look at this and say okay this looks fine but maybe the line above or maybe
the line below is causing problems so if i look to the above here i can see i forgot this curly brace in our main function so now i can go ahead and Rerun my program and recompile appropriately so the lesson is don't panic and try to fix your bugs from the top and as you're writing code make sure that you just make small incremental changes as you're learning the language save often recompile rerun or refix your code and then repeat that process so these curly braces that i've talked about in some of the lessons well
i haven't exactly described what those are and C is a curly brace language so you have a left curly brace and a right curly brace and they match in a pair and what those define is the block scope and that is a way to sort of divide our program into different functions different blocks of code like the example with the for loop or the while loop and ultimately provide some structure to our program So let me go ahead and show you an example of how the curly braces enforce block scope so if i create some
variable here like x and assign it to 42 i can safely print out x is its value just like we learned here and this program should work so i'll go ahead and compile it just so you can see now if x is defined in a another curly brace within here and i usually indent my code For every left curly brace that i have and i'll try to match the right curly braces then x's scope is just within these curly braces which it was defined so x's block scope is within this curly and this curly brace
so as soon as we leave this curly brace well we have no notion of what x is so i'm going to go ahead and try to compile This program i'm going to have to extend this window here so you can see the errors and it says x undeclared because well we don't know about x it's hiding between these two curly braces or that's the scope and the purpose of this again is to be efficient and to allocate resources just between these curly braces and then as soon as we're done our computer program while it's Running
will get rid of x in your next lessons in c we'll talk about how this works with something called the stack which automatically clears your memory or in other words your local variables that exist between these curly braces and that might be a familiar concept for folks who have programmed and the same thing applies in the c programming language so now that we know about curly braces and how they sort of Separate out our program we can start looking at how to create our other functions in c so we've only been working with one function
so far that's the main function remember that's where our program starts but what if we want to write our own functions in the c language well we'll look at that next all right let's go ahead and look at an example of functions as you know functions themselves Have to be fun and i have to make that joke so let's go ahead and look at functions anyways and i'll write a function here square and it'll take in one parameter and it will return x times x so let's go ahead and see how this works and then
we'll slow down a little bit to explain what exactly is going on so i'm going to go ahead and do printf here the square of 7 and then an n line Well let's say what it equals the result and then i'm going to call this function here square and pass in seven and a semicolon and let's go ahead and compile this program and run it and we'll see the square of 7 is 49. 7 times 7 is 49. so there's actually quite a bit going on here we're calling this function from within another function printf
and we've defined our own function here so let's Try to break this apart just so we can start seeing how the c programming language works so let's start with square here so the basic idea is i have int here square and then within parentheses another int and then x here so what's the anatomy here with this function here Well this is actually the return type and what this is stating is that we're returning some value that's going to be an integer okay so some value that's negative say 2 billion to positive 2 billion and a
whole number or just some integer value we have the name of the function which we've uniquely given it this is how we're going to call our function so this Is the name and then we have our parameters we could have no parameters or pretty much any number of parameters though usually five or less is normal and this specifies a temporary variable in our function and we've just named it x in fact just to give it a little bit of a better name because we're using x a Lot let's just call this arg this is the
argument so i'll update this to be r times arg and you'll see it works just the same and it's probably good to name our variables good things or have more description to them but again just like all of our other variables they have a data type so arg is always a integer value all right and then we of course have the Body of our function that's between the curly braces and that's where we do our return statement so return and then whatever our return value is going to be some sort of integer so x in
this example or arg if we're looking at our code times x well multiplying two integers will give us another integer value back so we're okay there All right so that's the basic anatomy of our functions in c we have the return type the function name any arguments and then the return and then we have our body of code between each of the curly braces here and that defines the scope so where things are going to exist in our functions so let's go ahead and write another function here just to make the point more clear and
in this example i'm going to have no Return type which is also an option because we might not need to return a value so i'm just going to call this void the function is going to be hello it'll take no arguments so sometimes you'll see this empty or type in void the curly braces and i'm just going to use puts and say hello with an n line and we'll go ahead and call that function hello And let's go ahead and recompile our program run it and we'll see that we get square and hello printed out
here all right so now we can see how we can write these functions and they become a little bit more powerful so let me go ahead and clear up a few other concepts here because it's interesting that we're able to call this function as sort of an argument of printf and That's allowed this is going to be evaluated first and then we'll fill in that value of the integer returned here just like we've previously done with printf but just to make that a little bit more clear i'm going to go ahead and just create a
variable here i'll call it result and it's equal to the square of seven and then i'll just put in the result Here this is also legal and perhaps a cleaner way of writing our c code so i'll recompile rerun and you'll see we get the same exact result all right and the other detail that i want to show in the c programming language is that we have to declare our functions that is put our actual functions before we use them so if i take this code and i put it after our main program and now
i try to recompile this I'm going to get a bunch of error messages here and it's going to warn me which well we haven't seen warnings yet but warnings are as good as errors if our tool that is our compiler gcc tries to be smart it might reorder our functions but it's warning us and saying hey you're using this thing square before we know about it because if we look at our code here in main we see square but we have no idea what It is it's way down here remember our computers are pretty simple
they're just reading line by line by line trying to figure out what's going on so typically we'll declare and define our functions before we use them now there is a way to get around this that you might see in some c code where i provide the function declaration And put a semicolon and say hey this exists and this matches the exact return type the parameters and the name which i should match exactly here with arg and then that tells my compiler or gcc that hey this square will eventually show up so i'll need to do
the same thing for our hello function and then i can recompile And no problems here and let me go ahead and just show this all on the screen at once so you can see what happens and again we can declare that our functions exist now we can use our functions and here's the definitions of our functions and again a definition means that you're defining how you actually do the work here's what you're Returning or here's what you're printing out with your hello world so there's all the code for you to take a moment to look
at and we'll go ahead and move on to our next example all right if you've done programming in other languages you maybe have heard of the concept of recursion and c like other languages allows you to recurse and call a function from itself that's a little bit confusing so let's Go ahead and show an example if this is your first time with recursion don't worry we can look at many other examples at a later time or you can look at some web resources but i want to go ahead and show you that it's possible in
c so i'm going to go ahead and write the countdown function that we previously looked at and it's going to take n which is the value at which it counts Down and in order to write a recursive function we need to have our sort of base case or the terminating case and for that i'm going to say if n is less than 0 then we will return otherwise if that's not the case or otherwise i can just return by calling the same function again with A value of n minus 1. so n is always shrinking
here and then i'll go ahead and print off the string here for whatever the value of n is percent d and then and that's passed in here so let me go ahead and make this a little bit smaller so we can see all of our code and i'll call our countdown function with a value like 10 we'll recompile And rerun here and we can see our countdown working so here's all the code as it is so let's go ahead and slow down just a little bit with recursion and the idea is in our countdown function
and let me go ahead and label it here countdown i need to have some sort of base case and that's sort of the smallest value or When things terminate so it can be the base case or the terminating case and then i have the recursive step how am i making this problem smaller or how am i approaching my goal well in the case of our countdown we're shrinking every time so that's the goal the recursive step where we shrink or approach our base case now this isn't necessarily a lesson In algorithms nor is that the
purpose but i just want to show you that recursion is possible and then we're going to continue a little bit further with this example just to show you how to write some better c code in the next lesson all right so there is a way that we can go ahead and improve this example because what might happen is what if a user tries to count down from negative 10 Well if we're shrinking every time what happens well let's go ahead and run this code and well it doesn't really do anything because we just return here
so we might want to add some safety to this function so we can do something like make the value unsigned so that means that one of our primitive types here we can qualify and state that it must be a positive Value that is without a sign no negative in front of the value so i can go ahead and try to run this and it'll give me some sort of positive number and it'll probably crash though because well i still passed in negative 10 here so what can we do to sort of enforce writing good code
well the c language many folks say is dangerous but there's ways that we can be sort of safe programmers so what i'm going to go Ahead and do is include something known as an assert assert.h and in my actual function here i'm going to write what's known as an assertion statement so how do i do that i write assert and i say n must be greater than or equal to zero and i can put in two and science to give me a warning and say n was not greater than zero so this asserts every time
in my code That n or any time this function was called must be greater than or equal to zero otherwise it's just going to crash the program so we won't get that sort of unpredictable result here when we have negative 10. that's not allowed to happen when our program is running or in other words during run time let's go ahead and compile and i'll try to run But strangely when i run this it's still sort of crashed because of how c again sort of treats these unsigned numbers so what i can do or what my
recommendation is when first starting off with c is just use an integer then use this assert here and we'll go ahead and try to run it so in case users do provide a negative value we can try our code and we'll get the Assertion message with the warning here telling us that well we provided a negative value so by default on your first day and see just rely on simple types like integers and you can write little assert statements to help make sure that your code is safe all right so with this lesson we've now
branched out a little bit to understand that there's more different libraries in the c programming language that we can take advantage of pretty cool huh all right Let's go ahead and continue on forward so with some of our countdown examples that we've had previously we've seen the if statement pop up so it's time to now talk about if else if and else statements so let's go ahead and rewind with a little simple example creating first an integer and setting it to some value here now in c we have if and then we can do conditional
statements i could say if this is greater than or equal to zero And x is less than 50 then go ahead and print off x is in our range and this is perfectly valid to do so let's go ahead and run this code just to show that it compiles again working from simple examples and building up and we can see that's in our range and we can also instead of using and use a boolean operator to say if x is greater than or Equal to 0 or x is less than 50 which it satisfies both
of those conditions but as long as it satisfies one we're okay so let's go ahead and run this x is in our range let's go ahead and just set it to negative 42 to see that one of these conditions is true and again x is in our range prints out now let's say that we want to do something with else conditions so we could say if x is greater than zero We'd say x is positive and it's a positive integer otherwise we would say and i'll just print off x is either zero or negative and
let's go ahead and run this to see what our result is and x is positive here and if i go ahead and change this program And i'll resave that means i need to recompile rerun then x is either 0 or negative so this is our if and then else condition so if this is not true then execute this block of code and then in c we also have else if conditions so we could say if and it will evaluate this condition first else if x equals zero then we could print off X is zero and
i'll put an n line there otherwise if it's neither greater than zero if it's not zero then it must be negative here and i'll go ahead and update our text here so we can go ahead and rerun this example and see what the value is all right now me having to constantly recompile every time i change this value Was a little bit annoying so in the next lesson we'll learn about how to get input in from the user in our programs all right so let's go ahead and modify this program a little bit so i
can determine what the value of x is from the user so what i'm going to go ahead and do is just assign x to 0 here and then we're going to put out a message here so i'll put out a string enter a number for x colon And a semicolon and then a new function called scanf so scan formatted text well what kind of text are we reading in well it's an integer so percent d and then i'll close it off and then we're going to use ampersand x because this is the value that we're
going to be changing now why the ampersand well that's going to come in the next lesson just bear with me for a moment why we have that ampersand so what we're going to go Ahead and do is run gcc we see that our program compiles i'll run it and says enter a number for x let's try 42 x is positive and now i'll rerun that program i'll put in zero x is zero i'll rerun it again negative 50 and x is negative so scanf is a way for us to read in formatted data if this
was a float i could put percent f and as long as x was a float this would also Work so that's the idea here now where does this ampersand come from this is one of the mysteries of c but also one of the reasons why we like the language it lets us see some low level details which we'll get into in the next lesson all right let's go ahead in this lesson and revisit this idea of ampersand and i'm going to go ahead and just clear up some of this terminal here and give us a
fresh start here so To understand what the ampersand's doing let's go ahead and just create a variable x and we should always initialize our variables so i'll set it to 42 here so visually what i have here is some name location x in my computer so i'm just going to draw a box here and it's labeled x and stored inside of this box is the value of 42. so any time i use this variable x in my program it's just going to look Up the value in the box and send it to us but where
does this box actually live and you could sort of imagine this like an actual mailbox on your house here and i'll try my best to draw this actual mailbox here and maybe you have a flag here if you have mail at least this is how our mailboxes work uh in the states here and if i want to see what the actual address is here you know your street Name and the location you know one two three or whatever it is how would i get that address of this box so what that give is the ampersand
gives us this is the ampersand operator which will return to us the actual address of this mailbox or where we live in other words so we can think of ampersand again as a function so i'll go ahead and show it to You in how it works here so let's go ahead and just do printf and what i'm going to do is print out some number here and i'm not going to print out 42 but i'm going to print out the address of x here and let's go ahead and see what this gives us in our
program if we recompile this expects an integer but the argument has a return type in star Okay this is a little bit strange here if i try to run this it's just going to give us garbage some negative value here so what i really need to do is pass in another format specifier percent p and this is going to lead us into our next topic here but now it compiles without any problem and if i run this you'll notice i get some address here and you'll notice that there's a Zero x here and then some
number here seven f f d six five eight three four seven zero four etc now your number might be different because we have different computers and my computer is storing x in a different location that's fine but this here is the address of where x lives so this mailbox here and it stores inside of it 42. now how would we get from this address what's inside of the mailbox well for That there's actually something cool so this statement prints out the address that's what percent p format specifier gives us if we want the value in
the address of x and you can think of it as ampersand x or ampersand with the parentheses around it we can instead use this asterisk that popped up so this Says give me the address of x and this asterisk out here it doesn't mean multiply but rather what it means instead is look inside of this box and from this reference or this memory location give me the actual value so let's go ahead and run this and again we'll see the warning here because the specifier is not right because What is x well it's an integer
so let's go ahead and make sure in our format specifier we pass in percent d i'll go ahead and run this no warnings and just sort of like magic we'll see we get the value 42. now this is where we need to talk a little bit about another topic in c programming which is known as a pointer so let's go ahead and dive into that in the next lesson so in this lesson i want to talk about a fun concept known as pointers they're Really powerful in the c programming language and one reason why we
use it they give us this low level access so what exactly is a pointer though well a pointer points to something that's one way to think of it or the other way is just that a pointer stores a memory address and we've just learned how to get Addresses with ampersand okay so let's try to connect these ideas by looking at a few short examples and remember this is your first day in c so we just want to try these and try to wrap our head around it just a little bit we'll internalize or understand the
point of pointers a little bit later on as you get some more practice in your c career but let's just start off with the basics so again let's just create some variable X equals 42 here and we've learned how to get the address of x here so let me go ahead and print out that address using percent p so the address of x is and we do that by using the ampersand x and again if you want to think of ampersand as a function i'm going to go ahead and wrap this and pretend x takes
an argument that's also fine close or balance our parentheses and semicolon so let's just go ahead and Start with this much here recompiling and re-running our program and this will probably look a little bit nicer with an n line so let's go ahead and run this here and we'll see the address of x is well what i've displayed here again it might be different on your machine okay so if a pointer here can store a memory address how does that work well first let's declare a pointer declare a variable that is a pointer Okay so
what does this mean is i'm going to have some variable here and i'm going to call it p of x and its job is going to be to store some memory address so something that's zero x you know f f whatever the address is all right so how do i declare a variable well to do this there's a special syntax int star or int asterisk and this is a pointer type you can think Of the star as a point but it sort of belongs to the integer here and i call this p of x and
since it stores an address well i need the address of something let's just store the address of x and let's go ahead and print it off so i'm essentially just going to copy this line here and let's paint it here or paste it right there and i say p of x is And let's just go ahead and print out the value of p of x and if p of x here p x stores an address we'll use percent p to print out address values let's go ahead and just compile this code and if i run
it you'll see the address of x is 0x7ff etc and we have a matching address stored in p of x so pointers store addresses And i can point to other variables so essentially what i've done here is i have this other box x here which stores some value 42 and p of x stores well whatever this box is now i'm not going to draw into the box but it just knows the location here zero x f f whatever and if i want to get the value inside of the box i have to de-reference and that's
where that asterisk came in here So this was creating a pointer and this is accessing the value to the thing we point to so let's go ahead and do that and i'll do that just with a printf here and star p of x value or the thing we point to the value of the thing we point to is and this is specifically an integer Pointer we can point to other integers like x which is an integer we could create a float star which would be a floating pointer it could float to or point to other
things that are floats and the type matters or even if we have custom data types which we will reveal shortly so that's the idea here so percent d that's the thing that we're storing and we're dereferencing so the star P of x and again i can think of this as a function the asterisk and it goes behi before the variable just like the ampersand went before the variable that we were trying to get the address of so let's go ahead and reveal that it compiles and it runs so we say star p of x the
thing we point to the value is 42. now this is pretty cool and you might be wondering what the point is and i'll end The video showing how we can build data structures with this but allow me to just give you a little bit of a teaser here if i created a second pointer here and i'm just going to call this px2 and it also points to the ampersand x now let me just go ahead and create all these variables at the start of my program here And p of x 2 which i'll draw on
our picture here and let's go ahead and label it appropriately px2 also store some address but if we're pointing to this same box here that means we're effectively sharing the value so pointers are a way of letting us share data By just pointing to one consistent location it's sort of like if my information was in a database and a bank sort of points to my information and some tele company or you know cell phone provider points to my information and mult in my employer points that same information we'd want it to be the same so
that's sort of the idea here so anyway let's go ahead and just print off if i put asterisk p X 2 here or in other words d referencing then let's see that we get the same value here so if i recompile rerun p of x when i dereference it is 42 and px2 is also 42. so try typing this out try drawing this out and then you'll sort of get the point of pointers we store addresses and when we use equals we assign or point to things again it's your first day in c so you
Might not 100 get it but understand that it's a very powerful programming concept so we've just learned about pointers that they point to memory locations and we've learned about some other things like arrays and i want to motivate a little bit more why pointers are powerful or a cool thing with something that we've learned previously arrays this idea that i can allocate say five items together remember we had this piece of code here int array And we said five so this array was always going to be 5 elements large right and we 0 index 0
1 2 3 4 and we put in some values here 23 76 0 etc etc i'll just make up some numbers here all integer values stored contiguously in this block but what if i don't know what this size is going to be should it be 5 should it be 10 should it be a thousand or maybe the user should decide or maybe you're loading some data From the internet and however big that data set is that'll be the actual size that you need so in other words how many boxes we need we can't always determine
before we save compile and rerun our program so this is where we bring in two functions known as malik and free and they have to do with dynamic memory allocation and we use a pointer to help us with This so in other words malik means memory allocator and free means well freeing that memory essentially just getting rid of this storage that we've requested so that our machines can use that memory for something else so let's go ahead and look at an example here now for this i'll need to include a new library std which stands
for the standard library and then dot h Okay so let's go ahead and see how we use this so i'm going to need to create a pointer and i'm going to call this dynamic array and it's going to be equal to well malik and this is the memory allocator think about this as some program if you like in your operating System that hands you out memory when you ask and it'll hand you out as many bytes as you need so how many bytes do i need 4 8 12 it's up to you that's what the
parameter is 112 but there's a little bit more of a disciplined way to handle this you can use size of and then how big the data type is so an integer and this function will return you the size of an integer which is usually four bytes And how many of those do you want do we want a hundred maybe just five for this example let's go ahead and run with that so that's the malik function now i'll go ahead and compile this program and i'll run it and no problem well almost no problem here i've
asked for this memory while our program's running but i never released it when i was done and that's the polite thing to do we need to before our Program quits free this memory so we use this function here free and we free our dynamic array so then we are done with our program and it's executed successfully and we use this dynamic array here because we've allocated five Integers in a contiguous block again and we're pointing to them here so we can just use our arrays as we did previously here's the first element and we'll assign
it to 23 and let's go ahead and do all five of our elements that we use just as a normal array here so let's do 76 0 5 and 81 so matches our diagram All right so nothing really new there in the sense of we've learned what happens except this is again the point of a pointer here that's called dynamic array so i'm going to go ahead and get rid of this and label our dynamic array here the variable that we created and that's a pointer so it points to things so when i assign it
equal to malik this gave us Five boxes of memory so i'm going to weave my arrow around here and point to the first chunk of that memory and then the assignment here zero one two are the little offsets into each of the boxes here i'll offset by zero from this initial chunk here offset to one offset to two offset to three offset to 4 etc as i access each of the elements in this Array and this is what's known as a dynamically allocated array here we assign it with a pointer ask for some memory while
our program is running and then we assign values just like we normally do with arrays and then free that memory when we're done with it so that's also one of the points of pointers it allows us to do this at runtime where we can ask for the size here now again just to show how this is a Little bit more dynamic let's go ahead and create a variable called size of array and i'll assign it to 0 by default we'll use one of our functions we've learned about before scanf percent d because we're reading in
a integer value and we're passing in the size of the array because this is the address of where this box lives And we use an ampersand which i'll have to talk about with functions momentarily and now i can dynamically size this array by just passing in this parameter here so then when our program runs one line at a time setting up the size of the ray reading in that value from a user and then allocating while the program runs the appropriate amount of space or the number of boxes that we need and Then we can
fill them in using say a loop here and let's go ahead and do that four and i equals zero while i is less than well however big our array is that we asked for we'll increment and just set up each of the elements in our array at index i equal to well just 0 for now it's fine so let's go ahead and run this program just to show that it runs it doesn't Really do anything useful i've got to add how many elements i want to allocate 99 and then it will set each of those
99 elements to zero and then free the array and exit our program anyways i hope this gives you a little bit more intuition in fact this is sort of our second data structure we have just regular statically allocated arrays which we fix the size or fixed size arrays and this new dynamically allocated array so let's revisit this example or at Least one part of this example briefly why am i passing in ampersand size of array here why does the ampersand have to go in here and not just the variable name well i can give you
a quick answer by just closing out this code and showing you one of the useful resources in c here and that's the help pages so we can sort of just guide ourselves i can type in Min which is short for manual and scan f and on the terminal it'll actually show me the pages now you can also just google this command and you'll get some help as well but it tells me what library i need to include that's why we've been including standard io and then the particular function we have and then the parameters are
well how we're going to format things so that was the percent d And then i just see dot dot now this probably isn't the greatest function to start from but the idea is you eventually need to pass in some variable here and the address of that variable is what's getting filled with some data so let's go ahead and look at writing an example here um of how to or what happens with this ampersand size Of right here so i'm going to go ahead and clear up this code here and write a function here and it's
going to be called set value and i'm going to set a integer value here now first let me just try it with int and then new value and i'll go ahead and set new value equal to 9999 here and we'll go ahead and see what happens here so we'll create and x set it equal to 42 i'll call set value i'll pass in x and then i'll go ahead and print out x is and then whatever the value is here so go ahead and pause and take some predictions as to what is going on here
so i'll recompile the code and it compiles and in three seconds i'll run it three two One did you make your prediction x is still 42. all right so what happened here so c has something called pass by value so pass by value and what this means is we make copies Of variables when we pass them into functions so pass by value here so essentially what this means is when i call the set value function here set value and its first parameter is new value new value And if i call it with x here i
get a copy of x here i don't get the actual x that we set up at line 42 but instead a new copy is made that new copy of x is set to 999 and then that copy goes out of scope when we leave these brackets here because again the c language is trying to be efficient now if i want to change this value because remember our goal was to do something like with scanf where we were passing in some Format to tell us what kind of value we're working with and then ampersand some value
so how do i pass in an address to a function go ahead and take a moment to think about that and if you thought about it for just a moment well what stores addresses well pointers so i need to put int star that's the type here that i have And now i need to well pass in the address of something so if i pass the address of x that's giving me the actual address of where x lives so that actual location now is what's going to be changed here but we're not quite done here because
well how do i access the actual value well in order to do that i need to dereference so i'll use the star here and i'll put parentheses just To be clear and now if i run this program what's going to happen and make your predictions in three two and one and you'll see that x after this function has been called since we're passing in the actual address of x does in fact get changed so this is why in some functions like scanf where you see an address being passed in that's because we actually want to
change that Actual variable and that's known as pass by value semantics all right so you're going to see that a lot in the c library for example and we'll go ahead and move on to our next example so now that we've talked about pointers past by value arrays and some of these concepts i want to start putting some things together and one of the things that we have been Missing is strings we have been using strings in the sense of well whenever i call one of these functions like put s or put string between the
quotation marks i have a string here so instead what i want to do is actually show you how to write a string literal and how we represent that so we use one of the primitive types here char for a single character and a Single character would just be something like this here and i can try to print this out here let's just see if it'll actually print this out here and it's actually going to complain here and if i look at the function signature for put s here which i'll go ahead and split this window
look in our manual page which you'll get in the habit of We'll see that the put function if i look at it for put string takes this thing called const char star s so what does this exactly mean here that our strings are taking it well let me go ahead and close this and again any time that i have some pointer here so i declare that with charstar that Means i'm pointing to the first in first character because that's the type here and again this is pointing that's what the star means in a chunk of
well however big our array is so let's first look at this with a string little and it literal means just a literal string here so it could be something like hello so let's go ahead and try this out so instead of just drawing one character i want a block of memory here i'm going to Point to some chunk of memory here and with two quotation marks not just the single quotes this will make a string here and i'll put hello and now i'll try to recompile and i'll rerun this program and i'll get hello so
that's the idea of a string literal now since this is a literal string stored in memory and it looks like i have an array here so 0 1 2 etc Can i actually access each of those individual characters and how would i do so well what if i did print f and i said well in this array which i've named a here a of 0 let's go ahead and try to print this off and this would be an individual character so percent c a of 0 and i'll go ahead and compile this and well I
get h here and i'll go ahead and put an n line here just to make it a little bit easier for you to follow along with if i rerun it there we can see it is now what if i want to actually change a of 0 and say it equals now b and let's go ahead and just do this before we print it off to see if the change is reflected and i can pilot And this time i get something called a segmentation fault so it looks like i can't just change the string literal here
because this is sort of a constant value so there's actually a different way to do this in c where i'll get rid of the star here and i'll put the brackets after the a and c is smart enough to figure out that this array is Of characters is going to be 1 2 three four five characters or there's actually a sixth character but i'll get to that in a moment so let's go ahead and just try to run our program again rerun it and this time it works so there is a difference there is a
difference between char star a and i'll put on a separate line here char a with the array here This is again a string literal that says this is a literal string you cannot change it because it is literally just hello somewhere represented in memory versus this which is an array and arrays are pretty flexible we can index into them we can change the values and so on so remember the brackets with array and for strings remember the special case where you have a char star and can't change it now getting to the length of this
Well when i'm printing off this array here and in fact let's do this looking at put s for the string how does it know where to stop how does it actually know that it's not hello bob or hello mike or hello whoever well in fact what c does automatically for us is it puts a terminating character and this is called a null terminator with a backslash and a zero that's a null character that says End this string so it's as simple as that one two three four five and then this backslash and a zero is
the sixth character so that's how c knows how to terminate strings and if we want to go ahead and see this we can use a handy function here called stir blend and it takes in some sort of string here and it'll return the size here that says the stirline function Calculates the length of the string pointed to by the s excluding the terminating null byte here so it should give us five characters here in order to use this function we'll include string dot h here so let's go ahead and do that include string dot h
and let's go ahead and just print off the length of our string here so i'll do it here print off length of string a is And we'll put percent d here and call the sterling function on our string a and let's go ahead and see what we have here now this is saying here that we're getting a warning here so let's go ahead and look just a little bit closely at that function here and the warning is fine it'll probably still run but we want to take care of warnings in Our c code so the
manual page stir len and this is going to tell us that it's returning a type size underscore t which is actually a sort of positive uh long integer so it's telling us the percent uh ld is the correct specifier for the value that's returned here so let's go ahead and amend that and follow the recommendation of the compiler here and if i go ahead and rerun this code and run it again we'll see the length of The string is 5. but we will want to be careful here because the length of our string is actually
5 characters plus this null terminating byte because again the c language and the functions where we pass strings into have to know where to terminate the string and that's the null terminating character all right folks so then we'll do one more lesson and then we will wrap things up with a little data Structure that i promised way in the beginning so looking at some of our previous examples it's been cool that we have strings floats string literals we can point to data but what if we want more complicated data types beyond just our primitive data
types that we've had previously well one way is that we can compose primitive data types or other custom data types that we've made into a structure and that's exactly what a Struct is in c so i'm going to go ahead and just remove this here and show you how to create a struct so a struct here is some way to again structure data together and i'm just going to call this student here and then put a semicolon here and some attributes of a student so again you could think of this like an index card with
all their information maybe their Age maybe their id maybe their average grade which we'll just store as a character and if we wanted we could have maybe some array storing their name let's just go ahead and leave that as is okay so if i want to create a student i type out struct student and i'll use myself an example mike and then i can index into each of the Fields by saying mike age equals 500 and then let's go ahead and just print off mic.age and this is an integer and i'll do an end line
mic.h and let's go ahead and run this much of our program and see what happens so we can see mic.h here all right and let's go ahead and add the rest of our fields i have my id which will also be One two three and i'll have my average grade which come on folks a of course so let's go ahead and print these off uh id if i'm being realistic it was probably a b average so let's go ahead and leave it as is average grade here and id So if i run this here uh
oops looks like i missed um one error here again let's look at our error message before i just go ahead and fix it but again always starting from the top because we see this error here we'll go up here look around and find that we need to close that right parenthesis now recompile rerun this and we'll see Our printout here which i can improve it and add some spaces and these kind of things here sometimes i like to align things uh nicely here let's go ahead and do that for this example just since we're getting
close to the wrap up here of this lesson here um but just to visually sort of show what's going on is when i create this student mic Which again is a structure what i have is these three fields that belong together again the age the id and then the average grade here and this is what makes up mic here so that data is sort of packed together and travels together it's it's structured together and we could pass in this struct mic into functions for instance There is one nice cleanup thing that we can do here
instead of having to type out struct and then student every time we can actually in c use type def and then at the end here we would just say student underscore t so instead and i'm just going to comment this out here we can just say student underscore t and then mic so that'll save us a little bit of Typing in our programs and you might see both and see here so i'll recompile it and rerun it now let's go ahead and instead of having to print this off here let's just call function where we
pass in our structure so i'm just going to call this print student and it'll take in a student t and again i've declared my structure above my function so i can use this here and i'm Just going to label them s and let's go ahead and take our printf statements i'll pass them in here and we don't want these all to be mic here let's not assume anything there are a lot of mics in the world but let's just make this s s and so on and then we'll just call our function Print student and
i'll pass in mike here so this will make again a copy of mike so copy of these three fields and pass them in here so let's go ahead and compile that and run it and we can see our student again that's being printed off that we've passed in here is mike so that's our structured data now all the things that we've learned before we could also have a pointer here so let's go ahead and create a Pointer so student star here and let's go ahead and just try to compile this and let's go ahead and
see what happens here let me go ahead and change my layout here so i'll recompile and all sorts of errors all sorts of madness here so where do we start or where do things go wrong well First off we need to be passing in the address here so let's go ahead and try to fix that here so i'll pass in the address mic here because remember pointers store addresses and i'll recompile this and fewer errors so that looks better but now the compiler is saying s is a pointer did you mean to use arrow okay
what's arrow that's something new here So let's go ahead and just follow the compiler's recommendation here and see if that works here s arrow s id s with an arrow and the average grade here and wow it compiled and now if i run it it's doing the same thing that i did before that's amazing so we get our values here okay but what is the arrow actually doing well the arrow is doing a combination of Dereferencing and accessing a field so another way to write this is i'm still accessing a field with a dot but
i'm actually dereferencing that specific field so i'll put it in parentheses and let me just separate it out a little bit here so if i recompile this um oops let me um do this uh properly um then we can actually see what's going On here now you're going to notice i i got errors here because we need to do the correct order of operations so i need to dereference first that is follow my pointer to find the actual value the actual student mic somewhere in memory and then access the id so if i compile and
run this time it'll work so just take a moment to observe that this sh With the arrow here is doing this exact same thing here as id i'm dereferencing s to figure out who the actual student is and then dot accesses a specific field and gives me the id so i'll go ahead and just again draw this out so if i have mic and then i do a dot that is selecting Whatever the field is from my structure here so dot age and when this was a pointer i have to first well since i'm pointing
to something right i just have some random pointer here i need to find my student however i point to it dereference it so that i can look inside of the boxes and then access the specific field with a dot here so then i did the star Around mic to again trace around and find the box that i need and then the dot to select the appropriate field whichever field that might be alright so that's the idea of structs to structure our data together and we can use them with pointers as previously done and just like
our other concepts that we've learned where we've used data types we can create and i'll refactor this a little bit here as many students as we want so i can call this my Classroom and i can have 500 students and student xero has an age that we can assign and so on and if we want to we can do this dynamically if we don't know exactly how many students will have so i can make this a pointer and i can set this equal to well the size of however big our student T data structure is
and then if i want say 700 students this time and let me go ahead and make sure this compiles and since i'm using malik i need to include the standard library that's what this air is giving me and our compiler is pretty good about giving us errors here at some point they're just going to write the code for us right [Laughter] so let me go ahead and recompile that and we say oh error for conflicting type here hmm okay uh what exactly happened here uh well i just spelled student wrong so let me make sure
i recompile that and it still says uh error for conflicting type and well i can't name things the same thing so this is our uh dynamic The size classroom here i'll recompile again and well should i run it is it bug free or not and maybe some of you who remember will also have to free our dynamic class before our program ends here so now i can recompile it and rerun it alright so now you understand or have an idea that strucks exist and we can create our own custom data types All right so our
grand finale encoding examples before we wrap up is to show you a new data structure and this is going to be known as a linked list or i'm at least going to give you a partial implementation and the idea is we're going to create some sort of struct here that's a node it's going to have two fields in it one is going to be some data which is going to be an integer And the next field is going to be a pointer to some other node and that will point to the next node in our
linked list which will store another node with an integer and another node t and this will continue on to another node somewhere in memory here int node t Until we reach a value in c known as null and this is exactly similar to the null terminating character that we saw with strings earlier except this time we have structures and we're just going to use this null type to indicate that our pointer points to nothing that's how we use null in c for our pointers so let's go ahead and create the structure so i'm going to
use typedef again this is a struct node And then node t is how we'll declare now of course you can use struct node but node t is a little bit shorter the data that's carried in each node is an integer and i'm just going to call it the data and within our node we have a node t i'm going to actually write this as node and it's a pointer and usually how we label this is just the next thing that we point to all right so let's go ahead and just create some nodes here node
t 1 and node t 2 and node t 3. now we need to access each of the fields here so one's data is going to equal one two's data is going to equal two and three's data is gonna equal three so i'm just populating these nodes so far here so i have one here i have two and i have three And now i need to link them together so that's setting the next field of each of these nodes and i'll do these sort of consecutively so ones next node well what exactly is it going to
point to well it's going to equal and it needs to store an address here because this is a pointer so the address of 2 And likewise two's next node needs to store the address of three and three's next node that it needs to point to is well there's nothing so null here so it doesn't point to anything so this is our linked data structure and it's really our third sort of data structure we've learned about right we've learned about arrays That are fixed in size we've learned about dynamic arrays and now linked data structures and
i'm just showing you a list but we could have all sorts of connections with our nodes to make them graphs or they could have cycles or multiple chains and multiple directions or self edges and all sorts of interesting things that you'll learn about in your c journey which is pretty cool and well ultimately If we can represent something as a graph or in this structure we can work with it in computer science which is super super exciting so let's get to the point here and just try to print out this actual linked list so how
would i do this well i might need some sort of while loop here and maybe i can just have a node sort of As a temporary iterator here and what it's going to do here is point to the first node in our chain so it needs the address here it's going to say wow whatever our iterator node is and while the next field dot next or arrow next is not equal to null well then we have more information to print off So let's just go ahead and print off whatever our iter rater is which points
to 1 here and i'll walk through this again slowly and let's just go ahead and say data percent d and we need to access the data of our iterator whatever it points to and then iter equals itter next Okay that's a little bit weird let's go ahead and try to compile it though see if we made any mistakes no mistakes that's good and if i run it i see data 1 data 2 no data 3 though it looks like it was off by 1 here so we just want to look at when our iterator is
not equal to null let me try that again rerun it and now i see all of our data one two and three in this list all right let's go ahead and walk Through this portion here looping through a linked data structure and the idea here is that i just created some temporary node here enter and i pointed it to the start of our chain here again just pointing to this box here and again it's just a pointer here to other things that are notes and well it needs to store the address so again the address
of wherever this Node lives here and then i said okay this iterator it's not null it points to something right this actual box here so if it points to this and if i dereference it which is what the arrow is doing remember arrow means dereference so look inside these boxes and then if i choose the field with data that gives me this inside data here and print off one and so forth And then this line is a little bit weird iter equals iter next but i'm basically saying well now set this iterator here to look
at the thing that i'm pointing to and inside of this box grab whatever this node is and point me to this box next and then i repeat the steps here where i access the data inside and then i access whatever the next node is and then sort Of follow this chain forward um here and then i do this one more time until i eventually reach null which is this condition iter not equal to null so eventually we will point to null and that's when we know to terminate here all right and that's a little teaser
of the types of data structures that you can build and see and that's all you Really need arrays where you can decide if you want your memory to be contiguous or this more flexible link structure where you can add things to your chain or in between your chain or in multiple directions if you want some sort of graph structure so folks i hope you enjoyed this crash course of c programming in a day now there's a few topics that we haven't touched on things like unions function pointers bit shifting and a lot of the Standard
c library things like different string functions handling arguments and all these other different things that you can do in c remember c is a language that's been used to program games operating systems any type of application so there's a lot built in and there's a lot more to learn that said that'll leave you with plenty of fun later on to learn from so go ahead and review this video if you did watch the whole video comment below And i'm going to give you a big thumbs up for doing that and i know there's definitely some
of you who went through and did all the exercises and played around with the code and that's awesome so i hope you learned a lot from this video i hope you're going to look at some of the topics that i mentioned things like function pointers arrays building your own data structures bit shifting unions and so much more after completing this and after you've Mastered some of this material so folks i hope you enjoyed this make sure you like make sure you subscribe and we're going to see in the next video and comment below if there's
something else you want to see done and see take care