C Programming Tutorial-Chapter 1

============================
Things Covered In Chapter 1
Who this tutorial is for
An introduction to C
What you will need for this tutorial
Your first C program (hello.c)
Analysis of the hello program
============================

Who this tutorial is for.

This tutorial assumes the following:
1. That you own a computer or have access to one.
2. That you know the basics of using your computer, ie, you know how to make a folder(directory),
how to create, copy and delete files.
3. That you have access to a text editor(edit, notepad, pico, vi, emacs anything) and you know how
to use it.
4. That you have heard a lot about C programming and that you want to learn how to tell your
computer what to do.

Introduction
OK, so you're a hacker. You may be the best there is. You may know Perl, Shell Scripting, Batch File Programming, the lot. But unless you know C, your knowledge is incomplete. Why? Because most serious exploits, nearly all hacking tools (including Linux/UNIX), even MS-Windows are written in C or its successor, C++. C was created in the ancient days of computing at Bell Labs by two people called Brian Kernighan and Dennis Ritchie. It was born through a need for a portable yet flexible and powerful language. In those days most code was written in assembly language. But that meant that it wasn't possible for programmers to port code to other platforms. A C program can be taken and compiled on any system under the sun that has a C compiler and it will work (almost) exactly the same anywhere. This comes at a sacrifice of speed however, but today's programs are usually way too complex to be written in assembly language and C is arguably the next fastest alternative. So what are you waiting for, read on!

What you will need for this tutorial
1. A computer (duh).
2. A C compiler (the program that converts your C program into an executable file).
Decent C compilers can be found at the following locations:
Compiler Location OS
Borland C++ 5.5 http://borland.com Windows
(signup required)

Turbo C/C++ http://community.borland.com DOS/Windows
(Various Versions) (signup required)

Microsoft Visual C++ http://msdn.microsoft.com/ Windows

GNU C++ http://www.gnu.org/ software/software.html UNIX/Linux
Bloodshed C++ http://www.bloodshed.net
Windows
Your first C program
Now that you have your C compiler set up, start up your text editor and type in this program exactly as you see it here.

/*hello.c
Classical first C program*/

#include <stdio.h>

void main()
{
printf("Hello World!\n");
}

That's it! Save the file as "hello.c".Now all you have to do is compile and run the program.
Compile
For Bloodshed C++/Turbo C++/Microsoft Visual C++ select compile from the compile menu.
For Borland C++ 5.5 start the msdos prompt, change to the folder where you've saved the program and then type
bcc32 hello.c
For GNU C++ type
gcc -o hello.exe hello.c

If you see any errors go back and check to see if the program is exactly like this (you might have forgotten the semicolon at the end of the printf thing). Then save and compile again.

Run
For Bloodshed C++/Turbo C++/Microsoft Visual C++ select run from the run menu.
For Borland C++ 5.5 start the msdos prompt, change to the folder where you've saved the program(if you haven't already) and then type
hello
For GNU C++ type
./hello

You have just successfully written your first C program! Easy huh?


Analysis of the hello program

Now let us analyze the hello.c program line by line.
Line 1,2:
/*hello.c
Classical first C program*/
This is called a comment. The compiler completely ignores everything between the /* and the */. Comments can be any number of lines long and are used to make your program understandable to yourself.

Line 3:
#include <stdio.h>
This tells the compiler to include the contents of the file "stdio.h" in the program. stdio stands for standard input and output and this is the file which defines the printf funtion used later in the program. Without the #include statement(try it) the compiler would give you an error because it doesn't know what printf is.

Line 4:
void main()
This is something that will appear in EVERY C or C++ program you ever write. This is called the main function. When the program runs, the computer first looks at the stuff at the beginning (anything beginning with #) and then jumps to the main function. Whatever you write here is what the computer will execute first.

Line 5,7:
{
....
}
The braces tell the compiler that whatever is contained within them is part of the main function (in this case) or more generally that the stuff enclosed in braces is a unit.

Line 6:
printf("Hello World!\n");
Prints Hello World! on the screen. The printf function prints whatever is enclosed in quotes on the screen. The \n thing tells printf to go to the next line.

So that's it for today's lesson. Please feel free to comment on this post. Constructive criticism will be appreciated. Also I'd like a few suggestions for the next chapter. You could post any doubts related to the tutorial here.