Hello and welcome back.
In this video, I'm going to walk you through on how to use the command line interface, the terminal
on a mac operating system to compile and run your C programs.
So the first thing you need to do inside your Mac OS is you need to bring up a terminal window.
And you can do this in a couple of different ways.
You can go to the finder, bring up the finder and go to applications and go down to utilities and select
terminal right there.
Or if you have it on your desktop already in your window right here, you can just select the terminal
window right down in the bottom there.
Once you select terminal, this is your command line interface.
Essentially, this is where you can type in commands and run and build your C programs.
And it also there's a lot of other Linux Unix commands, Mac OS commands, the macOS operating system,
read the hood is Linux like, but it's not exactly Linux.
And so what you'll see when you bring up the terminal is you'll see the name of your computer typically,
and then also maybe a path and a prompt.
And the prompt is where you can actually type in your commands.
I'm not going to go through an exhaustive tutorial on all the commands in the Mac terminal, but the
basic commands that you should understand are CD, which is change directory.
It allows you to navigate to different folders.
You just type in CD and then the name.
You can also use the mkdir command to create new directories and you can use the LS command to list
the contents of a directory.
So by default, depending on where you brought up the terminal, your path may be in right at the top
level.
So here I'm in my Jason's desktop directory.
If you want to create a directory to store all of your C programs, you can just use the mkdir command
and then create a directory right at the top level here.
So maybe I'll just create a directory called projects.
And once I created that projects directory I can navigate to that directory by typing in CD change directory
and then projects and you can hit the tab button to complete names.
And if you hit enter there and you issue the LS command, you'll see your inside your projects directory.
And so at this point you can actually bring up a simple text editor, start writing some C code and
then come back to the terminal and issue commands to compile and run that code.
Now on the Mac OS, I'm not a mac OS expert.
I typically don't use a mac OS too much, but there is a simple text editor that you can use if you
go to the applications.
This might not be the best one.
There are many text centers you can probably download, but one that comes with the Mac OS is if you
go under applications, you'll see one named text edit and so you can create your C programs in this
simple editor by double clicking text edit And it is just a simple editor, right?
There's not much going on here.
It just allows you to write some of your C code.
And so once you're inside text, edit By default, they're going to save files in the rich text format.
You want to change that because you only want to change file save files in the C extension so you can
go up to the text, edit and you can go up to preferences and under the new document tab you can just
say the format, you can say plain text because you don't want any extra stuff saved with this.
And again, there are many editors available on Mac OS, You know, you can use something much easier
than probably Textedit, but this is the basic one that comes with the operating system.
So now I just want to go up and I want to say under text that I want to say file.
I want to say save, and I want to navigate to wherever I created my projects directory.
Usually in the middle you can go to the username and you'll see everything listed there, and then you
can go to projects.
Then you want to go up to the save as text field and you just want to say, let's name this Hello world
dot C and you want to make sure that it says plain text encoding.
You may have to restart Textedit if you just change the preferences like I showed you so that this comes
up.
But you just want to make sure that you say Unicode UTF eight and then you can hit save and it should
have saved.
Hello world.
See, you can navigate back to your terminal and if you issue the LS command, you'll see that there's
a file named Hello World C now.
So now we've saved our file in the right location.
Now we can actually start writing some code and saving it in order to compile it.
So the nice thing about Ides is they'll bring up some templated code for you.
You don't type everything in.
Now I'm going to write some C code and this code is going to look unfamiliar to you.
It's not important at this point.
It's just again, we're trying to set up our environment so that we can run and compile a C program
from the command line interface in the Mac OS.
So I'm going to type in some C code pound include let's make this this text a little bit bigger so you
can see it.
So we can go up to text, edit and we can go to preferences and we can go to the plain text font.
Let's just make it something we can see.
Wow.
Heck, we'll make it 18 swipe Include.
Standard io.h and then we'll just say int main and some things may be automatically capitalized for
you.
Again, might not be the best text editor we can come back and correct these.
We add the curly braces and then we can just say printf.
Hello world backslash n for line feed and then end it with a semicolon.
Then we got to make sure that INT is lowercase and then we can go ahead up and we can say file save.
Again, this is just a simple C editor for source code text.
Edit may be not the best one, but this is one I'm familiar with.
So now we go back to the terminal and if we shoot the LS command, we see Hello world dot C.
Let's make this a little bigger.
And we can now actually see the content of that text file by just typing in something like more.
And then the name of the file and you can see that what's inside there, standard int main and so forth.
So we're able to save our C source code to a text file.
Now we want to compile it.
If you remember from previous classes when we installed the C compiler, the C compiler is named GCC.
If you type which GCC, you can see where the path is.
If you type in GCC dash dash version, you can see the GCC version, but the command to actually compile
is just GCC.
And so if we clear this out and we say GCC and we're currently in our project's directory and we type
in the name of the C source file, which is Hello world, and we can actually complete that using a
TAB completion and we hit enter, you'll see that it just went back to the command prompt and that usually
means there's no errors.
But after issuing the GCC command, that should generate a binary program, an executable.
So if we type in LHS, we can see there's something named a dot out.
That's our program that was created by default, the default GCC command.
We can run our program by doing dot slash forward and say a dot out and that will run the Hello World
program.
And you see his output.
Hello world.
Now the GCC command can get much more complicated.
We can change the name of the executable.
We can do all sorts of things.
I'm not going to discuss all the compiler command line options in this lecture that's going to be in
the advanced C class.
We're just going to go over the basics.
Now, if you type in GCC, dash dash help, this can give you some additional information on some of
the compiler commands.
The really important compiler commands, the ones that you'll probably use a little bit more are the
Dash C and the dash O, which you'd have to actually maybe maximize your window to see them because
it looks like they're higher up.
But GCC Dash cal are really the main ones.
If you type in GCC dash, see that will compile a single source file.
I don't recommend it because then you have to actually link and so forth.
A really good one is, as you noticed, a dot out is by default the name of the program.
That's not a really good name.
So you can say GCC and then the name of your source file, Hello world dot C and then dash o dash o
will change the name of the program from a dot out and you can name it whatever you want.
Let's name ours my program.
And so if we compile and we have no errors now what you see generated is my program and it's a little
more meaningful name.
You can then do a dot slash forward on my program and that will run it the program.
Now if you have errors in your code.
So for example, let's go back to Textedit and let's say we have an error.
Let's say we forgot a semicolon or we we change the name of Main to misspelled or the INT was a capital
I or standard I o doesn't wasn't saved correctly.
Say we save that.
Now when you compile with the basic GCC command GCC hello world.
See what you see is we have an error and so we would have to go back, fix that error in the text editor
and then rebuild it and resave it.
Right.
Because we're missing a semicolon here.
So if we go back, we add the semicolon, we save it and we compile it again.
We don't have that error and we can actually run it and we have no errors and then we can actually run
our a dot out program because we didn't name it there.
We can hit the up arrow key to go to previous commands and we'll execute our program.
So this was just some basics on how you compile and edit using a simple text editor, a C program using
the terminal.
Again, it can get more complicated.
These are just the basics.
If you don't like the overhead of an IDE, this is always a good option.
Thank you.