Programming in C (Page 1 of 9 )
Programming in C is not difficult, despite what you may have heard. Read on to start learning how to program in this powerful language. This article is excerpted from the book
Beginning C, 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:
- Editing
- Compiling
- Linking
- Executing
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.
Editing 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 the emacs 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.
Compiling 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 .obj extension.
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
where myprog.c is the program you want to compile. Note that if you omit the -o flag, 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 as cc myprog.c) or a menu option from within an IDE (where you’ll find a Compile menu option).
This article is excerpted from Beginning C by Ivor Horton (Apress, 2004; ISBN 1590592530). Check it out at your favorite bookstore today. Buy this book now. |
Next: Linking >>
More Code Examples Articles
More By Apress Publishing