# C Programming Tutorial For Beginners

Print this out and create this variable up here I can do all of that inside of this for loop now I can basically.

Code I can paste it down into here and we essentially have the same thing so right now this block.

Block of code are 100% equivalent they’re doing exactly the same thing so let’s go ahead and get rid of all this code and let’s test out our shiny new for loop so I’m just gonna run my program and you’ll see over here we’re getting exactly the same thing that we.

Got before we’re printing out one two three four five so it’s the same exact program doing the same exact thing and that’s why for loops are great because we can take that little structure like where we have an indexing variable and we can use it.

With something like this so I want to show you another situation where these four loops can come in handy and we can use them to loop through all the elements inside of an array so actually up here I have this array that I created it’s called lucky numbers and I’m just gonna grab this and bring it down here so we have this lucky numbers array it has 4 8 15 16 23 42 and what I could actually do is I could loop through all of the elements inside of this array from this.

For loop so I’m gonna show you guys how we can do that now let’s first off see how many elements we have we have.

1 2 3 4 5 6 so we have 6 elements in this an array so what I’m gonna do is I’m actually gonna say I is equal to 0 and you’ll see why we need to do this in a second but basically array indexes start at 0 so this first element in the array is at index position zero and just for a little refresher.

If I wanted to access one of these elements from inside the array I could say like lucky numbers was zero and this is going to give me.

If I said lucky numbers two then I’ll get access to this element this 15 so this is basically how we can access an element inside the array so I’m gonna set I equal.

To zero and I’m gonna say I want to loop while I is less than six and six was how how many elements we had in the array so I want to keep looping as long as we’re less than six and I’m gonna say I plus plus now down.

Here I’m gonna do this same exact thing but instead of printing.

Out I I’m gonna print out lucky numbers I so I’m gonna print out the array element at index position I and lucky numbers.

Basically the first time we go through this loop we’re gonna be printing out lucky numbers zero because I is going to be equal to zero the second time we go through the.

Loop we’re gonna be printing out lucky numbers one because I is gonna be equal to one etc and we’re gonna keep doing that until we get all the way up to five which is gonna be the last element which is this 42 so let’s go ahead and do that.

So I need to put this back to I and let’s run.

This program so you’ll see over here we’re basically doing exactly what I said so the first time through the loop were printing out four we’re printing out that first element the second time through the loop are printing out eight which is the second element 15 16 23 and 40 – so we’re printing out all of those elements in turn as we go through this for loop and there’s a lot of situations where for loops.

Are gonna come in handy but.

This is a very very very very common situation where we want to loop through all the elements in an array and either print.

Do something to them whatever so that’s the basics of working with for.

Loops and I do just want to say like anything that you do with a for loop you could do with a while loop you know I basically showed you guys how we transform that while loop into a for loop the thing with for loops though is.

That they’re very convenient so it makes it really easy to do something like this without having to go through all the trouble of creating a while loop in this tutorial I’m gonna talk.

To you guys about two topics in C the first topic we’re to talk about our two-dimensional arrays a two-dimensional array is basically a situation where.

We have an array where all of the elements in the array are actually arrays themselves the second thing I.

Want to talk to you guys about is nested loops so we’re gonna look at how we can use a looping structure where we have a loop inside of loops it’s gonna be pretty cool and actually these.

Two topics can go together really well and I’m gonna show you guys how we can use nested loops and two-dimensional arrays in order to make an awesome program so let’s get started the first thing I want to show you guys is two-dimensional arrays so down.

Here we can create a 2d array and actually.

The concepts that I’m showing you in this video can apply to multi-dimensional arrays so not only two dimensions but three four or five really as many dimensions as your heart desires so in order to create a two-dimensional array I’m just gonna make an array of numbers.

So I’m just gonna say int and we’ll give this a name so why don’t we just say nums and normally when we create.

An array after we’d say the name of the array we’d make an open and close square bracket but when we create a two-dimensional.

Array we’re gonna make two open and closed square brackets just like that these two open and closed square brackets will basically represent like the width and the height of our array so you guys will see what I mean in a second but basically we’re gonna have like elements in the array and then each of those elements is gonna be an array.

And we’ll have elements inside of.

It so these two squares will allow us to like manipulate all that stuff so what I’m gonna do is I’m gonna set this equal to an open and.

Closed curly bracket now this is normally how we would create an array so normally I would just say like 1.

2 3 4 whatever I could put all my numbers in here but with a two-dimensional array all of the elements are gonna be arrays so instead of just saying like 1 2 3 we’re actually gonna create arrays inside.

Of here so for example I’ll put an array right here and this text editor is so annoying with making these so for example the first element in this array I could just make an array so I’m gonna say.

