Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: C Programming Tutorial Chapter 1

  1. #1
    Antionline Herpetologist
    Join Date
    Aug 2001

    C Programming Tutorial Chapter 1

    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.

    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
    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.

    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.
    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.

    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
    For GNU C++ type

    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:
    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.

  2. #2
    Senior Member
    Join Date
    Nov 2001
    Great Job,

    Isn't it amazing that some of the worlds most gifted coders start with `hello world'.
    Know this..., you may not by thyself in pride claim the Mantle of Wizardry; that way lies only Bogosity without End.

    Rather must you Become, and Become, and Become, until Hackers respect thy Power, and other Wizards hail thee as a Brother or Sister in Wisdom, and you wake up and realize that the Mantle hath lain unknown upon thy Shoulders since you knew not when.

  3. #3
    Antionline Herpetologist
    Join Date
    Aug 2001
    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");

  4. #4
    Senior Member
    Join Date
    Dec 2001
    Great stuff, thanks cgkanchi, keep these tutorials coming.
    I successfully done this one...now onto chapter 2...

    \"Do you know what people are most afraid of?
    What they don\'t understand.
    When we don\'t understand, we turn to our assumptions.\"
    -- William Forrester

  5. #5
    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...


  6. #6

    Help with C wanted

    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.

  7. #7
    Antionline Herpetologist
    Join Date
    Aug 2001
    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).

  8. #8
    Senior Member
    Join Date
    Sep 2001
    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

    hope this will help..


  9. #9
    Fastest Thing Alive s0nIc's Avatar
    Join Date
    Sep 2001


    oohh that was fun.. im really startin to like dis.. lolz
    cant wait for the next part..

  10. #10
    Junior Member
    Join Date
    Feb 2002
    thanx for the tute..............bring chapter 2
    at least I can understand what u are saying
    Genius of the mind is not necessarily from the mind of a genius

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts