The Ground Cero Guide To C
By Henrik Aasted Sørensen

Chapter 1: LET'S INCLUDE IT.
Chapter 2: HOW TO MAKE A FUNCTION
Chapter 3: HOW TO USE VARIABLES.
Chapter 4: MAKING YOUR VERY OWN FUNCTIONS
Chapter 5: ...AND NOW FOR SOMETHING COMPLETELY DIFFERENT!
Chapter 6: USING THE PRINTF-FUNCTION.
Chapter 7: ARRAYS.
Chapter 8: STRINGS
Chapter 9: GETTING INPUT FROM THE USER
Chapter 10: GOODBYE
Chapter 11: EPILOQUE - FREQUENTLY ASKED QUESTIONS

INTRODUCTION.

C is generally acknowledged as one of the best programming languages
available. It has a rather long story, and has been used for building a
lot of the "BIG" programs in existence.

With this textfile, I'm trying to give total newcomers to programming a
chance to start out with C. If you find any errors or have any comments
please feel free to mail me at seth@post1.tele.dk. If you feel insulted
by the language please feel free to mail a complaint to pope@church.vc.

Experienced C-programmers might not be able to find much of value
in this textfile, but programmers experienced in other programming
languages (like QBASIC or Pascal) might find this text useful for
upgrading to C.

It is my opinion that a good example can outweight a lot of writing, so
you'll often see that I'm using examples, which I'm then commenting,
rather than handing it to you in all theory.

I've chosen to play an egocentric, godlike being and therefore tried to
pretend what you might think when you read through this document. Don't
feel insulted if I don't hit the kind of questions you have.

The writing of this text was begun on the 22/7 1996, at 1.30 in the morning.

This text is free for you to use, as long as you don't change it.

This text will always be available on the Ground Cero homepage at:
http://home1.inet.tele.dk/seth

Chapter 1: LET'S INCLUDE IT.
Even though C is one of the most used programming-languages it is in
it's rawest form not even able to print a character to the screen!

This may seem a bit strange, considering that most programs display
something on the screen. Therefore C uses external commands to
display characters to the screens. These commands are located in
headerfiles.

"STOOOPPPPP!!! What the F___ is a headerfile?" some of you scream!

A headerfile is a compilation of pre-made, commonly-used commands.
They are probably located in the "INCLUDE"-directory where you
keep your compiler. The most used header-files, when programming
basic C, are STDIO.H (STandarD In Out) and CONIO.H (CONsole In Out).
They contain most (if not all) of the commands you'll need while
using this tutorial.

To include a header file, so that you are able to use the commands
it contains, use the "#include" command. An example of
including CONIO.H is:

#include "C:\QC\INCLUDE\CONIO.H"

With some compilers you can include a headerfile without specifying
the entire path. This works by putting brackets around the
headerfile's name, like this:

#include <CONIO.H>

When the C-compiler compiles your program it has to know where to start.
That's why at least one of your functions (NOOO! Don't scream! I'll
explain functions later.) has to be named main. A function always
has a pair of parantheses after the name, why I'll explain later. To
mark the start and the end of a function we use {}, where { marks
the start, and } marks the end.

If we combine this with our old knowledge about #include we are now able
to set up our first C-program:

#include <STDIO.H>
#include <CONIO.H>

main()
{
}

This program will compile, but if you run it, you probably won't
be too impressed with the result. But, have no fear, soon we'll
make programs that actually does something.

Chapter 2: HOW TO MAKE A FUNCTION

When you look at a function like main(), all you see is the title
and two parantheses.

"What are these for?" you ask (because I feel like answering it now).

With these two parantheses you are able to send parameters to a function.

For example when you want C to print something on the ordinary screen,
you'd write:

printf("Hello");

printf is the name of the function we're calling.

("Hello") is the parameter sent to printf. If there are
more than one parameter, a comma (,) seperates each
parameter.

The semi-colon is used to tell C when you are done
writing the statement.

I'll explain how to make your own function in a later chapter.

Chapter 3: HOW TO USE VARIABLES.

"A program without variables is like a plane without wings:
impossible to control!"
                                                    Unknown

A variable is a very basic part of every programming-language.
It is (almost) impossible to make a program without using variables.
In short it is a block of memory that you can store, change and read
information from/to.

There are a lot of variables in C, and here are the most common:

Integer:
Used to contain integer-values. Type name: int.