This is like 1 so you can see here I have two elements inside of this array which is itself the first element of the nums array so let’s make another one we’ll come down here and we’ll make another one and this keeps trying to format differently so.

Now we’re gonna say three four so this thing right here this whole array that’s the first element in the nums array right and that array has two elements inside of it same thing with this one this is the second element in the numbers array and it has two elements inside of it so it’s basically an array within an array let’s make one more and so this is going to be five six so now we have three array elements right one two three and each of them has two elements inside of them so whenever we.

Create a two dimensional array like this we always have to specify the number of elements and then the number of elements inside each array so in our case we’re going to have one two three elements in the array and each array has two elements inside.

Of it so it’s gonna look like that so we would say three and then two and that’s basically.

How we created so now let’s talk about accessing these elements so I’m gonna create a little print statement here and we’re gonna print out some of this stuff so I’m gonna say % D and I’m gonna show you guys how we can access individual elements so.

Basically I’m gonna say nums and let’s say that I wanted to access this top-left element this one the first thing I want to do is specify the index where the value that I want to access is stored so like this would be index position 0 this would.

Be index position 1 this would be index position 2 so we’re gonna.

Say 0 and then I want to specify the index position of the individual element inside of 0 so I could say like this is element 0 this is element 1 so why don’t we access element 0 so I print out nums 0 0 and.

This is gonna print out that numbers coming out it’s gonna print out that one for us you can see over here we get that one so let’s try a different one let’s try to grab this for right here so this is.

Going to be an index position one and then one so this would be at 1 1 and now we should.

Get that for let’s see yeah cool so we get the 4 all right.

So that’s basically how we can access elements inside of these arrays and also just want to point out that if I didn’t want to give this an initial value.

I could just like put a semicolon here and I could just like manually define each index location so I could say like 0 0 is equal to 7 or something like I.

Don’t have to give it a value like right up front although in our case let’s just do that because it’s.

A lot easier all right so we have our numbers array and we figured out how we can print out the elements so now I want.

To talk to you guys about another concept which is called a nested for loop and you guys will see in a second why I’m teaching this alongside 2d arrays but a nested for loop is a.

Situation where we have a for loop and inside of that for loop we have another loop so I’m gonna show you guys this really quick let’s say we create a for loop I’m gonna create two variables over here in I and.

Know if I showed you guys this in the course yet but if I want to just like declare two variables I can just say I comma J and that will declare both the variables I’m not giving them actual values yet we’re gonna do that inside these four loops so I.

Want to show you guys how we can use a nested.

For loop in order to print out.

All of the elements inside of this.

Two-dimensional array so I’m gonna say for I is equal to zero.

And we’re gonna keep looping as long as I is.

Less than three and the reason I’m saying three here is because that’s how many elements are inside of this nums array so I’m gonna keep looping as long as I is less than three and then I’m gonna say I plus plus now inside.

These curly brackets I want to create another loop so every time we go through this one iteration of this top loop we’re gonna fully execute through another loop so I’m gonna say for J is equal to 0 J is less than 2 and the reason I’m saying 2 here is.

Because that’s how many elements are inside each array inside of the nums array and you guys will see in a second why this is gonna work and then I’m gonna say J plus plus so now I’m gonna make some more open.

And close curly brackets and down here I’m gonna actually be able to print out all of the elements inside of this array so I’m gonna go ahead and do that I’m just gonna say printf and we’re gonna be printing out an integer because these are all integers and we’re gonna be printing out nums and I’m gonna print out nums I and J so whatever the value of I is and whatever the value of J is that’s the index position inside.

Of nums that we’re gonna print.

Out and now just so this is formatted a little bit better why don’t we put a comma right there and then I’m also gonna put another printf over here that’s just gonna print out a new line and you’ll notice that this printf is outside of this inner for loop so I’m gonna run this program we’re gonna see what it does you guys will see exactly what’s happening and then I’ll sort of walk you through a little.

Bit more what is going on so let’s run this program and you’ll see over here when we ran the program we’re basically getting this entire 2d array.

Printed out so I’m printed out 1 2 3 4 5 6 so let’s talk about why that happens over here I’m saying for I is equal to 0 I is less than 3 I plus plus so I’m.

Going through this particular four loop three times I’m gonna execute all the code inside of this four loop right here three times that’s as many times as there are elements inside of our 2d array now every single time I go through this top loop every single time I go through it I’m going to.

Execute this loop in its entirety so I will loop through this loop all the way through I’ll go through all of its iterations and this loop says J is equal to 0 J is less than 2 and.

2 over here is how many elements are inside of each one of these raise so this element in the noms array has one two elements inside of it this element in the numbers array has one two elements inside of it right that’s where this two is coming from then I’m incrementing J and.

