Home

Creating Source Code in C

|
|  Updated:  
2016-03-26 22:42:59
|   From The Book:  
No items found.
C All-in-One Desk Reference For Dummies
Explore Book
Buy On Amazon

When you create a program, you tell the computer what to do. Because the computer can't understand speech and because hitting it — no matter what emotional value that has for you — does little to the PC, your final line of communications is to write the computer a note — a file on disk.

To create the note, you use a program called a text editor. This is a primitive version of a word processor, minus all the fancy formatting and printing controls. The text editor enables you type text — that's about all.

Using your text editor, you create what's called a source code file. The only special thing about this file is that it contains instructions that tell the computer what to do. And although it would be nice to write instructions like "Make a funny noise," the truth is that you must write instructions in a tongue the computer understands. In this case, the instructions are written in the C language.

After you finish writing the instructions, you save them in a file on disk. Have the first part of the filename be the name you want to give the final program. For example, if you were creating a game called UFO Kill, the source code file should have a first name of UFOKILL.

The second part of the filename, the extension, must be C, for the C language. This is important! Most text files end in TXT or sometimes DOC. For the C language, your files must end in .C (dot-C), such as UFOKILL.C.

  • The source code file is a text file on disk. It contains instructions for the computer that are written in the C programming language.
  • You use a text editor to create the source code file. Most C compilers come with their own text editors. If yours did not, you can use a third-party text editor to do the job. (Some programmers prefer third-party text editors.)
  • You can use a word processor to create your source code files. However, save the file as a "plain text" or "DOS text" or "ASCII" or "unformatted" file. (Using a word processor to create a source code file is a lot like using a 747 to drive to work; it's a little too much power for the job at hand.)
  • The source code file ends with a C as its filename extension.
  • The first part of the source code filename should be the name of the program you want to create.
  • Be clever when you name your source code.

The compiler

After the source code is created and saved to disk, it must be translated into a language the computer can understand. This is a job for the compiler to do.

The compiler is a special program that reads the instructions stored in the source code file. The compiler runs through each instruction and translates it into the secret code understood only by the computer's microprocessor.

If all goes well and the compiler is duly pleased with your source code, it produces an object code file, a second file that's saved on disk. The object code file has the same first name as the source code file, but it ends in .OBJ (dot-OBJ). So for that UFO game, it would be UFOKILL.OBJ.

If the compiler doesn't understand something, it displays an error message on the screen. At that point, you can gnash your teeth and sit and stew. Then go back and edit the source code file again, fixing whatever error the compiler found. (This isn't as tough as it sounds.) Then you attempt to compile the program again — you recompile.

After the compiler does its job, the program isn't finished. A third step is required: linking.

The linker

The linker is a program, like the compiler. Its job is to create the final program file.

What the linker does is to take the OBJ file created by the compiler and spruce it up, producing the final program file. That file ends with either a COM or EXE extension — which is how program files are identified under DOS.

The first part of the program filename is the same as the first part of the source code filename. So if you start with UFOKILL.C, the compiler creates an object file, UFOKILL.OBJ, and then the linker creates the final program file, UFOKILL.EXE.

  • In most DOS C compilers, both the compiler's and linker's jobs are done together, one after the other. You may occasionally see "compile" and "link" listed as two steps (which they are), but with your C compiler they may be combined into one.
  • Like the compiler, when the linker sees something it can't figure out, it produces an error message. In that case, you have to decipher the error message and compile the program again (recompile).
  • The program file ends in EXE, though it's possible to tell the linker to create COM files. You can refer to your linker's documentation for pulling off that trick.

Yup, that's right: From starting with a single source code file, you end up with three files on disk: UFOKILL.C, UFOKILL.OBJ, and UFOKILL.EXE. Some compilers may anoint your hard drive with even more files.

About This Article

This article is from the book: 

No items found.

About the book author:

Dan Gookin has been writing about technology for 20 years. He has contributed articles to numerous high-tech magazines and written more than 90 books about personal computing technology, many of them accurate.
He combines his love of writing with his interest in technology to create books that are informative and entertaining, but not boring. Having sold more than 14 million titles translated into more than 30 languages, Dan can attest that his method of crafting computer tomes does seem to work.
Perhaps Dan’s most famous title is the original DOS For Dummies, published in 1991. It became the world’s fastest-selling computer book, at one time moving more copies per week than the New York Times number-one best seller (although, because it’s a reference book, it could not be listed on the NYT best seller list). That book spawned the entire line of For Dummies books, which remains a publishing phenomenon to this day.
Dan’s most recent titles include PCs For Dummies, 9th Edition; Buying a Computer For Dummies, 2005 Edition; Troubleshooting Your PC For Dummies; Dan Gookin’s Naked Windows XP; and Dan Gookin’s Naked Office. He publishes a free weekly computer newsletter, “Weekly Wambooli Salad,” and also maintains the vast and helpful Web site www.wambooli.com.