Click to See Complete Forum and Search --> : C Programming Tutorial Chapter 1


cgkanchi
December 27th, 2001, 07:18 AM
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.

UberC0der
December 27th, 2001, 11:56 AM
Great Job,

Isn't it amazing that some of the worlds most gifted coders start with `hello world'.

cgkanchi
December 28th, 2001, 05:06 AM
Well, K&R put that into us didn't they? :) Also, check out C for Dummies, the first program there is a masterpiece.
It says, printf("Goodbye, cruel world!\n");

hot_ice
January 13th, 2002, 03:17 PM
Great stuff, thanks cgkanchi, keep these tutorials coming.
I successfully done this one...now onto chapter 2...

Greg

Mystic Ravenous
January 16th, 2002, 02:39 AM
Great!! As you know i'm writing the C++ tutorials, kinda been a long time though, but been busy learning it. I have ot learn it for linux to.. hmmm..

I love the format you written it in... :)


cheers,
MR

canada
January 22nd, 2002, 11:39 PM
Great tutorial! But I wonder if anyone can help me. I got my hands on C++ exploits, that include the following libraries: <NetDB.h><NetInet\In.h><SYS\Socket.h><Unistd.h>, it seems to me that they are only available on Unix systems, but can the be also found on Windows C/C++ compilers, if they can then which onces?
Perhaps there can be some equivalents to them?

Thank u.

cgkanchi
January 23rd, 2002, 03:26 PM
I think, though I'm not sure, that <winsock.h> should be all that you have to include for network programming in C/C++. Sorry if I'm wrong though, I just suck at network programming (mainly coz I've never tried it).

intruder
January 25th, 2002, 11:15 AM
good job done kanchi...greate post...

u have given a very good start for newbies...

well if anybody wants more information on the language just click here (http://www.cprogramming.com)

hope this will help..

intruder...

s0nIc
January 25th, 2002, 01:56 PM
oohh that was fun.. im really startin to like dis.. lolz
cant wait for the next part.. :cool:

kajull
February 22nd, 2002, 05:25 AM
thanx for the tute..............bring chapter 2
at least I can understand what u are saying

##prisoner##
February 22nd, 2002, 05:52 AM
Thanx a lot!! It's a good introduction to C. I'm looking forward to Chapter 2.

cgkanchi
February 22nd, 2002, 06:03 AM
There's already a chapter 2. Those interested can find it at http://www.antionline.com/showthread.php?s=&threadid=140166
Cheers,
cgkanchi

Lord Optix
February 14th, 2003, 10:01 PM
I've tried the above program in bloodshed dev-c++. I did everything to spec and tried to run it. The onlything that happens is my dos prompt screen flashes once then dissapears. Is there a solution for this problem ???

YoungBuck
February 15th, 2003, 12:41 AM
Thanks bro

jxrry59
February 15th, 2003, 02:31 AM
I might be wrong but if you use visual studio You use cin>> and cout<< statements instead of the printf. Also, I think you use <iostream> instead of <stdio.h> I am learning c++ with visual studio and it is difficult as I need to unlearn a few things.
ANybody agree or disagree with me?? Is this just a visual studio thing????

cgkanchi
February 15th, 2003, 02:21 PM
You're right that is the way to do IO if you're doing C++. The printf() / scanf() method is the correct way to do that in pure C. So, it's not a Visual Studio thing, just a C++ thing.
Cheers,
cgkanchi