I’m gonna print out so I’m saying print F and I’m printing out nums at index position I and index position J so the first time we go through this for loop I is going to be equal to zero and remember that first time we’re actually going to be going through this loop two times so we’re going to be printing out nums 0 0 and nums 0 1 and then we’re coming.

Down here and printing a new line the next time we go through this I loop we’re gonna come down here and print nums 1 0 and nums 1 1 finally the third and final time we’re gonna print out nums to 0 and nums to 1 that’s basically how this is working so two-dimensional arrays.

And nested for loops are a match made in heaven and there’s a lot of situations where you know besides just looping through 2d arrays that we’re going to use nested loops.

But hopefully this gives you.

A little bit of a introduction into what they are and why they’re useful in this tutorial.

I’m gonna talk to you guys about accessing memory addresses in C now in the C programming language a lot of times we’re gonna want to.

Store different information right and there’s a bunch of different ways that we can store information we can use things like variables we could use arrays we could use structs but the basic point is that whenever we’re using.

C we’re gonna want to keep track of and maintain a bunch of different pieces.

Of information and one of the easiest ways to do that is by.

Creating variables so down here.

You’ll notice that i have a bunch of variables I have this integer called age and it’s just has the value of 30 we have this double GPA 3.4 and we have this character grade and.

It has the value of the a character I have a bunch of different variables in my program and it allows me to keep track of data.

Really easily and I can use these to you know maintain and I can even modify these different informations variables are.

Great but I want to talk to you guys a little bit about how these work in the actual physical memory of our computer so whenever I create a variable for example when I create a variable called age and I give it a value of 30 this value 30 actually gets stored on our computer so all computers have.

Memory so a lot of times you’ll hear people refer to this as RAM it’s called random access memory and basically RAM is the memory that your computer is going to use when it’s running programs so for example.

If I was to run this C program my computer would use Ram it would use that memory in order to store and keep track of all this different information right so when.

I create a variable like int age C is actually gonna store this value 30 at a specific memory location so it’s gonna take that value 30 and it’s gonna store it.

Somewhere in RAM right when I create this double called GPA C’s gonna take this value.4 and it’s gonna store it inside of the physical memory on our computer when I create this grade c is gonna store this character on the physical memory in our computer now here’s the thing.

When I create these variables I give them descriptive names right so I gave this a descriptive name so I.

Know what’s stored inside of it right and when I want to access this value I can just refer to the name right I can just refer to age I could modify and I could print it out I could do something else to it right I can do whatever I want with it and the way that I can access.

This variable in other words the way that I can access the value 30 is just by referring to.

The name of the variable same goes down here if I wanted to access the grade right if I wanted to access this capital a I could just refer to the name.

Of the variable but again here’s the thing all of this information is stored in our physical memory somewhere and so whenever C needs to access that information whenever our computer needs to access this value 30 or this value 3.

To a specific memory address right so when I want to access this variable age I can just type out age and.

Then I can use this variable but when C wants.

To refer to this value it’s not actually using age it’s gonna use the memory address where this value is stored inside of our physical memory so all of these values are stored inside of a physical address in our memory so all of these values have an address where they’re stored in memory.

And when C needs to access them it can use that address so I’m gonna show you guys how we can access that address how we can print out the address on to the screen and basically just wanted to give you guys an introduction into what memory addresses were so down here I’m gonna say printf and I’m gonna show you guys how.

I can actually print out the physical memory address where each one of these values is stored on our computer.

So down here if I want to print out a memory address I need to use a % and a P so normally if I was just gonna print out like a number I could say like % D or I can say % C for a character when we want to print out a physical memory address we’re gonna say % P and this actually stands for pointer we’re not gonna talk about pointers in this tutorial.

We’re just gonna talk about memory addresses but just know that you need to say % P and now I’m going to type out the name of the variable whose memory I.

Want to access so I’m gonna access the memory address of age and before the variable I’m just gonna say ampersand so I’m gonna make an ampersand age and I’m gonna say percent P and now we should be able to print out the.

Memory address where the variable age is stored in other words we can print out the.

Memory address where this value 30 is stored so let’s.

Go ahead and run our program and you’ll see over here we’re getting this number.

So I’m getting this number 0 0 6 0 FF 2 0 0 so this would be like a hexadecimal number.

I guess it’s not technically a number it’s.

Like I think it’s hexadecimal but basically this is the physical memory address where the value of 30 is stored in other words it’s the place where C stored the value inside of this age variable I could do the same thing for these other variables so actually why don’t I format this a little bit I’m gonna stay here age and then I’m.

Gonna make a new line and we’re just gonna say.

GPA and again I’m gonna print out another one of these addresses and we’ll do one for.