Get A Spiny Norman Shirt Get A Shameless42 Shirt Get A Tux Shirt
top pipe
Get Firefox!

C For Yourself

Anatomy Lesson

Before writing your first program, you should get familiar with the basic files found in a C program, and the structure of each of these files. The problem is that various forms of C have slightly different naming conventions. This isn't normally a problem because the files are functionally similar. You'll just need to remember which filenames you are dealing with as change from one flavor to another. To add to the confusion, most flavors allow you an easy way around these conventions. However, I would strongly advise against that kind of thing. Most development environments have a collection of tools that expect files to follow the prevailing style, and will be rendered useless or difficult to use by a silly cosmetic detail.

The Backbone: Your Makefile

If you use the analogy of a backbone being the main anchor point for the parts of the body, then you have to call the Makefile the backbone of a C program. If you are using a GUI based Integrated Development Environment, then the details of the Makefile may be obscured or completely hidden from you. If you are writing "Macho" C like most of the pro's do, then you will want to come up with one. I don't intend to include a lesson on Makefiles here, so if you need one, check your manual for a tutorial. Luckily, very simple programs, like the ones that you will see in examples, don't require a Makefile.

A Makefile is normally a plain text file that contains rules, scripts, and instructions for performing a task. This file is then read by a program named make, which performs the tasks specified. The components of a Makefile can be extremely cryptic, and have caused many sleepless nights for many programmers. If make weren't one of the most powerful tools in the programmers toolbox, it would have been done away with years go. It isn't surprising that the first feature of any GUI based IDE is abstracting the Makefile to a series of checkboxes and settings.

If you would like to learn more about make, check out:
This very basic look at make and makefiles or
Another basic look at make and makefiles or
You'll find a little more detailed info here
and the full GNU Make manual here

The Muscle: Your Source Files

Source files can contain anything within the C language. This includes functions, macros, definitions, declarations, comments, and anything else that a compiler understands.

Your source file, usually named <myprogram>.c or something similar, is the only file that is required for a C program. Though you can certainly create a program using just this single file, it can become difficult to manage very quickly. For this reason, most professional programmers start a project with plans to break up the source code based on some sort of criteria. I've seen some strange schemes for this, including arranging functions alphabetcally (for example: all functions starting with 'A' go in a file called 'a.c'). The ones that seem to work the best involve breaking the code into functional blocks.

Sometimes, there is a good reason for putting everything in a single source file. Supplying a small snippet of sample code for an SDK is probably the most common of these. Writing a tiny application to test a theory is another. In most cases, however, you will want to break things up.

The Bones: Your Header Files

The next kind of file you will be dealing with is the Header file. A header file, by convention, consists of definitions and type declarations that can be used by the module's source code itself, or by external source code that wishes to interract with your module. These header files usually end with a suffix of .h, and are included (more on include later) in your source file. Once included, the header file is treated as if it appears in the source code beginning at the line where it is included.

There really isn't a hard and fast rule about which declarations must appear in the headers and which must appear in the source code file. The only rule that applies here is that a header file should never contain executable code. It can, however, contain macros, which often cause the insertion of executable code through text substitution.

The Organs: Your Object Files

The Memories: Your Library Files

Community Property: Shared Libraries

Next Issue: Getting Started >>
Valid XHTML 1.1!