|
Quick Navigation Bar overview :: introduction :: basic c [ toc | forums ] |
Note: If the document URL does not begin with http://randu.org/tutorials/c/ then you are viewing a copy. Please direct your browser to the correct location for the most recent version. |
Let's give a go at a very simple program that prints out "Hello World" to standard out (usually your monitor). We'll call our little program hello.c.
#include <stdio.h>
main() {
printf("Hello, world!\n");
return 0;
}
What's all this junk just to print out Hello, World? Let's see what's happening:
Seems like trying to figure all this out is just way too confusing. Let's break things up one at at time:
return 0 statement. Seems like we are trying to
give something back, and it is an integer. Maybe if we modified
our main function definition:
int main() Ok, now we are saying that our
main function will be returning an integer! So remember,
you should always explicitly declare the return type on the
function!#include <stdlib.h> to our includes.
Let's change our return statement to
return EXIT_SUCCESS;. Now it makes sense!
int printf(const char *format, ...); printf returns
an int. The man pages say that printf returns the number of
characters printed. Now you wonder, who cares? Why should you care
about this? It is good programming practice to ALWAYS check for
return values. It will not only make your program more readable,
but in the end it will make your programs less error prone. But
in this particular case, we don't really need it. So we cast the
function's return to (void). fprintf, fflush, and exit
are the only functions where you should do this. More on this
later when we get to I/O. For now, let's just void the return
value./* */.
The comment begins with /* and ends with
*/.Let's see our new improved code!
#include <stdio.h>
#include <stdlib.h>
/* Main Function
* Purpose: Controls program, prints Hello, World!
* Input: None
* Output: Returns Exit Status
*/
int main(int argc, char **argv) {
printf("Hello, world!\n");
return EXIT_SUCCESS;
}
Much better! The KEY POINT of this whole introduction is to
show you the fundamental difference between correctness and
understandability. Both sample codes produce the exact same
output in "Hello, world!" However, only the latter example shows
better readability in the code leading to code that is understandable.
All codes will have bugs. If you sacrifice code readability with
reduced (or no) comments and cryptic lines, the burden is shifted and
magnified when your code needs to be maintained.
Document what you can. Complex data types, function calls that may not
be obvious, etc. Good documentation goes a long way!
You will most likely be using the GNU C compiler, gcc in
a Linux or *nix-like environment. Most
environments will have the cc variable set which is much
more suitable when using Makefiles.
A superficial look at compiling would entail: gcc file.c
But is this what you really want? We get a file called a.out in our
directory. This is because you aren't specifying the correct compiler
options. Taking the theory of "that which is learned first is learned
best", compile your programs using the "anal retentive"
flags. This will not only catch more errors at compile time, but will
help you create a program with less errors at run
time. The recommended options to include while compiling are:
So here is an example command line:
gcc -ansi -pedantic -Wall -O2 -o hello hello.c
This produces an executable called hello in the directory where
hello.c resides.
