Pages

Thursday 23 June 2011

Compiling a C Program



Compiling a C program depends on the compiler as well as the operating system under use. Supposing that you are using Turbo C compiler on DOS platform, the compilation of a C program stored in a file called TEST.C, would look like as shown below:

C:\>tc test.c

Here, TC is the name of Turbo C compiler. The compilation would produce an object file called TEST.OBJ, Object files, thus produced, contain the machine language translation of the C program. However, this file is yet not executable. To make it an executable program you need to LINK the object files with library files, when TEST.EXE is produced.

Most often you shall be using an IDE for program development and therefore, you do not need to compile and link the programs explicitly as shown above. Every IDE provides a user friendly set of commands to carry out the compilation and linking automatically.

We will consider how Turbo C IDE may be used to develop and compile a C program. When you start the Turbo C IDE, the IDE provides all the necessary commands that let you write, compile and link your programs. A typical Turbo C IDE is displayed here:


Clearly, the IDE is menu driven. All the commands are grouped into menus such as; File, Edit, Search, Run, Compile, Debug, Project, Options, Window, and Help. You may take your time to navigate through the menus and see what command they offer to you.
To just give you a feel, we will interact with the IDE in form of a session. Let us create a program file called TEST.C containing the following program:
#include
main ()
{
printf("this is the first C program");
}
Never mind if you do not understand what this program means. It is just to demonstrate how you would write your programs and compile them.
To write the program into Test.C file:
1. Go to file menu.
2. Click at New command. Turbo C IDE opens a blank file for you, is shown here:



Note that Turbo C names the file as NONAMEOO.C. You can now enter your program in this file and save it with whatever name you wish to assign to it.
3. Type the program as shown below:


Now that you have entered the program into the opened file, save it as TEST.C or whatever name that suits you. Remember, the IDE automatically saves the file with C extension. All the C programs must be saved with C extension. To save this file, go to File menu once again. Click at Save command. A dialog window appears as shown below:

Type in the name - TEST and press OK. The file will be saved on the disk.
4. You can now compile the program by clicking at compile command available
in Compile menu. Alternatively we can press Alt+F9. The compilation result is displayed in compile dialogue window as shown below.

In this case the compiler reports that there is an error in the program and that the compilation could not proceed. In order to see the errors just press any key when the following window appears.

Note that there is an error at line no.1 - "Unable to open include file 'STDI.H'". Obviously the Include file name should have been STDIO.H instead of STDI.H. Correct this and recompile. This time the program would compile successfully.
1.Now that you have object program ready, you can now link it by clicking at link command in compile menu. This produces TEST. EXE.
2.In order to run the program, go to Run menu and click at Run command. You will see the output of the program as shown below.

Once you are through with the programming session, you can quit from the IDE by clicking at Exit command available in File menu.

c programmming online,basic of c language,structure of c programming,c language statements,Compiling a C Program,Pseudo Codes in c,basis data type in c ,variables in C Program,constants in C Program,c operators in C Program,special operators in C ,Decision Making,Branching & Looping in C Program,if statement in C Program,for loop in C Program

No comments:

Post a Comment