Programming in C: Unit I (b): Introduction to C

Files Used in a C Program

The source code file contains the source code of the program. The file extension of any C source code file is ‘.c'.

FILES USED IN A C PROGRAM

Every C program has four kinds of files associated with it (Figure 2.3). These include:

Source Code Files

The source code file contains the source code of the program. The file extension of any C source code file is ‘.c'. This file contains C source code that defines the main function and maybe other functions. The main() function is the starting point of execution when you successfully compile and run the program. A C program in general may include even other source code files (with the file extension. c).

Header Files

When working with large projects, it is often desirable to separate out certain subroutines from the main() function of the program. There also may be a case that the same subroutine has to be used in different programs. In the latter case, one option is to copy the code of the desired subroutine from one program to another. But copying the code is often tedious as well as error prone and makes maintainability more difficult.

So, another option is to make subroutines and store them in a different file known as header file. The advantages of header files can be realized in the following cases:

• The programmer wants to use the same subroutines in different programs. For this, he simply has to compile the source code of the subroutines once, and then link to the resulting object file in any other program in which the functionalities of these sub-routines are required.

• The programmer wants to change or add subroutines, and have those changes reflected in all the other programs. In this case, he just needs to change the source file for the subroutines, recompile its source code, and then re-link programs that use them. This way time can be saved as compared to editing the subroutines in every individual program that uses them.

Programming Tip: Missing the inclusion of appropriate header files in a C program will generate an error. Such a program may compile but the linker will give an error message as it will not be able to find the functions used in the program.

Thus, we see that using a header file produces the same results as copying the header file into each source file that needs it. Also when a header file is included, the related declarations appear in only one place. If in future we need to modify the subroutines, we just need to make the changes in one place, and programs that include the header file will automatically use the new version when recompiled later. There is no need to find and change all the copies of the subroutine that has to be changed.

Conventionally, header files names ends with a 'dot h' (.h) extension and names can use only letters, digits, dashes, and underscores. Although some standard header files are automatically available to C programmers, in addition to those header files, the programmer may have his own user-defined header files.

Standard Header Files In the program that we have written till now, we used printf() function that has not been written by us. We do not know the details of how this function works. Such functions that are provided by all C compilers are included in standard header files. Examples of these standard header files include:

string.h : for string handling functions

stdlib.h : for some miscellaneous functions

stdio.h : for standardized input and output functions

math.h : for mathematical functions

• alloc.h : for dynamic memory allocation

• conio.h : for clearing the screen

All the header files are referenced at the start of the source code file that uses one or more functions from that file.

Object Files

Object files are generated by the compiler as a result of processing the source code file. Object files contain compact binary code of the function definitions. Linker uses these object files to produce an executable file (.exe file) by combining the object files together. Object files have a ‘.o' extension, although some operating systems including Windows and MS-DOS have a ‘.obj' extension for the object file.

Binary Executable Files

The binary executable file is generated by the linker. The linker links the various object files to produce a binary file that can be directly executed. On Windows operating system, the executable files have an '.exe' extension.

Programming in C: Unit I (b): Introduction to C : Tag: : - Files Used in a C Program