|
An Introduction To C - Lesson 1:
Lessons:
[ 1 ]
[ 2 ]
[ 3 ]
[ 4 ]
[ 5 ]
[ 6 ]
[ 7 ]
[ 8 ]
[ 9 ]
Lesson Requirements
A C compiler, GCC is well worth a look and available from gcc.gnu.org
Lesson Summary
The best way to learn to Program is to start doing it, so here we go!
Hello, Quake World!
In a somewhat less than radical break from the norm we will create our first program "Hello, Quake World!". This will be a simple command-line (eg DOS, Unix) program which prints "Hello, Quake World!"
at the prompt.
Create a hello.c file and enter this code:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello, Quake World!\n");
}
Now compile and run your program. Assuming you have no errors, you will receive this output:
prompt:\hello.exe
Hello, Quake World!
prompt:\
If you do have errors when compiling your code, go over it again and look for errors.
The Break-Down
Let's have a good look at what our first program does.
#include <stdio.h>
The '#' (hash symbol) defines the
line as an Instruction for the PreProcessor. The instruction is to include the
file "stdio.h" in our source file. Essentially, the PreProcessor
replaces this #include <stdio.h>
line with the contents of the file "stdio.h". This is basically a
merge, like you get in a Word Processor. So now you are probably wondering what
this "stdio.h" file is. Well that's not important right now.
int main(int argc, char *argv[])
This is a Function declaration. Functions allow us to group together peices
of code and refer to them in a simpler and more structured way. Instead of
having thousands of lines of code all together, we group them seperately into
modules that we can then call individually.
From the Introduction
to Programming Tutorial Lesson 1, you will remember our Program to make a
cup of coffee:
Making Coffee:
a. Boil some water
b. Get an empty mug
c. Place a spoonful of coffee in the mug
d. When the water has boiled, fill the mug with the water
e. If we want milk, add milk to taste
f. If we want sugar, add sugar to taste
Let's say we want to make 12 cups of coffee. We could repeat the instructions
of the above recipe (or Program) 12 times. Or, if we grouped the instructions
into a Function, we could call this Function 12 times from the one Program --
the program to "Make 12 cups of coffee".
Functions will be discussed in more detail in a further Lesson. For now, a
basic Function looks like:
return_type function_name (arguments)
{
...
function_body
...
}
So let's get back to our code:
int main(int argc, char *argv[])
This declares a Function called "main()" which returns an integer
value (a number). The Function takes two arguements, which we wont worry about
just yet. Typically, all console (DOS, Unix) programs require a main() Function
declared.
{
printf("Hello, Quake World!\n");
}
So what does this bit of code do? The first thing you might notice is the { }
brackets. These brackets group pieces of code together, and in this case they
are used to group the code to the main() Function.
printf is another Function to
print (display) characters to the screen. It takes the "Hello,
Quake World!\n" parameter as the characters to display. The
"Hello, Quake World!" part should be quite straight-forward as that is
what is printed. The "\n"
however is a special character -- the New Line character. It forces a line
break.
Phew! That should be enough for your first Program! Have a good play about
with it to change the text it displays. As an exercise, why don't you try to get
your Program to display:
Welcome to my first program.
This is line number 2.
And this is line number 3.
A few solutions to this exercise will be provided in the next Lesson.
|