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
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.
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;
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:
As with all articles like this, reading them over once more is always a good thing :) |