Learn about Source Files in C programming

A C program’s source code consists of function definitions, global declarations, and preprocessing instructions. One source file contains the source code for small programs. Larger C programs have many source files. Because preprocessor directives and global declarations are typically used to define function definitions, source files typically have the internal organization shown below:

Preprocessor directives
Global declarations
Function definitions 

C facilitates modular programming by letting you divide an application into as many source and header files as you’d like, then edit and build each one independently. Logically linked functions, like the program‘s user interface functions, are often contained in each source file. C source files are often labeled with the filename suffix.C.

The first source file, containing the main( ) function

// circle.c: Prints the areas of circles.
// Uses circulararea.c for the math

double circularArea( double r );
int main( )
/* ... As in Example 1-1 ... */

The second source file, containing the circularArea( ) function

// circulararea.c: Calculates the areas of circles.
// Called by main( ) in circle.c

double circularArea( double r )
/* ... As in Example 1-1 ... */

When a program is composed of multiple source files, it is necessary to specify the identical macros and constants as well as declare the same functions and global variables in numerous of the files. Thus, throughout a program, these definitions and declarations provide a kind of file header that is essentially constant. You can write this information only once in a separate header file for consistency and simplicity, and then use the #include directive in each source code file to reference the header file. The filename suffix.h is typically used to identify header files. Other files may be included by a header file that is expressly included in a C source file.

A translation unit consists of all the header files that are included with each C source file. The source code is parsed into tokens, the smallest semantic units, like variable names and operators, by the compiler as it progressively goes through the contents of the translation unit. For further details, refer to the section “Tokens,” which concludes this chapter.

You have a lot of flexibility when it comes to formatting the source code because any amount of whitespace characters can appear in between two consecutive tokens. To format “human-readable” source code, you can use spaces, tabs, and blank lines as much as you like. There are no limitations about line breaks or indenting. The preprocessor directives are a little less flexible: they can only ever occur on their own lines, and the hash mark (#) that starts the line cannot be preceded by any characters other than spaces or tabs.

For formatting source code, there are numerous conventions and “house styles” that apply. These common rules are included in the majority of them:

For every new declaration or statement, begin a new line.

To represent the nested structure of block statements, use indentation.

Naveed Tawargeri

Hi, I'm Naveed Tawargeri, and I'm the owner and creator of this blog. I'm a Software Developer with a passion for Programming.