Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (2)

Powered by Vanilla. Made with Bootstrap.
C for newcomers, Part 1
  • chroniccommand
    Posts: 1,389
    Things I will be going over in this portion of the guide:
    1. About C
    2. Installing C
    3. Your first C program
    4. Errors
    ---------------------------------

    About C
    C is a programming language developed by Dennis Ritchie of Bell Telephone Lab for Unix, in 1972. C has influenced many languages today such as C++. It is the "offspring" of the languages B and BCPL(Basic Combined Programming Language). It can either be considered a Mid-level programming language or low-level. Programming languages have different levels to them. High level languages are generally easier to code, and easier to understand. Usually it would be easy to read English. In Visual Basic, which is considered a high level programming language, to code to exit a program is
    me.close
    In C it is usually
    return(0);
    Notice how in Visual Basic.net it is easier to understand that the program is closing, and in C, if you didn't have much knowledge you would probably have not clue what it does. The lowest level programming language is considered machine language, which is numbers and codes the microprocessor understands and then executes. What the C compiler does, in all basic explanation, "converts" the C code to assembly language code, which the computer understands and executes. Something you need to understand is a computer is actually stupid. Many believe that a computer is a very smart machine. It is in all its entirety stupid. The programmer / user tells the computer what to do. The computer is just fast and obeys the commands. If something is messed up its not the computers fault, its your fault. You screwed up the instructions and what it needs to do, not the computer.
    C programming cycle
    The C programming cycle is an idea widely understood by the C guru's and coders. Here it is(Taken from the internet.. Don't say its leached bitch)
    1. Come up with an idea for a program.
    2. Use an editor to write the source code.
    3. Compile the source code and link the program by using the C compiler.
    4. Weep bitterly over errors (optional).
    5. Run the program and test it.
    6. Pull out hair over bugs (optional).
    7. Start over (required).

    1 is usually the hardest, unfortunately.

    Installing C
    Installing C is pretty easy. If your on Winblows, all you need to do is download a C IDE / compiler. On GNU/Linux, I prefer to use gedit in plain text, then compile it with GCC. Your Linux distro should come with GCC already. All we need to do is create a new .C file, write the code and save it. Then you can compile with GCC. Just open up a terminal, change to the directory of the .C file, and use this:
    gcc program.c -o program

    In case you haven't realized, -o means output. This will take the program.c file and make it an executable. Now just type "./program" without the quotes of course, and it should execute the program you just compiled! We'll get more into the actual code soon. Just wait it out, read, and hopefully you'll learn something =P
    Your first C program
    Lets start off with a famous beginner program, Hello world. This is perhaps the simplest you can get, and the easiest.

    #include <stdio.h>
    //This is a comment
    int main()
    {
    printf(\"Hello world!\n\");
    return(0);
    }

    Now I'll dissect the code for you, so you can understand it.
    Line1: This line includes the stdio.h header file in your program. STDIO stands for Standard Input and Output Library. This library contains basic functions, such as printf, puts, scanf and more. Don't worry about those, you'll learn more in a bit

    Line2: This is a simple comment. Comments are crucial for good code. The compiler skips the comments and goes over them. I'll put more on comments in my part 2 guide. But basically, comments can look like //comment here
    or
    /*
    comments
    blah
    blah
    */
    The first type just comments out one line of code. The second one comments out from the /* until the */, where the comment function ends.

    Line3: int main() is the main function of a C program. It is the main backbone of a C program, and every C program needs one. Without the main() function in the program, you cant have the program.

    Line4: The "{" character starts the block of code for function main. I'll speak more about those in part 2(or 3, probably 2). They can come right after the int main() function like so: int main() {
    But usually it's easier to read after the main().

    Line5: Time to learn about the printf function. Printf is a simple function provided by stdio.h and prints to the screen. I'll eventually talk about the "puts()" function, but for now it's the printf function. The f in printf means formatted, because you can include more formatting in it such as variables and strings etc. The printf is followed by a set of parenthesis, which is also accompanied by quotation marks. As you can see, ("Hello world\n") starts with parenthesis, and quotes and ends with them. Without the quotes you would get an error in your code when compiling.

    Line6: return(0); is a function used to tell the program that it's all done. This is basically telling it "Okay, done here".

    Line7: Of course, you must close the curly braces to end the block of code you started on line 4.

    \n
    You may have noticed, or you may have not, the \n character in our little hello world programs printf string. This little character creates a new line. Basically it will hit enter after printing that "Hello world" string. There are other function like this, such as \t which will make a new tab in.

    semicolon
    You may have noticed that some of the code ends with a semicolon and some do not. The semicolon is what is used to tell the compiler that the line of code is finished for that, and to continue to the next one.

    Errors
    Errors, they happen to the best coders and there's no way to avoid them. Humans make mistakes all the time. They can be something as simple as misspelling "printf" to "prnitf". The C program will go crazy looking for that "prnitf" function in the header file, but it's just not there. if you get errors like that, just look through the code and fix the errors. Now look at this code, Err.c

    #include <stdio.h>
    int main()
    {
    printf(\"Herro thar\n\")
    return(0);
    }

    Notice where the error is? Try compiling this and see what happens. You will get an error like so:
    err.c: In function `main’:
    err.c:5: parse error before “return”

    See, it points out the line where the error is.. or does it? Check line five which is the return function. It's all fine right? Well the C compiler is pointing out that line 4 is actually where the error is. You get this because in line 4, which is the printf function, you never added the semicolon. This is where you facepalm, and add the semicolon. Compile it again and you should get no errors and it should work perfectly. Remember, errors happen all the type, they are perfectly normal. Don't shoot yourself over one tiny error. Look over the code and find out what happened.

    This is part 1, which taught the basics. I will work on part 2 soon, which will include some more functions, syntax, usage and more.
    --Chroniccommand
  • Xin
    Posts: 3,251
    Great Paper, i need your Msn/irc anyway i can contact you as we are looking for staff with good knowledge !
    Xin
  • zcmiko
    Posts: 15
    Nice post.
    Imma study these asap.
    I love this :D
    Thanks!
  • chroniccommand
    Posts: 1,389
    said:


    Great Paper, i need your Msn/irc anyway i can contact you as we are looking for staff with good knowledge !



    You may contact me on IRC. Sometimes I don't get to get on much as I'm either studying or reading books on exploitation, hacking, coding etc.
  • Xin
    Posts: 3,251
    Okay fair enough , let me know when you get the irc set up so i can contact ya better!
    Xin
  • very good paper thanks :)
  • Thanks for teaching. I know about C++ little . But I'm a beginner in C .
    Can C++ give me a hand in C ?