Hey everyone, it's Caleb from Caleb the Video Maker 2. In this video, we are going to write our first computer program. Ooh, magic! Not really, it's just programming, so... One tip is if you are in the shell here, and you just want to get rid of all this crap, there is a keyword you can use, and that keyword or command is clear. Voila!
Now, we are going to create a new file, and we are going to call it hello.c. Now this.c here is how we say this is a C file. Kind of like how when you have a text file you put.txt or you'll see.txt somewhere.
Exactly the same way. So let's press enter. So when you open this and you start typing, you know, like, oh, it's not doing, and then it finally starts working. Well, the reason it's kind of a pain in the butt like that is because Vim has different modes. And one of those modes is insert mode.
As you can see, we are currently in insert mode. So to get out of insert mode, you press the escape key. To get back into insert mode, you press I.
So whenever you want to type, all you have to do is first press I and then type. So let's just type some stuff in here. I meant to say Caleb, but I wrote stuff. To kind of walk around like this, you can just use your arrow keys while you are in insert mode.
In some upcoming videos, I'll teach you some more tricks and some fancy ways to kind of move your cursor around. That'll be coming up in just a couple videos. Okay, let's delete everything.
Now, since this is your first program, you might just kind of have to copy and put exactly what I put, but I will try to explain it the best I can. In the next video and the video after that, I'm going to be explaining this stuff in more detail. So first, we need to go into insert mode, and you want to type include. And then you put stdio.h.
So the very first symbol you have to put is the pound symbol. And then you put include. Then you put a less than sign, standardio.h, greater than sign.
Then enter to go down two lines. And you can put int main, parentheses, next line, curly brace, end curly brace. So this is where the bulk of our code is going to go.
Like I said, you'll understand all this formatting within a couple videos. But for now, just get into the habit of typing it just like that. Inside of here, what you want to do is say print F, and then you use parentheses and put a semicolon.
That is how you do a statement in C. This is basically saying to the computer, hey, I want you to do something, specifically print something. These parentheses, we put some data inside of that. So hover over it and put two double quotes.
Back up a space, and now what we are going to put is... Hello world! To indent you can use the tab key and then what you need to say is return 0 semicolon. So if you are completely new to using the terminal you'll notice that the cursor is actually a huge box so sometimes it's a little less clear as to where you are typing. So if you are like here for example and you start typing it's going to put the characters right before the R.
So if you need to visualize a thin line, it would be the left side of the box that is blinking. So that is where our cursor is. One other thing I wanted to add was in this printf statement at the very end inside of the double quotes, I wanted to put a backslash n and you will understand what that does pretty soon here. So we've written our program. Now I want to compile it and run it.
So to get out of insert mode again you press escape and then we need to save it and escape out of the entire program. So to save it you can use colon W and then to quit you can put a Q. So you just type all that at once. You just put colon W Q and press enter and it brings you back to the terminal. To compile this we use GCC which is the compiler we downloaded in the previous video.
Hello.c and press enter. And you can see that it didn't do anything. That's actually a good thing. When you get no response, that means the compilation was a success.
If you did something wrong, you're going to get some errors. Now, when you compile a file, it creates a new file that we can run. To run that file, what we do is we type period forward slash a dot out. It's kind of weird, I know, but... trust me you get so used to it you can type this stuff without even looking then we can press enter And you can see that it outputs hello world.
Isn't that cool? We got to tell the computer something to do and it did it. Now let's go back, look at our code and try to explain a couple more things. So to go back into our code, we open vim again. The first thing is this main here.
Well, this is called a function and we'll explain that in a ton of detail in the next video. But this gets ran when we execute the program that is compiled. So when we put period forward slash a dot out, this function gets ran.
And inside of this function, we are saying, hey, we want to print this thing right here. This backslash n is called a new line character. And it says, hey, after you're done printing this, go down to the next line.
The return zero is just saying, hey, we're done with the program and everything went well. So we can end it right here. So let's go back to the terminal and I'll show you the output one more time.
You can see up here it says hello world and then it goes down to the next line. That's because we did that backslash n. Now if you're not so lucky you'll have some problems with your code and the first time compiling it's not going to work. But don't freak out, don't worry, just always try again.
And just remember if at first you don't succeed don't try skydiving. To illustrate compilation errors let me go change some of my code. Let's just go in here and you know just blah. Write it, quit.
And then we need to compile again. So to compile we do gcc hello.c. Oh there you can see we broke junk. So this is an example of an error and I'm sure you will see. Thousands if not millions of more of these.
It's very helpful to practice reading these because they actually might be able to help you figure out what's wrong with your code. But for now, that's all I got to say in this video. If you've enjoyed it, please click like and subscribe.
In the next video, we are going to explain in much more detail all of the pieces of this code we have right here. So that's going to be really good if you're a beginner or you're just starting out. So be sure to go check that out.
Thanks guys, and I'll see you then.