Hi, my name is Alex and this is the first
in a series of videos on becoming a better
computer programmer. For this particular video
we're going to be focusing on the C programming
language, which is commonly used at universities
as a teaching tool and has much in common
with most languages used in commercial development.
At this point I am assuming zero knowledge
of programming and C in general, so obviously
feel free to move to a more advanced video
as your needs require.
By way of introduction, let me say I've been
programming as a hobbyist and a professional
for the best part of 13 years now, having
intially started with Basic before teaching
myself C. I now work for the market leader
in financial analytics software, still using
the C family of languages on a day-to-day
basis.
Whenever you encounter a new programming language
you're invariably shown what's called a 'Hello
World' program, which is generally the simplest
meaningful program that can be written in
a language. On screen now is the 'Hello World'
program in C which we're going to pick apart
over the next few minutes. You can take this
program and compile it in any C or C++ compiler,
such as Microsoft Visual Studio on Windows,
which I'm using here.
The first thing to note about C is it's a
procedural programming language. This means
the program is arranged as functions while
control and execution flows from one function
to another. Your program is read and executed
line-by-line by the computer. You may be wondering
how the computer knows where to start this
execution, and the answer is that when you
compile your program it looks for a function
named main, as this one is here. It takes
arguments, signified by the words in brackets,
which are inputs from the command line, and
returns an integer - signified by int. Don't
worry if you don't understand these terms
yet. Note that you can have only one main
function in a program or you won't be able
to compile it.
Each function, including the main function,
must be included in what's called a block,
signified by these curly braces. The code
between these outer braces constitutes what's
called the scope of the function. This has
implications later on when you declare variables
which only exist within the scope of a function,
or a subsection of a function. Again, don't
worry about this too much now, and instead
remember that you can identify the scope of
a block of code with these curly braces.
Functions also end in a return statement.
If you're not returning anything specific
- that is, the function returns type void
- you can skip the return statement altogether,
but it will just be implied by the compiler.
When learning C, you should use the return
statement every time just to keep track of
what you're doing in your program. In this
case, our function returns a 0; returning
a zero from a main function of a program signifies
to the operating system that the program has
terminated without errors, whereas any other
number would be returned as an error code.
These codes enable you to have multiple programs
working together, reading and processing each
others' output codes. This is relatively common
practice in industry.
The last part of this program, and the most
important part, is the printf() statement.
Printf is another function which is defined
by the C language. It displays a message to
the standard output device. Thus if you run
this program in a command line, it will print
to that. There's a couple of key things to
note about the way printf is used.
Firstly, we have to tell the compiler where
to find Printf. This is done by the include
statement at the top of the program; stdio
(standard Input/output) is part of the standard
C library and contains the necessary information
about this function.
Secondly, the section included in the brackets
after printf is what's known as an argument.
Functions can take any number of arguments,
or no arguments like our main function, signified
by the word void. For the printf function,
the argument is, as you've probably guessed,
what you want printed to the screen. Make
sure when you pass a string of characters
like this to use double quotation marks, not
single, for reasons we'll cover in the next
video. The backslash-n is what's known as
a special 'escape character' and tells the
printf function we want a new line after this
point.
As a quick note, this system("PAUSE") statement
simply tells the computer to wait for the
user to press a key before continuing in the
program. I've inserted it here so that when
we run our program, the output window will
stay open until we hit a key, allowing us
to see what our program is doing.
Finally, notice that the end of this line,
and the end of the return statement, have
a semicolon. Forgetting this is a key mistake
you'll find happens a lot when learning C,
and it causes problems because the compiler
won't know where your lines end. If you find
a program isn't compiling, always double check
you put semicolons at the end of all your
lines, notwithstanding a few exceptions we'll
cover later.
And that's all there is to it. We can now
compile and run this program, and see the
output here. For now, try creating this program
yourself, and try putting different message
in the program and see how the output changes.
Of course, feel free to post any questions
afterwards. Thank you.
