C: For Beginners - Insert Obligatory First Program Here
(Page 2 of 4 )
Writers that create books and articles for programmers believe that programmers are trained monkeys, obsessed with one goal: to say "Hello" to the world. They always give you these dumbassed introduction programs to print out "Hello World" to the screen. We're not going to do that though.
(Note: Interestingly enough, the tradition of using "Hello World" as a programming introduction was started by Kernighan in his 1972 book Tutorial Introduction to the Language B and was inherited from an internal memo by the same author from Programming In C: A Tutorial).
#include <stdio.h>
int main (void)
{
printf("Why does every programmer look like this...n");
printf(" ~~n");
printf("c00cn");
printf(" (0)n");
printf(" Vn");
return 0;
}
Before I explain the above code, let me show you the result:
Why does every programmer look like this...
~~
c00c
(0)
V
You have no idea how long that took me to make. He looks a little bit like Dilbert, right? Anyway, here is how the program works:
The first part of our code tells the compiler to include a file named stdio.h. This file is made up of descriptions of input/output functions and is used when writing output and accepting input from a user.
The next part is the int main(void). This is required in every C program. The int part tells the compiler that an integer will be returned to whatever made the function itself run when it is finished. The main is a function, and the void in the parentheses states that there will be no parameters in main. As a beginner, it's okay if you don't understand any of that. We will cover it more in depth in future articles. For now, just know that every C program you create must have int main(void) in it.
The printf is a function (it is defined in the stdio.h file, and that is why we had to include it) that prints text to the monitor. The words in between the parentheses and the double quotes will be printed to the monitor. And we use the semi-colon to end the line.
Also, you will note the n. This is known as an escape and it tells the program to add a new line at the end of the sentence (or wherever you place it). If we write the following code without the escape...
#include <stdio.h>
int main (void)
{
printf("Why does every programmer look like this...");
printf("ooga booga");
return 0;
}
...then our result would be:
Why does every programmer look like this...ooga booga
However, if we had included the n escape, like so:
#include <stdio.h>
int main (void)
{
printf("Why does every programmer look like this...n");
printf("ooga booga");
return 0;
}
The result would be:
Why does every programmer look like this...
ooga booga
There are other escape methods, such as the t, which creates a tab, but we will go over those later.
Lastly, the line return 0 literally returns the value 0 to the operating system. This tells the operating system that the program ran with no errors. If the value was anything but a zero, it would have indicated an error or a warning.
So there you have it, your first C program.
Next: Working with Numbers >>
More BrainDump Articles
More By James Payne