AN INTRODUCTION TO ISO C AND EXTENSIONS.
----------------------------------------
Let me introduce myself. My name is Ian. I live somewhere in Central
Canada.
The purpose of these pages are twofold:
1. To give me some writing experience.
2. To help people that have had some exposure to the language, but
stopped learning because they had trouble understanding some key
points of the language.
I will not introduce every single item about the language such as
where to place braces "{", or what header files are available to you,
but I will try to explain many other beginner concepts. From that
point I will move to intermediate and perhaps advanced topics.
This and the following pages will give you an introduction to the C
programming language, as defined by the International Organization for
Standardization (ISO). If I use something that is machine/system
dependant I will be sure to inform you of it.
On a side note, I recommend that you be in a calm and healthy state of
mind. You will probably find that learning new information with a hangover
is a very difficult process. Speaking of hangovers, I have a web page all
about recovering from last nights party. Click here to see it.
I will begin with a very small program and then explain what everything
does. Let's begin with the much overused "Hello World" program:
/*--------------------------------------------------------------------
Program: HEYWORLD.C
Author: CORVUS
Date: Aug. 15, 2001
Description: This program displays the words "Hello World!" on the
default device (usually the screen).
--------------------------------------------------------------------*/
#include <stdio.h>
int main(void)
{
printf("Hello World!\n"); /* Display text */
return 0; /* Main always returns an int. */
}
So there you have it. A full working program that follows the standard
set out by the ISO.
This program will display the words: Hello World! on the screen.
Let's walk through it:
You may notice that at the top of our program we have a box that has a
description as well as other info inside it. You may also notice that
at the top left hand and bottom right hand of the box are funny looking
symbols. They look like "/*" and "*/". These are comments. You use
"/*" to begin a comment and "*/" to end a comment. Everything in between
these two symbols are ignored by the compiler. This includes the dotted
lines and all the stuff in between.
Every program you write should have some type of comment in it to tell
you (or whoever has to read through your code later on) what it does.
You will also notice that there are more comments embedded inside the
program itself. One on the same line as the printf statement and one
underneath describing the return statement. I encourage you to comment
your code. Even on lines that you think you can easily tell what it
does. Comments are not included in the final executable, so they do
not take up space or slow down your program in any way. Use Comments!
That said, onto the next line:
You will see underneath our comment box the line: "#include <stdio.h>".
This is what we call an include statement. Most if not all programs
have an include statement. Our include statement has the word "stdio.h"
inside greater and less than symbols. There are many different include
files. The file we included contains definitions for many different
input/output routines. The reason we require it in our program above is
because the definition of "printf" is stored in this file.
For now, just know that you will probably be using this header file 99%
of the time. (One exception to this rule is Windows programming, which
I will not be covering).
Let's now go to the next line containing some writing, which happens to
be the main program itself. It begins like so: "int main(void)" WoW.
Before I started programming I rarely had the chance to use the word
"void" before. If you have never done any programming before, this
line may also seem a little strange to you as well. This will require
a small discussion, so let's begin:
We will skip over the word "int" for now and deal with the word "main".
What is main anyway? In short main is where program execution begins.
It is also a function. All C programs MUST have a main function. It
is the heart of any C program. That said, lets discuss the other parts
of our main function.
The main function must always be declared in this way "int main(void)"
unless you are passing command line options to it. If that's the case
the word "void" is removed from the brackets and two variables will be
placed inside. Those will be discussed at a later time. The word "int"
means that the "main" function will return a value. We'll get into
return values shortly. For now, just make sure that your main function
is written "int main(void)" Ok? Good.
This is probably a good time to point out that the C language IS case
sensitive. That means that the words "main", "MaIn", "MAIN" are all
different. Please be careful that you use the right case. This can
be hard to grasp for some people coming from a case insensitive
language, such as Pascal.
On the following line we have a curly bracket "{". This is the
beginning of the "main" function. All code will be written somewhere
AFTER this curly brace. If you come from a Pascal or Ada background,
then the brace is equivilent to the BEGIN statement in those languages.
Underneath the curly brace we have the words:
"printf("Hello World!\n");" First off, printf is a function that
displays text on the screen. Well, that's not entirely correct.
Let's say you don't have a monitor. What then? The above sentence
should really say "printf is a function that places text on the standard
output stream". Streams are discussed heavily in the C++ language, but
rarely mentioned in the C language. There are some differences between
the two languages and the way they use streams, but this borders on
more advanced topics, so to keep things simple I am going to assume you
are going to output to your monitor.
The words you want to display on the screen are stored inside double
quotes. like so: "Hello There!". The strange looking "\n" is called
an escape character. This name can be misleading. The "\n" symbol
means to start a newline. (Which on DOS and Windows OS' just happens
to be a carriage return, followed by a line feed.)
The whole line is followed by a semi-colon. This is used as an end of
statement marker.
On the next line we "return 0". You might recall a few paragraphs up
that the function "main" will return a value. In ALL cases main will
return an int. That is what the "int" in "int main(void)" is. In
general the value 0 is success, while any other value means failure.
You may also use the pre-defined "EXIT_SUCCESS" or "EXIT_FAILURE" in
place of integer values. If you decide to use the two pre-defined
values you must include the header file "stdlib.h".
on the very last line, we have another curly brace "}". This time
facing the opposite way from the first one we encountered. This simply
means that we've reached the end of the function. This is equal to the
end statement in pascal (if that helps).
So there you have it. A fully working program...Fairly useless, but at
least it runs. Now that you have a basic program skeleton, make it
print something else on two lines.
For example, try displaying:
Hello There! My Name
is BoB!.
If this is really easy then great! Do it in your head if you want.
When You can figure that out we'll go to Part 2.