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.

C Programming :: Introduction

C Programming :: Hello, World

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:

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!

C Programming :: A Glimpse at Compilation

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.

C Programming :: Introduction Summary

  1. Program with understandability in mind. Speed in writing the program may pay off now, but writing a program with readability and understandability will pay off in the future.
  2. Take advantage of the compiler's smarts! Use the "anal retentive" options!
  3. Did I mention that you should make your programs well documented and understandable? :)
  4. Note: From here on out, major topics will be highlighted in blue and subtopics will be highlighted in black.

Valid XHTML 1.0! Valid CSS!
© 1999-2008 Alfred Park (fred AT randu.org)