Hello, and welcome back.
In this lecture, I'd like to talk about what goes
in to creating a c program.
So there are four fundamental tasks in the creation of any c program.
And what these tasks are include editing, compiling,
linking, and executing.
We talked about the different steps that you'll employ when you write a
c program in terms of you're going to understand requirements, you're
going to design your code, you're going to write your code,
you're then going to test and debug it and so forth and then maintain it.
What I'm talking about here is what you actually need to do when you start
to write a program. We kind of talked about editing, compiling but linking
and executing are also important.
And what I want to talk about here is the details of this.
So these tasks are going to become second nature to you because
you're going to be doing it every time you write a c program.
You're going to be editing your source code, you're going to be
compiling, you're going to be linking, and then you're going to be running.
You're going to be doing this very, very often.
So the processes of editing, compiling, linking, and executing
are the same for developing programs in any environment
with any compiled language.
This is not unique to c.
If you're writing java - a java program, you're going to write
the source code, you're going to compile the source code.
Linking - you might not be as aware of in java but it's going to do
some kind of linking and we'll talk about what linking is, and then
you're going to run the program.
You're going to be doing all this.
What I'm describing here again is how it's done in c.
So editing is exactly what you would think it is.
It's the process of creating and modifying the actual
code, the c source code.
The c source code is going to be inside of a file.
And it's going to contain all the statements and all the
instructions that you write.
All c source code files end with the .c extension, and
that's how you can identify that it's a source code file.
Before that .c extension is going to be called the base name, you want
to have your file names be meaningful.
If you write a c program that adds two numbers, you may have a file called
add.c, that contains a source code.
And remember that most c programs are going to have
many many different files.
So your files should have good names.
But the way that you can identify c source code is the .c extension.
They all contain the .c extension.
Now when we edit our code for this class, we're going to use an
integrated development environment.
We're not just going to use a simple editor.
An example of a simple editor would be something like wordpad.
We're going to use something that has an editor but also has
many other things that help you write your code more efficiently.
The IDE - and this is referred to as an IDE, integrated development environment.
The one that we're going to use for this class is Code Blocks,
I'm going to cover in detail, but we're going to use code
blocks for the editing task.
But you can use whatever you want.
If you want to use notepad, if you want to use something even more
minuscule, you can Atom, there's visual studio IDEs, but the one
we're going to cover in here is how to edit your code and code blocks.
And this is a nice ide and the reason I chose it we'll talk
about more detail, but it is cross-platform and easy to use.
So editing is the basic process, writing the code.
Compiling.
We know what a compiler does.
It just takes that high-level language code and and converts it
to machine language, and it also detects and finds errors in your code.
After you edit your code, you're going to run the compiler.
So the input to the compiler is going to be all of your source
code, all of your .c files everything that you're editing.
And again, a c program can have many source files.
But the input to the compiler are these source files, compilation
itself converting this code is a two-stage process.
The first stage is called the pre-processing phase.
During which your code may be modified or added to.
And the second stage is the actual compilation, it generates
the assembly language code.
And we'll talk about this in more detail, this pre-processing phase.
But you just need to know now that there's two stages
when you do compilation.
The one that we're going to focus on or the one that's easy to understand
is the generation of the object code.
So the compiler is going to look at each statement and it's
going to make sure that the syntax is correct and that the
semantics of the language is met.
Syntax is to make sure that the structure of the code is right,
that you have semicolons at the end of the statement, that you're
using data types, that you're - you're following the rules of the language.
Semantics is the meaning of the words.
So if you use an if keyword, that's a condition.
Semantics happen in the compilation phase by creating
a symbol table and so forth.
But the compiler is important.
It's going to generate this object code.
It's going to tell you if you have any errors in your syntax.
It can also sometimes recognize whether or not you have dead code
where basically code's not being used.
It does not find logic errors.
It only finds syntax errors.
Again, syntax is the rules and language.
Logic errors are the meaning.
Like if you made a mistake in logic, you may make a
mistake in the functionality.
So your code can run, it can compile, but it may not work as intended, and
this is referred to as a logic error.
The compiler is not going to tell you if you have a logic error.
If you meant to ask the user for a number instead of a character string
or you ask the user for their last name before their first name that's
a logic error. Compiler is not going to tell you those types of errors.
Typical errors reported by the compiler are going to be things like
undeclared variables, expression that doesn't conform to the
syntax, you don't have the right parentheses, you have you know
blocks that don't have ending blocks.
You're going to become very, very familiar with the compiler
errors and what they mean.
Hopefully, after you've learned the c language and you run the
compiler, you're going to see an error and you're going to
be able to fix it immediately.
Say whoops forgot a syntax or whoops forgot a semicolon, oh,
I forgot to provide a right bracket to end that block of code.
It's going to become second nature.
So after you fix all the errors and your compiling
is successful, it's going to translate into assembly language
and generate this object code.
So the actual object code is a .o file.
It's the actual summary language code that gets run.
It has an extension .obj or .o.
.obj typically on windows will be the extension.
.o on a Linux or Macintosh system will be the extension.
You're going to see that file generated after you compile your code.
And that's just the end phase of the compilation, that's
the actual assembly language.
It's going to be binary, it doesn't look like text you're not
going to be able to look at it.
It's just going to have a .o extension, and that's
what the compiler generates.
The way that you run the compiler in c is you actually use a command.
You probably won't need to do this much because the ide, the editor
in the integrated development is going to do it for you by just
clicking a menu option, but it's important to know what's going on.
So when you issue the compiler command, it's typically using a GNU
compiler which is a gcc command.
But at the basic level, you just run a compiler program either cc or gcc.
You run that program and then you specify the input.
Remember, the compiler takes as input the source files.
You specify the input by using a -c flag and then specifying the
source files, one after the other.
So examples here are cc-cmyprog.c or if you're using the GNU compiler,
which is a popular one gcc-c.
And if you omit the c flag your program will automatically be linked
as well which is the next step.
But really it's easy to run that compiler if you're
at the command line interface.
It's even easier if you just use a menu option which we'll
probably be doing in this class.
But again, it takes input, the c source files.
In code blocks, as I mentioned, you're going to be just clicking
a menu option and it will automatically compile a link and
then you can click another menu option it will automatically run.
But again, what's going on under the hood is you're actually issuing
the compiler command with the -c flag and the input source files.
After you compile, you link. Like I said every programming
language has a linking, but a lot of times it's transparent, you
won't even know what's happening.
In c, the linking process is important because it's not always transparent.
But with most modern IDEs today, it is, but I do have to emphasize
that it is important in c.
So after the program has been translated into this.old
code its object code, it then is ready to be linked.
The reason we link is if we're using external libraries we have
to grab all those other libraries and put them into one program.
So linking is just getting all the dependencies in place and
building an actual executable file.
So the .o file is the objects.
You then link all the .os into one executable.
And again, this is usually transparent, usually happens
automatically when you compile.
But again, it may not and you may have to do a separate
command, like a -l or something.
But it takes all those object files and builds one executable.
And the executable is what you run.
So it's going to combine all those.
And then it's also going to combine.
And you may have .os that are additional libraries, that other
programs that you may want to use that somebody else wrote or other libraries
that come with the c language.
You're going to take all those and put them together.
And the linking phase will also detect and report errors.
They're two separate phases.
Linking, again, would usually be like redefinition errors or you're trying
to call a function that doesn't exist.
So if you don't have that .o file to make up the entire program, you're
going to have a linking error.
And you'll need to have all the .o's.
And again, the .o's may not even be yours.
They may come with the same language, they may be part of the standard
library or they may come with another program that somebody else wrote.
But you need all those .o's to form one executable.
So program libraries support and extend the C language by providing
routines to carry out operations that are not part of the language.
Input and output libraries are example, mathematical library,
string manipulation libraries.
This is just the same thing if you're familiar with the java program, it's
the same thing as the java api.
It's the same thing as packages.
It's this concept of I want to write data to a file I don't
want to implement it myself, I'm going to use an existing library
that comes with the language.
It's still written in c, it's just somebody c comes with that.
So there's all sorts of different libraries that we're
going to learn about and use.
This happens part of the linking phase.
So a failure during the linking phase means that once again you have to go
back and edit your source code to make sure that you can find that object
file, so a lot of the failures usually mean that you just need to include
a file or something like that. This will become second nature as well.
Success, if you're done compiling successfully, you're
done linking successfully, that will produce an executable file.
In a windows environment, the executable file will
have a .exe extension.
In Linux and Windows, it will just have by default a.out.
You can change the name of a.out to something more meaningful
by just issuing something along with the compiler command.
But most IDEs in code blocks as well will have a compile and link program
in a single operation and it will produce the executable and then you
can just double click the executable or you can run the executable from
a command or from the menu option.
But no at the low level if you're on Linux, you're going to see an
a.out by default as your executable. On windows you'll see a .exe.
A program of any significant size will consist of several source
code files, I mentioned this earlier.
Each source code file needs the compiler to generate the object
file, that needs to be linked.
The program is much easier to manage by breaking it up into a
number of smaller source files.
You will want to use many source files, and you don't want a source
file that contains 5000 lines of code.
That's not cohesive.
That's not easy to manage.
So you're going to create files that contain source code, and the
source code is going to be specific.
It's going to have a specific functionality.
It makes it much easier to develop and much easier to
debug and is much more cohesive.
So you're going to have many smaller source files.
You don't want large source files with thousands of lines of code.
And all these source files are going to make up the entire program.
They're all going to have their own .o's, and they're all going
to be input to the compiler.
All these concepts that make up the program, essentially
are also considered a project.
You can think of it as that at a higher level.
It's your project, it's your program.
After you've finally done all this and you've linked it,
you've got your executable file, you can then run the program.
In most IDEs, this is just going to be a menu click or you
can double click in windows, you can double click the exe.
And if you're on Linux, you just type in a.out.
Now when a.out you have to have certain permissions so you have to
maybe issue a change mode command, but to run it you can say ./a.out
and that will run the program from the command line Linux.
But again usually you don't have to worry about this. So the execution
stage is where you run your program.
Each of the statements of the program is sequentially executed in turn.
If the program requests any data from the user, the program
will pause and get that input.
You can get the input via the keyboard or it can get the input via
graphic user interface if you have libraries for that in your c code.
Results that are displayed by the program appear in a
window called the console.
And we're going to show you examples of this when
we talk about code blocks.
But when you run your program, typically, you're going to see output.
Not always, sometimes programs will do things that don't produce output.
But usually, they produce output and you see the output right
on the terminal or the console.
The execution stage also generates a wide variety of error conditions.
So you may produce the wrong output which is just a logic
error and you have to go back and edit your source code.
It may sit there and just do nothing because you have logic issues.
And then lastly it may crash your computer which is the worst
case scenario, which means that you did something really bad in
your program and it was so bad that the program just died or
just or it actually caused other programs on your operating system
to crash your entire computer.
So running the actual c application can have cause problems. If
the program does not perform the intended functionality, then you
have to go back and edit the source code you may have to reorganize
or redesign and this is where we talked about the different steps.
This is known as the debugging phase to find and fix your errors.
We're going to have a whole section on debugging because
it is that important.
I mentioned earlier that one of the best ways to learn is
to learn through your errors.
You're going to constantly go back and edit source code and debug.
The entire process of compiling, linking and executing the program is
repeated until you're done, until you have all the results that you need
for the functionality of that code.
So here's, again, another nice illustration I like to provide.
This is taken from a book called Beginning C by Horton.
But this is the process that you're going to have.
You're going to edit your source code.
You're going to create a file with a .c extension and a meaningful name.
You're going to use an editor to do that or an IDE, integrated
development environment, something like Code Blocks.
You're then going to compile it.
The compiler is going to generate an obj file an o file.
If it doesn't generate that file, that means you have errors and
you have to go back and fix them.
After you create the obj files, the object files, you then
have to link them all together to create an executable file.
And you could also have errors in that phase where
they're not linked properly.
You have to go back and edit your source code.
Once you actually have no errors and you have an executable file,
you can then run your program.
And again, after you run your program, you still may have errors
because there could be logic errors.
So then you would go back and edit your source code.
So this is going to become a repetitive process that you're
going to be very familiar with.
Edit, compile, link, execute.
And this again is typically what you do in all languages.
You're going to be doing this over and over and over again until your program
meets all of your requirements.