### 3.0 The first program
----------------------------------------------------------------------
Here's the first C program you will write. It is is a short
program. I promise it will be easy. You will need an ASCII editor,
to enter the text. Some good editors help you in writing source
code by automatically indenting the code, highlighting the commands,
giving you direct access to the compiler and so on.
Get a GOOD editor. A bad one is really a pain! I prefer the
very quick JED/XJED editor. It is fast and small, not a second
OS-layer like the EMACS. And not that cryptic like vi.
NOTE: Carolyn is totally right here: IT IS IMPORTANT to learn
to use vi, cause it is the one you will find on any *NIX system.
On the other hand it comes from ancient *NIX (*NIX stands for
all the different kinds of Unix like Linux, Irix, Ultrix, AIX,
BSD, OpenBSD, FreeBSD, NetBSD, etc. etc.) times, its command
structure is very powerful and awful IMHO.
Newbie note: Pico is a great newbie
editor, and is also available in many Unix shells. We won't make
too much fun of you if we catch you using it.
Type this into your favorite editor in your Unix shell account:
#include<stdio.h>
#include<stdlib.h>
main()
{
printf( "Hello, Hackers!\n" );
#if __STDC__
printf( "May I introduce myself: I am a compiler of the
new version! \n" );
printf( "I am an ANSI compiler\n" );
#else
printf( "I am an old K&R compiler!\n" );
#endif
}
Save the text in a file called "HelloHackers.c".
Time to compile the human readable text, our first source
code into machine readable assembly language. Time to work, compiler!
Type at the commandline of your account the following command
sequence:
cc -o HelloHackers HelloHackers.c
This will call the compiler and tell it to take "HelloHackers.c"
and compile ( = translate) it into an executable program called
"HelloHackers".
Now check, whether the program really runs! Type:
HelloHackers
Maybe you will get an error message here, like:
HelloHackers: command not found
This looks like a path error. See the GTMHH, Programmers'
Series, Number 2 for some ideas on path statements, or ask tech
support at your ISP.
To execute the program despite the fact, whether you have
got this error type:
./HelloHackers
Now you will get an output. This will be either
Hello, Hackers!
(May I introduce myself: I am a compiler of the new version!
I am an ANSI compiler.)
or
Hello, Hackers!
(I am an old K&R compiler!)
Nice, isn't it?
### 4.0 C compiler vs. C compiler
----------------------------------------------------------------------
Maybe you are wondering, why the same program can produce
two different outputs without anything changing in the source
code or/and with the command to produce the executable program?!
The reason is: There are two different kinds of compilers
out there.
In the beginning, when C was designed, its fathers, Brian
W. Kerningham and Dennis M. Ritchie, invented a style of C, which
was very open. The compilers made on the base of this definition
of C don't look very much for errors in the source code. They
simply translate the source code despite whether there is something
contradictionary or unlogical in the source code. This
kind of C is called of "the old style" or of "the
K&R style". Compilers which only can compile this
kind of source codes are "K&R" compilers"
or "old compilers".
More important: Because there was no official standardization
of this style, different vendors add some "special features"
to their compilers. Also, some aspects of this languages were
not completely defined by K&R. Slowly, the one programming
language C became "different languages, called C".
The result was that source codes which could be compiled on one
machine without any problems, produced errors on other machine
with a
different compiler.
So when you try to compile some exploit you downloaded from
http://www.rootshell.com and it gives you error messages,
the fault may be that it was written in a C version that your
compiler doesn't like!
Then, K&R decided to write the Second Edition of their
first book about C. This time, the ANSI commitee standardized
this implementation of C. All compilers which want to be "ANSI
compatible" have to fullfill this standardization. These
compilers are called "ANSI compilers", and this kind
of C is called "ANSI-C", the modern form of C. These
compilers will take a closer look to your source code and will
warn you if they will find any suspicious commands.
With a little programming trick, which will be explained later
in this tutorial, the source code can decide what kind of compiler
it needs to use and produces an appropiate output.
### 4.1 If your compiler is a K&R or "old" compiler
----------------------------------------------------------------------
If you want to buy "The C Programming Language"
by K&R get the FIRST edition of this book. It introduces
the first implementation of the C programming language.
As we will see, there is one special so called "function"
-- more about this later -- in each source code. It is called
"main". If your compiler is a K&R compiler, please
use
main()
as the definition of it.
### 4.2 If your compiler is an ANSI-compiler
----------------------------------------------------------------------
If you want to buy "The C Programming Language"
by K&R get the SECOND edition of this book. It introduces
the implementation of the C, which has been standardized be the
ANSI commitee.
As above, this kind of C uses a special function calle "main"
also. But the definition of this function is different from that
above. It is
int main( int argc, char *argv[] )
.
Later we will discover more differences between these two
types of C compilers. But for now, you only have to remember
the kind of main-function, you should use.
Because ANSI-compilers understand the old K&R style also
normally, the above example program compiles without a problem
on them. In the rare case, that you get error messages or wanrings
concerning "main", use
int main( int argc, char *argv[] )
instead of
main()
in the above example.
More how to program in C --->