Creating Source Code in C

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.










dummies

Source:http://www.dummies.com/how-to/content/creating-source-code-in-c.html

No comments:

Post a Comment