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 (0)

Powered by Vanilla. Made with Bootstrap.
C for newcomers, Part 2
  • chroniccommand
    Posts: 1,389
    In my last guide, I talked about Installing/compiling, basic syntax, you created your first program and more. In this part, I will show more functions included in stdio.h including scanf, puts, gets etc.

    1. I/O basics
    2. Functions
    3. Comments
    -----------------------------------

    I/O basics
    I/O, Input/Output. What fun is a program when all it does is print shit to the screen? It's more fun when the user can input something. I/O can come in handy when coding. In the following program, we ask for a name and then print a message to the user.

    #include <stdio.h>

    int main()
    {
    char name[20];
    printf(\"What is your name, stranger?: \");
    scanf(\"%s\",name);
    printf(\"Oh, thats a cool name %s\n\",name);
    return(0);
    }

    Now for the dissection
    Line 1: Remember the last part of this C for newcomers guide? I talked about this type stuff. Here is the simple include for the stdio.h library

    Line 3: Declaring the main function, so you can start your main piece of code.

    Line 5: Here, we are declaring a character "name" with 20 bytes. Think of this as creating a space to hold the name, which can be twenty characters

    Line 6: This simple line is asking the user for there name, with the printf function. There are more ways to print something to the screen, but thats for later in this chapter.

    Line 7: Now we use the scanf function, which is taking what the user types into the keyboard, and saving it to 'name', which can be 20 characters as declared before.

    Line 8: Now is the where we print the string saying hello. Remember both %s and \n? %s is that placeholder for string, in case you forgot and \n is the newline character. Now, since we are using printf we can use name in that printed sentence, so name replaces the %s. Name is what the user typed, which was saved to Name(Remember, 20 characters).

    Line 9: Finally, the line to tell the compiler the program is basically done.

    So as you can see, the basics of I/O is asking the user for input, and then printing output. Pretty basic right?

    Functions
    Now to learn about those fun functions. Scanf, puts and gets(For this section). Scanf was used above, and reads info from the keyboard. It is a way of getting input from the user for later use in the program. Scanf can be very useful, and you'll see why eventually. The syntax of scanf is shown above, but I will show it here. Basically, we asked for the name, and got the name with scanf.

    scanf(\"%s\",scan);

    Now for this to work, me must have declared the character "scan" up above to hold the bytes from the keyboard. Then we can output it with printf. So to sum up scanf, it is used in I/O as a way to read input. It's as simple as that. Now for the puts() function included in stdio.h. Puts is like printf, but there is a slight difference. Puts is not formatted, therefor you can only print text on the screen with it. Plus, puts(), always puts a \n after everything even if you font include it in. Puts would be used the same way as printf, but you cant have the identifiers like %s and %i. Use the puts function when you want to simply put some text on the screen. And finally for this section, the gets() function. Gets is like scanf, but a bit simpler. Consider the following string using gets();

    gets(name);

    This would get the input and store it in the character 'name'. No need for ("%s",name). Gets() has its downfalls though, because it allows you to input as many characters you want. This could allow attackers to do something called a "keyboard overflow".

    Comments
    I briefly described comments in my last section. I will now go over comments a bit more. Comments can be used two ways. Like this:

    //Comment here
    printf(\"Hello\"); //Print's hello

    Comments are used to make things easier for the developer. They also point out what happens in the code. You could also comment out a bunch of lines of code like so:

    /*
    Comments
    Go
    Here
    */
    printf(\"hello\");

    As you can see, the lines of comments start with a slash and then an asterisk, and end with an asterisk and slash. Now that you know a bit more about comments, see if you can find the errors in this program and fix them

    #include <stdio>
    //Start the program
    int main()
    {
    char lolz[20];
    puts(\"Hello, world!\");
    /* Uncomment this for input
    scanf(\"%s\",lolz); /* Ask for input */
    */
    printf(\"Goodbye\");
    return(0);
    }

    There are a couple errors in here. For an exercise, try to fix them.

    In the next chapter of this guide, I will be talking about integers, variables, and *UGHHHHHHH* math in C. Yes, math... so fun. Happy C
    --Chroniccomand
  • Xin
    Posts: 3,251
    Nice tutorial! we should code some easily buffer overflow-able apps for the hacking challenge's , shouldnt be too hard.
    Xin
  • chroniccommand
    Posts: 1,389
    said:


    Nice tutorial! we should code some easily buffer overflow-able apps for the hacking challenge's , shouldnt be too hard.



    Sounds like fun :P I've coded some things like that. Have you ever played "SmashTheStack" wargaming?
  • Xin
    Posts: 3,251
    Ive played io.smashthestack , no others! The others seem to be down a lot of the time ill probably do some of the others too
    Xin
  • thanks very good c tutorial