Characters:
Takes up 1 byte of memory. Used to contain
integer values in the range from -128 to
127. Mostly used to contain a single
character. Type name: char.

Floating:
This type can contain real numbers. Type name: float.

These three should be able to cover our needs for the moment.

When you need a variable you declare it like this:

int my_integer;

int it the type name of our new variable.
my_integer is the name of our new integer-variable.
The ; is used to terminate the declaration.

Variables MUST always be declared in the beginning of the function:

#include <CONIO.H>
main()
{
  int my_integer;
}

This is impossible:

#include <CONIO.H>
main()
{
  printf("We're on a mission from God!");
  int my_integer;
}

Don't do it! It will probably just cause you trouble when you
have to find out why it won't work.

"How do I use my new variable?"

It's very easy! Let's say we wanted our new integer to contain
the value 5. We would then write something like this:

my_integer = 5;

Easy and logical!

It is also possible to give a value to a variable, while declaring it:

int my_integer = 5;

Well, it's not called a "variable" for nothing. Let's say we wanted
to increase our integer with 4:

my_integer = my_integer + 4;

Easy, but not logical!
[ If you're not too much into math, then I can tell you that a value
is very rarely equal to itself plus 4. That's the reason why the
expression is NOT logical. ]

Of course we can use all the basic operators (+-*/) the same way.

Another way of adding or subtracting the value 1 from a variable,
is this:

my_integer++;
my_integer--;

Another way of doing exactly the same thing is:

my_integer += 4;

This will add 4 to your integer variable.

Chapter #4: MAKING YOUR VERY OWN FUNCTIONS

Sooner or later you'll have to make your own functions when
programming C. It's not good programming custom to cram all
of a program's code into the main-function. Here is how
it all works:

void my_function (void) {}

{} empty brackets, because we haven't started
programming the contents of our function.

void is the data type that the function
returns, when it has finished it's job.
Void means that the function won't return
anything.

my_function is the name of the function.

(void) is the parameter list. Void means
that the function doesn't accept any
parameters.

This is a very basic definition of a function. And not a very
flexible function at all, because we can only call it, and then it
runs like it has always run and will always run, because we can't
send it any orders through the parameter list.

Here is how to define a function that accepts parameters:

void my_function (int param1, float param2) {}

Well... the two first words ("void" and "my_function") should be
clear by now. But the new text in the parantheses demands an
explanation.

When you place a variable between the parantheses, it's actually the
same as declaring it at the beginning of the function, except that
it receives an initial value, which is passed from the calling
function, like this:

my_function(5, 3.14159);

If we called "my_function" with these values the two variables param1 and
param2 would contain 5 and 3.14159. Easy!

We can also call "my_function" with one of the variables in the calling
function:

int amount = 7;
float value = 14.42;
my_function(amount, value);

Logical!

"But how do I make my functions return a value?"

Now for the mysterious word before the function's name:

void my_function(int param1, float param2){}

Void really means that there is no datatype specified. We might
substitute it with any name of one of our datatypes (Chapter #3).
For example:

int my_function (int param1, float param2) {}

Now our function returns an integer, when it has completed execution.
To receive this we use our old method of assigning a value to a
variable:

int amount = 7, return_value;
float value = 14.42;
return_value = my_function(amount, value);

Easy.

Actually we can use a function exactly the same way we used a constant
or a variable in chapter 3:

int amount = 7, return_value;
float value = 14.42;
return_value = 7 + my_function(amount, value) + amount;

you can't put a semi-colon (;) at the end of a function-definition.
Instead you must put brackets ({}), around the code that the function
is supposed to contain.

To decide which variable we want to pass back to the calling
function, we use the command called "return".

Let's say we make a function that multiplies two integers.
That would look something like this:

int multiply(int number_one, int number_two)
{
  return(number_one * number_two);
}

Easy. (I hope ;-) )

Chapter 5: ...AND NOW FOR SOMETHING COMPLETELY DIFFERENT!
Now you want something repeated a lot of times.(!)

Let's say that, for some strange reason, you wanted to
make a program that filled the screen with Xs.

To make that as simple as possible we'd use a command called
"for". It is able to repeat code over and over again. As many
times as you want.

Let's make an example:

#include <STDIO.H>
main()
{
int i;
for (i = 0 ; i < 50 ; i++)
  {
    printf("X");
  }
}

