If you want to learn the fundamentals of C programming, you've come to the right place. This article is excerpted from the book Beginning C, third edition, written by Ivor Horton (Apress, 2004; ISBN: 1590592530).
C IS A POWERFUL AND COMPACT computer language that allows you to write programs that specify exactly what you want your computer to do. Youíre in charge: you create a program, which is just a set of instructions, and your computer will follow those instructions.
Programming in C isnít difficult, as youíre about to find out. Iím going to teach you all the fundamentals of C programming in an enjoyable and easy-to-understand way, and by the end of this chapter, youíll have written your first few C programs. Itís as easy as that!
In this chapter youíll learn
How to create C programs
How C programs are organized
How to write your own program to display text on the screen
Creating C Programs
There are four fundamental stages, or processes, in the creation of any C program:
Youíll soon know all these processes like the back of your hand (youíll be doing them so easily and so often), but first letís consider what each process is and how it contributes to the creation of a C program.
This is the process of creating and editing C source codeóthe name given to the program instructions you write. Some C compilers come with a specific editor that can provide a lot of assistance in managing your programs. In fact, an editor often provides a complete environment for writing, managing, developing, and testing your programs. This is sometimes called an integrated development environment, or IDE.
You can also use other editors to create your source files, but they must store the code as plain text without any extra formatting data embedded in it. In general, if you have a compiler system with an editor included, then it will provide a lot of features that make it easier to write and organize your source programs. There will usually be automatic facilities for laying out the program text appropriately and color highlighting for important language elements, which not only makes your code more readable, but also provides a clear indicator when you make errors in keying in such words.
If youíre working in UNIX, then the most common text editor is the vi editor. Alternately, you might prefer to use theemacs editor.
From a PC, you could use one the many freeware and shareware programming editors. These will often provide a lot of help in ensuring your code is correct with syntax highlighting and autoindenting of your code. Donít use word processors such as Microsoft Word, as they arenít suitable for producing program code.
The compiler converts your source code into machine language, and detects and reports errors in the compilation process. The input to this stage is the file you produced during your editing, which is usually referred to as a source file.
The compiler can detect a wide range of errors that are due to invalid or unrecognized program code, as well as structural errors where, for example, part of a program can never be executed. The output from the compiler is known as object code and is stored in files called object files, which usually have names with the extension.obj. The compiler can detect several different kinds of errors during the translation process, and most of these will prevent the object file from being created.
NOTE In UNIX, object files have the extension.o.
The result of a successful compilation is a file with the same name that you used for the source file, but with the.objextension.
If youíre working in UNIX, then the standard command to compile your C programs will be cc. You can use it like this:
cc -c myprog.c
wheremyprog.c is the program you want to compile. Note that if you omit the-oflag, your program will automatically be linked as well. If youíre using the GNUís Not UNIX (GNU) compiler, you should type
gcc -c myprog.c.
The result of a successful compilation will be an object file.
Most C compilers will have a standard compile option, whether itís from the command line (such ascc myprog.c) or a menu option from within an IDE (where youíll find a Compile menu option).
The linker combines the various modules generated by the compiler from source code files, adds required code modules from program libraries supplied as part of C, and welds everything into an executable whole. The linker can also detect and report errorsófor example, if part of your program is missing or a nonexistent library component is referenced.
In practice, if your program is of any significant size, it will consist of several separate source code files, which can then be linked. A large program may be difficult to write in one session. By breaking it up into a number of smaller source files, you can make the development of the program a whole lot easier. The source files can be compiled separately, which makes eliminating simple typographical errors a bit easier. Furthermore, the whole program can usually be developed incrementally. Each source file will have its own file name, and the set of source files that make up the program will usually be integrated under a project name, which is used to refer to the whole program.
Program libraries support and extend the C language by providing routines to carry out operations that arenít part of the language. For example, libraries contain routines that support operations such as performing input and output, calculating a square root, comparing two character strings, or obtaining date and time information.
A failure during the linking phase means that, once again, you have to go back and edit your source code. Success, on the other hand, will produce an executable file. In a Microsoft Windows environment, this executable file will have the.exeextension; in UNIX, there will be no such extension, but the file will be of an executable type.
In UNIX, the modules that are to be linked are given together with thecccommand, for example:
cc myprog.c mod1.c mod2.o
These three modules will be linked. Notice that the last module here has the extension.o. This is because it has previously been compiled: the.oextension tells the compiler that the module is waiting to be linked and doesnít need to be compiled again. The output of this stage is a file calleda.out, which you should then rename to something more meaningful.
An alternative form to this is as follows:
cc -o myprog myprog.c mod.c mod2.o
This will compile and link the modulemyprog.cand create an executable file calledmyprog(defined straight after the-oflag).
Many C compilers also have a Build option, which will compile and link your program in one step. This option will usually be found, within an IDE, in the Compile menu; alternatively, it may have a menu of its own.
The execution stage is when you run your program, having completed all the previous processes successfully. Unfortunately, this stage can also generate a wide variety of error conditions, ranging from producing the wrong output to sitting there and doing nothing, perhaps crashing your computer for good measure. In all cases, itís back to the editing process to check your source code.
Now for the good news: this is the stage where, at last, you get to see your computer doing exactly what you told it to do!
In UNIX and DOS, to execute a program you just enter the name of the file that has been compiled and linked.
In most IDEs, youíll find an appropriate menu command that allows you to Run or Execute your compiled program. This option may have a menu of its own, or you may find it under the Compile menu option.
In Windows, you can use Windows Explorer to locate the.exefile of your program, and then you can double-click the file.
The processes of editing, compiling, linking, and executing are essentially the same for developing programs in any environment and with any compiled language. Figure 1-1 summarizes how you would typically pass through each of these processes as you create your own C programs.