C Programming Tutorial For Beginners

Program whenever we want to run a program that we write in C we actually have to do two things the first.

Thing we have to do is what’s called building a program or compiling a program and basically what that does is it takes all of this C code it translates.

It into a language that the computer is going to be able to understand and execute so it’s essentially taking this C program code translating it into computer code and.

Then the computer can execute it and run our program so the first thing we have to do is always build our C file the second thing we have to do is run the file that gets built so once we’ve built this program we’ve compiled it down.

Into code the computer can understand we have to run it basically tell the computer to execute that code and in code blocks it’s actually really easy.

To do this so we can come over here and this little gear icon will allow us to build our program and then this play button will allow us to run our program but a lot of times when you’re programming you’re gonna want to build and run your code at the same time right you’re just gonna want to build it and run it just to see what happened you know see how your program did so we have this.

Option over here called build and run and this will both build your program and run it at the same time so generally as you’re going through this course you’re.

Gonna want to be using this option that’s build and run options when I click this it’ll essentially build my program run it and then it’ll show up here on this little window in this little window is basically a console window you can call it so a lot of people would refer to this as the console and it’ll just like output any information that we tell it to so if we write a C program that doesn’t output information that doesn’t print anything to the screen then we wouldn’t actually see this.

Using this printf command that window is gonna pop up so let’s talk about how these programs.

Actually get executed essentially when we write a program we have a set of instructions so when I’m writing a.

C program I’m basically writing out a set of instructions imagine you were writing like a recipe or something right a recipe is essentially just a set of instructions that when executed correctly will result in you know cooking or baking something delicious.

Right and that’s basically what these C programs are it’s a set of instructions that when executed correctly will result in the computer doing something for us and one of the cool things about computers is that we can program them to do.

Anything and C is one of the best languages to.

Do that with so let’s look at this instruction over here it’s called print F and this prints something out onto the screen we will notice over here at the end we have this semicolon and this semicolon is actually really important so whenever we write an instruction.

In C we always want to end it off with a semicolon that tells C that we’re done with that instruction and we want to move on to the next one so I could actually copy this instruction and I could paste it down here and now we’re basically going to be executing this instruction twice so if I was to run and build my program you’ll see that we’re not.

But we’re also printing it out twice so when we run our program the program comes down into this main method and it looks at all the instructions inside of it so it starts with this first instruction it executes it right so it prints out hello world onto the screen and then it also prints out.

A new line so this backslash n is like a newline character and that’ll move the text onto.

A new line when it’s done with this instruction it moves on to the next instruction and it’ll execute that instruction so as we go forward and we learn how to write in C we’re gonna learn more and more complex instructions and we’ll be able to use all those instructions in conjunction with each other to specify some seriously complex programs but for now.

This is really the only instruction that we know which is printf so why don’t we use this printf instruction in order to draw out a little shape onto the screen so I’m going to show you guys how we.

Can draw out like a little triangle onto the screen.

I’m gonna take this printf and I’m actually just gonna get rid of hello world and I’m gonna copy this a couple times so I’m just.

Gonna copy it and we’ll paste it down here maybe four times and what I can do is I can specify instructions to the.

Computer that will tell it to draw out a particular shape so down here I could make like a forward.

Slash and over here we’ll make another one and I’ll make another one and I’ll make another one and you can see here I’ve drawn like a little diagonal line with these.

Slashes now I’m gonna draw vertical bars going all the way down so I’m gonna put one here I’m gonna put one here I’ll.

Put one here and finally why don’t we draw some underscores and then we’ll put one so I’m basically drawing out a little triangle shape and these backslash ends are just printing out a new line so anytime I print this.

It’s gonna move it on to a new line so it’ll print this guy.

Out now on the line down the line below so if I was to save my program and execute it now the computer is going to go through it’s going to execute each one of these instructions in order and we’ll be able to print out the shape onto the screen so let’s do that you.

See over here we get our shape and one thing I really want to stress is that order matters so if I took this instruction and I placed it up here as the first instruction now this is gonna get printed out first so when I run my program you’ll see that we’re printing out this kind of funky looking shape and that’s because we changed up the order of the instructions so the computer changed the order in which it executed those.

Instructions and you’ll notice here again we’re using this semicolon after each line of code so that’s kind of the basics of writing a program that’s essentially all you need to know when we’re writing a program we’re specifying a list of instructions.

That we want the computer to carry out and depending on which instructions we give the computer and which order we give them and how complex.