Let's isolate the "for"-statement and go into a little detail:

for (i = 0 ; i < 50 ; i++)

for shows that we're dealing with a
"for"-command.

i = 0 assigns the starting value of i.

i < 50 shows how long we want i to
be increased. We only want it to be
increased until it reaches 49.

i++ is the command that increases
i after each time the commands,
marked by the brackets, have been
executed. We only want to increase i
by 1.

OK...this should be pretty easy. The demonstration code writes X
50 times across the screen. Very handy!

And now for something completely logical!
The "if"-command is a very simple, yet useful command.
You can use it to test variables against each other, variables
against constants and constants against constants (not much fun
in that, though!).

Let's say we've gotten the age of the user of our program.
We've stored it in an integer-variable called "age".
Unfortunately our program contains material, not intended for
persons below the age of 18, so we have to check if the integer
is equal to or above 18:

if (age >= 18)
{
  printf("Confidental material goes here!");
}
else
{
  printf("Sorry, young man! You're too young to think for yourself!");
}

That wasn't too hard, was it? This code asks:
Does the variable age contain a value greater than or equal to 18?
if it does then print our confidential material.
Else the value must be below 18, and then he isn't allowed to look.
Tell him!

This is roughly how it works. You can also test several integers in the
same if. Let's say that you have to be 40 years old or less to get
access. Then we could write

if ( (age >= 18) && (age <= 40) )
{}

REMEMBER: No semi-colon, because we use brackets to mark the containments
of the if-statement!

The double-& is a logical AND. The logical signs are:

&& (AND) If both values are true, we execute the code in the brackets.
|| (OR) If one of the values are true, we execute the code in the brackets. If both are true,
we also execute the code.

To test if a value is equal to another value, we use the comparison
equal-to: = =

if (age == 18)
{}

REMEMBER: Always remember the difference between the assign-equal-to(=)
and the comparison-equal-to (= =). A lot of time can go into
searching for an error like comparing to values with an
assign-equal-to (=).

If you want to test if a value is NOT equal to another value it looks
like this:

if (age != 18)
{}

So... that should be it for the "if's".

Another possibility to use, when you want to repeat something is
called while. It repeats the code in the brackets until the
statement in the parantheses is false.

Example required:

while (age < 18)
{}

This would repeat the code in the brackets until age growed greater
than 18. Rather simple, when you understand if.

Chapter #6: USING THE PRINTF-FUNCTION.[remember to #include STDIO.H]

Many times throughout this text, I've made use of a function called
printf(), without really explaining how it works, except that it prints
text to the screen. Well, it can be used for a lot more. Take a look:

printf("You're %d years old, and your name is %s", age, name);

