14
Aug

Learn Programming in C – Lesson 1 – Compiling your first program


Now that we have a compiler and we’re all
setup, we can start writing programs. So, what are programs made of? First, you need to start with the source code.
The source code is human readable code, and it can get sent to a compiler, which takes
our code and converts it into machine language. Machine language is able to run on the computer.
Therefore, we can actually get a program out of it. So lets take a look at our first program.
This is our first program. While many of the symbols and terms may seem confusing, what
this program does is quite simple. All this program does, is it writes “Hello World” onto
the screen. So, why does this program do this? The first thing the compiler does is it looks
for something called the “main function”. We’ll talk more about functions later, but
all you need to know is this is the main function. And, that anything between these braces ({ }) are
part of the main function. The main function is always the first thing that is run. And
so, the first line of code is executed in your program is the first line of the main
function. The first line of our program, and the main function, is the “printf” statement.
This printf statement tells the computer to write out whatever is inside the quotes onto
the screen. So, this will write out “Hello World!” onto the screen. Now, you may have
noticed there is a “n” after hello world. What this does, is this tells the computer
to basically press enter after it wrote “Hello World!” on the screen. You’ll see later on
how this really applies and it will make more sense. Finally, one it’s done that, it moves onto
the next line. The line is the “return 0” line. All this does is it tells the program
to leave the main function. And, when you leave the main function the program ends. You may have also noticed at the top there
is this other line – this include statement. Somethings are built into the C language like
the “return” line. However, some things are not built right into the language. The printf
statement isn’t built into the language itself. But, it’s common for programmers to want to
print out things to the screen. People have written code to let us write things to the
screen. They store this code into libraries. This gives us access to the printf function. Lets try actually writing this program and
running it. A source file is just a plain text file with a filename that ends in “.c”.
“.c” lets the computer know that inside there is C code. You’ll need a text editor to write
code. It can be any text editor. There are always simpler and fancier text editors you
can use to write code. Some add features such as colour coding and auto-filling code. gEdit,
the text editor we will be using for these videos will give us some colour coding so
our code is easier to read. So, lets open up gEdit. If you just search for gEdit, you’ll
show the Text Editor in Ubuntu. If you are using gEdit like me from Edit ->Preferences.
Under the editor tab, check “Insert spaces instead of tabs” because tabs can react differently
in different systems. Also enable “Automatic Indentation” so when you move between lines
the indentation will stay the same and decrease the tab width to either 4 or 3. Now we’re all setup to write code. I’m going
to save right away so it will let the text editor know we’re writing C code and it can
adjust the colour coding accordingly. I’m going to create a new folder for all of
the projects we do for these videos. And another holding the projects for this lesson. Inside
of here, I am going to name my file. You can name your file any descriptive name you want.
I would recommend not using spaces as it’s easier to compile. Remember to include the
“.c” at the end of the file. So, i’m going to call my program “hello.c”. Now that we’re
saved, let’s start by writing our main function. I’m going to move a few lines down and the
main function is written as “int main” with two parentheses. Then we have our open brace,
and I’m going to add our close brace. Next i’m going to move into the middle and hit
“Tab” because it is much nicer if you tab between braces; it makes your code a lot more
legible and easier to follow. I’m going to add our return line which tells the program
to leave the main function once it hits that line. This right here is the most basic program
you can write. It’s a full valid program, yet it does nothing. We don’t want to do nothing
so we’re going to keep going. We want to print out “Hello World” onto the screen. We do this
using the “printf” function. You start by writing printf – create two parentheses and
write “Hello World” followed by a backslash ‘n’ new line character. At the end of every
C statement you must end your lines with a semicolon. Usually if you are starting to
get errors, the semicolon is one of the first things to look for. Every statement must end
in a semi colon. Now, you may think we’re done except you would get an error if tried
to compile it. C, right now, does not know what the printf function is. So, we have to
tell it what it is. To do this, we have to include our standard input/output library
by adding hash (#) include, then between two angled brackets we will write “stdio.h”. Unlike
a normal C statement an include statement does not have a semicolon. You’ll quickly
pick up what needs a semicolon and what doesn’t. Now if we save our file we are done our source
code. This a complete program that prints out hello world to the screen. To actually
run it, we need to compile this. All programs we write are going to be run and compiled
using the terminal. The terminal can be searched for, and you’ll find it. This is the terminal. The terminal works by
entering commands and pressing enter to execute them. The terminal starts inside of our home
folder. So if we enter “ls” followed by enter. This lists the contents of our home directory.
As you can see, the programs folder is there and we want to move into it. We want to change
our directory into the programs folder. So we can enter “cd” (change directory) and write
the name of “programs” after it. After hitting enter we have moved into this directory. If
we list again, you’ll see that “lesson1” is there. We’ll move into that directory as well.
If we “ls” one more time, you’ll see “hello.c” as well as a backup file that you can ignore.
“hello.c” is our source code. To compile our source code we enter the command
of our compiler which is “gcc”. After writing “gcc” write the name of the file you want
to enter, which is “hello.c”. Press enter and if it doesn’t write anything there was
no issues in your program. If we list the directory again, you’ll see a new file; “a.out”
has revealed itself. “a.out” is our new program. You can run our programs using period forward-slash
(./) and then the name of the program. After pressing enter, you’ll see our program “Hello
World”. This is our first program. Next video, we’ll be talking about output
as well as handing compiler errors.

Tags: , , , , , ,

21 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *