Files used in C Programming

A C program uses four types of files as follows:

files in c

Source Code File

  • This file includes the source code of the program.
  • The extension for these kind of files are '.c'. It defines the main and many more functions written in C.
  • main() is the starting point of the program. It may also contain other source code files.

Header Files

They have an extension '.h'. They contain the C function declarations and macro definitions that are shared between various source files.

Advantages of header files:
1. At times the programmer may want to use the same subroutines for different programs. To do this, he would just compile the code of the subroutine once and link to the resulting object file in any file in which the functionalities of this subroutine are required.

2. At times the programmer may want to change or add the subroutines and reflect those changes in all the programs. For doing this, he will have to only change the source file for the subroutines, recompile the source code and then recompile and re-link the program.

This tells us that including a header file will make it easier at all levels of the program. If we need to modify anything then changes are made only in the subroutines after which all the changes will be reflected.

Standard header files

C provides us with some standard header files which are available easily.

Common standard header files are:
i) string.h – used for handling string functions.
ii) stdlib.h – used for some miscellaneous functions.
iii) stdio.h – used for giving standardized input and output.
iv) math.h – used for mathematical functions.
v) alloc.h – used for dynamic memory allocation.
vi) conio.h – used for clearing the screen.

The header files are added at the start of the source code so that they can be used by more than one function of the same file.

Object files

  • They are the files that are generated by the compiler as the source code file is processed.
  • These files generally contain the binary code of the function definitions.
  • The object file is used by the linker for producing an executable file for combining the object files together. It has a '.o' extension.

Executable file

  • This file is generated by the linker.
  • Various object files are linked by the linker for producing a binary file which will be executed directly.
  • They have an '.exe' extension.