The first part of it is easy: printf is the name of the function.
The text in the qoutation marks ("") is also easy, but there are four strange
characters: %d and %s.
Actually it isn't too strange: %d refers to the integer "age" just
after the sign ("). It means that the value in "age" will be written on
the screen just after "You're ". The %s means string (in a later chapter!),
and it prints the contents of the string name.

If the user said that he was 21 years old, and his name was Charlie,
the output of the program would look like this:

You're 21 years old, and your name is Charlie

Unfortunately any text written after this command, would be written
right after "Charlie". To make a return in the text, and jump to the
next line you can write "\n". This is called an escape sequence.
It can be put anywhere in the printf-statement. For example:

printf("You're %d years old \n your name is %s", age, name);

would produce the following output: (Again with Charlie's input.)

You're 18 years old
your name is Charlie

EASY!!

The most used escape sequences are:
\n........................ New line
\\........................ Backslash
\"........................ Qoutation mark(")
\a........................ Alarm. Makes the PC-speaker beep.
\t........................ Tabulate.

The in-line reference's to variable's are:

%d........................ integer.
%f........................ floating, real.
%c........................ character.
%s........................ string. (Later!)

There are more, which I didn't include in this tutorial,
because we're not going to use them.

Chapter #7: ARRAYS.

I know that I've promised to tell you about strings, but I just have
to tell you about arrays first.

Let's say that you wanted to make a program that contained the ages
of all of your friends...

Instead of defining the integers that contains the ages like this

int Peter_age = 17, Henrik_age = 5, Martin_age = 21;

we would define them like this:

int ages[3] = {17, 5, 21};

It would be very easy to write these ages to the screen. We would
just use our for-statement:

int i;
for (i = 0 ; i < 3 ; i++)
{
  printf("Age: %d\n", ages[i]);
}

That would produce the following output:

Age: 17
Age: 5
Age: 21

Smart!

You should remember that the array-index ([]) starts with 0 and not 1.

Chapter #8: STRINGS [Remember to include STRING.H]

"What is a string?" Well, basically it is a variable that can contain
a series of letters. Actually it is an array of 'char's. Aren't you glad
that I told you about arrays in the last chapter?

We define a string like this:

char my_string[50];

Well... you know the char. The brackets after the variable-name shows how
many chars we want in our array.
Try to guess how many letters our new string can contain?
Yes, you guessed it! 49!

The last char is reserved for the string terminator. The string terminator
is the character that tells us, or our program, when a string ends.

We can fill text into our new string like this:

char my_string[50] = "This is my new string";

Of course that would leave a great waste of space, since we didn't use
all the 50 characters.

Another way of defining a string is:

char my_string[] = "This is my new string";

This causes the string to be exactly long enough to contain "This is
my new string" + the string terminator.

To print the string we use the printf, like in Chapter 6:

printf("%s\n", my_string);

Output:

This is my new string

When it has been defined, it is a bit harder to change the contents of a
string, than in an integer.
We have to change it char by char...

char my_string[50];
my_string[0] = 'n';
my_string[1] = 'e';
my_string[2] = 'w';
my_string[3] = 0; /* The string terminator */

Fortunately, the authors of most c-compilers, supply us with an easy way
of copying strings. It is called strcpy.
We use it this way:

char old_string[30] = "Copied to new_string", new_string[30];
strcpy(new_string, old_string);

Doing this copies the containments of old_string to new_string.

Chapter #9: GETTING INPUT FROM THE USER

There isn't too much idea in using variables, if they aren't changed
now and then. The best source of changes are the user!

There exists an order called scanf. It's included in the STDIO.H.
It is in many ways similar to printf, but IMO (which you are stuck
with for now ;-) ) is it a bit too hard to control and manipulate.
Therefore I use the command getch, which is a bit harder to use,
but it can be used to make some very efficient functions of your own.
It's getch, which I'll now describe.

getch accepts only one character at a time. This may seem a bit slow,
but it can actually be turned to our advantage. Since we only receive
one character at a time, we can sort and change as they arrive.
But let's keep the spirit of this tutorial, and look at an example:

#include <CONIO.H>
main()
{
  char key = 0;
  while (key != 13)
  key = getch();
}

Well, most of it should be pretty clear... We include CONIO.H, in
order to be able to access the getch-function. Then we define a
char-variable called key. The next two lines makes sure that, as long
as key is not equal to 13, we won't continue the program.

The reason we chose 13 is that it is the ASCII-value for return/enter.
I won't start explaining the ASCII-table now, but I can tell you that
whenever a getch receives a return, it returns 13. Other ASCII-values
worth knowing are:

ESCAPE = 27
SPACE = 32
F1-F10 = 59-68
BACKSPACE = 8
TABULATE = 9

"How can I make the user input an entire string?"

Well, an example is worth a thousand words:

#include <CONIO.H>
#include <STDIO.H>
main()
{
  char string[30];
  int i = 0;
  while (string[i - 1] != 13)
  {
    string[i] = getch();
    i++;
  }
  string[i] = 0;
  printf("You typed: %s", string);
}

Okay... biggest example 'till now...
We start by declaring a string that can contain 29 characters + the
string terminator.
Then we declare an array pointer [Which is just a fancy term for an
integer used to show us where in an array we are].
As long as the user does not press enter (ASCII: 13), we'll continue
to collect his keyboard-inputs.
When he presses enter, we put a string terminator instead of his
return-value, and writes his string to the screen. You should note
that getch doesn't echo to the screen what it receives as input.
That's the reason why the user won't see what he has been writing until
we print it.
A lot of things could be done to improve this function:
- It should react to a backspace.
- It should be cancelled when the user presses escape.
- i mustn't be greater than 29, because our string can't contain it.

...but I you should try and do it yourself. If you need help: mail me.

CHAPTER #10: GOODBYE

Well, that seems to be all for now...
If you think anything is missing from this tutorial, or anything
is unclear, please feel free to mail me. (seth@post1.tele.dk)

If you use this tutorial I'd be really happy to hear from you.

Henrik Aasted Sorensen, august 5th 1996