Hello DNA!

Released 11/9-2002

Written by VerticalE

 Required reading: -none-

For my first article here at DNA, I'm going to start with the easiet application there is; the famous "Hello World". For those that haven't heard about these applications before, all they do is simply to write "Hello World" on the screen. They are so famous because just about every beginner book has them. Anyway, gone be the history lesson. Here is the source of a basic "Hello World" application in C
 
#include <stdio.h>

main()
{
    printf("Hello DNA!");
}

Scary, isnt it? Lets take it apart, piece by piece. #include <stdio.h> includes a file called stdio.h (a so-called header file) which have information about a certain set of functions. In this case, we want to use the printf(); function, and the information for how it works is stored in stdio.h. The compiler needs to know how it works, hence we include it.

Every application have a main(). It is where everything "starts". Just like you start reading a book on the first page, the computer that runs the application you make, starts by executing everything in main(). "In main?" I hear you ask. Well, see those { and } things? They are called curly brackets. They enclose something. Just like { this } is enclosed in brackets, printf("Hello DNA!"); is enclosed in brackets too, and in this case the brackets belong to main().

main() { this belongs to main }

main() { this
belongs
to main
}

main() only holds one thing in this example, and that is a function. A function can take either input, output, both or none. Printf means practically "print this to the screen". The parantheses next to the function is where it get its input. The input in our application is "Hello DNA". printf("Hello DNA!") means "print "Hello DNA" to the screen". As with every sentence, we have to end it. In C, we use the semicolon ; to end a function. Lets walk through the source once again:

#include <stdio.h> this includes the stdio header-file

main()
this is where our application start
{
    printf("Hello DNA!");
prints "Hello DNA" to the screen
}

Now comes the tricky part. Remember that I told you that every function can take input, give output, do both or none? Well, as you can see, the main() have parantheses as well, and yes, it is a function. The thing is, main is a special function. Most Operation Systems expects main to give something back, hence have an output. The output of main is the value that your application gives to the OS after it finishes. In our case, we just want to return 0. We do this by writing return 0;

Things arent quite that easy though. We also have to tell the OS what type of value it can expect to get in our return statement. For now, just remember that integers are numbers. When we want to use a number, we use an integer. 1 is an integer, 6345 is an integer etc. We dont write the full name "integer", but we just say "int". Main is sending the decimal 0 back after executing, and 0 is an integer. To update our source:

#include <stdio.h>

int main()
expect main to return an integer value (decimal number)
{
    printf("Hello DNA!");
    return 0;
return the decimal/integer 0
}

Wasnt that hard, was it? And it helps with those comments next to the code, doesnt it? Too bad the compiler doesnt tell the difference between comments and the real code. Or wait... it does! How? Well, you have to signal the compiler that something is a comment. There are two ways to do so. /* means "everything in this document, from now on, is to be considered a comment. */ means the opposite, hence /* this is a comment that ends here -> */. They also work over multiple lines, of course, as you'll see in an example later. Last, you have the // symbol(s). Everything on the right side is a comment, for the rest of the line. Here is the full source, with comments:

/* Hello DNA
    Written by VerticalE   */

#include <stdio.h>      //includes the stdio header

int main()
{

    printf("Hello DNA!"); //print the line to screen
    return 0;                     //return the integer value 0
}

As with all articles like this, reading them over once more is always a good thing :)