C Lesson 1 (part 1)
Hey guys, this Mac isn't alone, and today, as I promised before, I'm going to be making my first of many programming lessons. So first of all, sorry if it sounds like I have a cold because I actually do this time. But anyway, I must proceed.
So the programming language I'm going to be focusing on today is C, and we're going to have a few more C lessons. But C is a very low-level, hard-to-use language, so I'm going to explain to you why.
First of all, built into every processor is a language called assembler. Assembler is one of its like, very hard to use, but it's small enough that you can program into a processor. So to do something in assembler that would take a hundred lines, in C it might take a thousand lines or even more. Now, C is known as symbolic assembler to some people because it is basically the same thing. You still have to manage your memory or else you'll mess something up. You can still deal with specific locations in memory, and there's no string variables and no classes; it's all instructions that you're giving.
Now luckily, people have written other C functions for you, such as included in <stdio.h>
, etc., that allow you to enjoy C more like it's a regular programming language. The C syntax is kind of easy to get used to; I don’t know, it was for me anyway.
So, um, let's get started. You open up Xcode, you want to go up to File, then New Project. Now you can go under Command Line Utility and Standard Tool. Then when you click Choose, you want to name it. We're going to call it "Hello World." I'm going to save it to my desktop. Now click Save.
So now that, Standard Tool, is the type of C program that Xcode lets you do. So I'm going to be doing a GUI for the C program, and the way we would do that, by the way, if we wanted to do one, would be to use something called Xorg or X11, but that wouldn't work on Tiger. And it’s very hard to do compared to Interface Builder, so we're going to save the GUI stuff for Interface Builder.
So you'll notice there are a few files here: main.c
and then a built executable that has its red right now because we haven't compiled this app yet. So I'm going to show you how to do this.
So first of all, if you click main.c
, the code should appear here. If it doesn't, you can double-click it and it'll appear in a new window. Now they've already laid out the basic structure for a C application. This is where the code goes; this is pretty much mandatory. This is almost certainly mandatory and these are mandatory as well.
So I'll explain what all the stuff does. #include <stdio.h>
isn't really a C function as much as a compiler function; I'll explain that later on in another lesson. But stdio
stands for standard I/O, and we're using someone else's thing that they've already written that's standard, that everyone should have, that has all the great functions that we're going to be using in this lesson.
We might also under this include #include <string.h>
and you have to get used to the format of #include
. Pretty much always looks the same except the stuff inside the less than and greater than.
So these are actually files located somewhere on your computer, and I'm not sure Spotlight will show them, but there are a bunch of files that people have written for you.
So now this int main
is, uh, it's like a void; a void is something that contains code. Now if it's called main
, then it will run when the app is started. That's magic! The name main
is what's going to happen when the app opens. You can create another one called int Joe
, and you don't have to put anything between the parentheses and use these curly braces, but you're going to have to get used to.
You can put code inside of here, but it won't run when the app starts. So we get that. That command means that this main function has to return an integer. return
is a simple instruction, so you return zero. An integer is a number, but it has to be a whole number. It can be negative or positive, but it has to be whole and can't be decimal.
Okay, so right here’s the actual root of the application: printf("Hello World");
So this code right here, all it does is make "Hello World" come out in the console or in the terminal that you’re running in.
Let me show you what I mean. So if you click Build and Go, and I'll say Save, then it will run and quit. Now "Hello World" will now be here, not red anymore, and you can click it, press Command-C and Command-V to paste it onto your desktop.
Now unfortunately, I already have a folder with the same name as "Hello World" on my desktop, so I'll just open up something else and paste it in there. So since this is a terminal app, it will open the terminal and print "Hello World" to the terminal. That's it!
Now we're not going to be doing it that complicated way; there's another way to do it. Then I’ll show you, that's like a terminal emulator almost. So I'm going to show you how to make this code more advanced than just printing "Hello World" to the terminal.
That it’s run again. So printf
puts text in some file; it is obviously like a stream. And a C file pointer means, like, it's either read, write or append. So you can either read, write, or append to any specific file.
There’s a magic file that stdio.h
puts in here called stdout
, standard output. That is a file that's open as append, and we can write to that file. That file is what the terminal is, so when we write to it, it writes that text that we've written to it to the terminal.
So printf
writes to a file, and if we haven't specified a file for printf
to do it, it writes the text to the file called stdout
.
Okay, so now that we've got that cleared up, you might still be a little bit confused. I'll explain it all to you in better detail in another terminal lesson where we actually use files more.
So here's what the application is going to do. I'm just going to ask us for our name, we're going to type our name in, and then it's going to say, "Hello, [your name]."
So first of all, we want to declare a place in RAM that we can put text into. So let's see, it's harder to do this easily. We can declare, we can allocate a certain amount of bytes in RAM.
So say we know their name is not going to be longer than 512 bytes; we're going to allocate 512 bytes. A byte is like a character, a number, a letter, anything into memory. So we're going to do char name[512];
.
So let's get started. When we did int main
, we're declaring something called main
that's going to return an integer. When we say char name
, it's declaring a character, one character in memory that we can do whatever we want with.
But what the heck is this bracket bracket doing here [512]
? That's a good question. The brackets are there; we can get rid of those if you want, but they're there so char name
is one letter. But we want it to be 512 letters, and we don't want to have to make 512 characters the hard way: char name1
, char name2
, and have to read every character into those.
No, that's not cool. There's no way to declare a string; you can only declare characters. So we do char name[512]
, and there’s something called an array in C, and it's a group of one type of variable.
So we'll do char name[512];
. So when you have brackets right after a variable declaration before the semicolon, that's how many of those variables we're going to declare. So this thing name
is no longer one character; it's 512 characters because we can tell by this.
So only in the declaration do we do [512]
then the number of characters that are going to be in that right bracket. Now, C, we want to access the first character in the array of characters called name
.
So it's easy to do this: name[0]
, and 0 is really the first one! It goes up this: 0-511, not 1-512. 0-511, don't ask me why they made it like that, but they did.
So if we wanted to access the second character of the 512 we have access to, we do one. Right now, you're probably wondering why I’m on the screen all of a sudden, but I'm just saying that this video went way longer than I expected.
So if you want to see the rest of the video, check out C Lesson 1 Part 2. Goodbye!