Friday, June 28, 2013

Creating a Program in C

While creating a program, we need to follow certain steps. These are the steps to be followed while creating any kind of C program.

  • Create the source code.
  • Compile the Source code.
  • Link the compiled code to create executable.
  • Run the executable code.

Creating the Source Code

For creating the source code, we need to use some editor to enter our program. Generally most of the C compilers comes with editor. If your compiler doesn't have a editor, then we can go with the editors provided by the operating system. For example, Unix provides vi, Windows provides Notepad, DOS provides edit. We can use such a editor and can build the source code. 

After writing our program, we need to save the source code. While saving the source code, we can give any name of our own choice, but the name should end with an extension ".C". This extension specifies that this is a C program.

Compiling the Source Code

Compiling is the second stage in developing a C program. For converting this high level language to machine understandable language, this code is converted initially to object code. For this conversion, a compiler is used. Generally, an Operating system uses its own interpretation of compiling the C program. For example,
Microsoft C               -     cl filename.c
Borland's Turbo C     -     tcc filename.c
Borland C                  -    bcc filename.c
Zortex C                    -    ztc filename.c 
 For a Unix machine we use cc filename.c  for compiling. For most of the GUIs, it is quite simpler to compile and run, as they provide the icons for performing the same.

After compilation, we get the object code with an extension ".obj". This object file used by the linker to link with the library and convert to the executable.

Note: For a Unix system, this object file has an extension ".o" instead of ".obj".


Linking to create Executable

The object file we have obtained may have many predefined function calls. In order to get the functioning of such a function call, we need to link the object file with the library. Once the object file links with the library, all the predefined functions are called from this object file and gets the program to be useful. 

Note: Generally both the compilation and linking are combined in most of the compilers. 

Completing the development

After linking, we can run the program and check for the result. If the result is not expected, we need to go to first step again and again recompile and re-link the code to get the program corrected.