5.6 Source code organization

While many simple programs fit into a single C or CPP source file, any serious project is going to need splitting up into several source files in order to be manageable. However, many beginning programmers may not realize what the point of this is – especially since some may have tried it themselves and run into so many problems that they decided it wasn’t worth the effort. This article should explain why to do it, and how to do it properly.

Why split code into several files?.

The first question some programmers ask when they see a directory full of separate code files is, “why isn’t it all just in one file?” because, they just don’t see the point of scattering the code about.

Splitting any reasonably-sized project up has some advantages, the most significant of which are the following:

  1. Speed up compilation – most compilers work on a file at a time. So if all your 10000 lines of code is in one file, and you change one line, then you have to recompile 10000 lines of code. On the other hand, if your 10000 lines of code are spread evenly across 10 files, then changing one line will only require 1000 lines of code to be recompiled. The 9000 lines in the other 9 files will not need recompiling. (Linking time is unaffected.)
  2. Increase organization – Splitting your code along logical files will make it easier for you to find functions, variables, struct/class declarations, and so on. Even with the ability to jump directly to a given identifier that is provided in many editors and development environments (such as Microsoft Visual C++), there will always be times when you need to scan the code manually to look for something. Just as splitting the code up reduces the amount of code you need to recompile, it also reduces the amount of code you need to read in order to find something. Imagine that you need to find a fix you made to the sound code a few weeks ago. If you have one large file called GAME.C, that’s potentially a lot of searching. If you have several small files called GRAPHICS.C, MAINLOOP.C, SOUND.C, and INPUT.C, you know where to look, cutting your browsing time by 3/4.
  3. Facilitate code reuse – If your code is carefully split up into sections that operate largely independently of each other, this lets you use that code in another project, saving you a lot of rewriting for your next projects. There is a lot more to writing reusable code than just using a logical file organization, but without such an organization it is very difficult to know which parts of the code work together and which do not. Therefore putting subsystems and classes in a single file or carefully delineated set of files will help you later if you try to use that code in another project.
  4. Share code between projects/code reuse – By carefully separating code into certain files, you make it possible for multiple projects to use some of the same code files without duplicating them. The benefit of sharing a code file between projects rather than just using copy-and-paste is that any bug fixes you make to that file or files from one project will affect the other project, so both projects can be sure of using the most up-to-date version.
  5. Split coding responsibilities among programmers – For really large projects, this is perhaps the main reason for separating code into multiple files. It isn’t practical for more than one person to be making changes to a single file at any given time. Therefore you would need to use multiple files so that each programmer can be working on a separate part of the code without affecting the file that the other programmers are editing. Of course, there still have to be checks that 2 programmers don’t try altering the same file; version control systems such as CVS or GIT can help here.

Continue reading

5.5 Header Files

A header file is a file containing C declarations and macro definitions to be shared between several source files( .c files). You request the use of a header file in your program by including it, with the C preprocessing directive ‘#include’.

Header files serve two purposes.

  • System header files declare the interfaces to parts of the operating system. You include them in your program to supply the definitions and declarations you need to invoke system calls and libraries.
  • Your own header files contain declarations for interfaces between the source files( .c files) of your program. Each time you have a group of related declarations and macro definitions all or most of which are needed in several different source files, it is a good idea to create a header file for them.

Including a header file produces the same results as copying the header file into each source file that needs it. Such copying would be time-consuming and error-prone. With a header file, the related declarations appear in only one place. If they need to be changed, they can be changed in one place, and programs that include the header file will automatically use the new version when next recompiled. The header file eliminates the labor of finding and changing all the copies as well as the risk that a failure to find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with .h. It is most portable to use only letters, digits, dashes, and underscores in header file names, and at most one dot.

For more information on how to use *.h files you can visit the following page http://www.umich.edu/~eecs381/handouts/CHeaderFileGuidelines.pdf

3.5. Parameters and Arguments

Parameters are reference or value that is passed to a function, procedure, subroutine, command, or program.
C functions exchange information by means of parameters and arguments. The term parameter refers to any declaration within the parentheses following the function name in a function declaration or definition ( int func_name(int parm1, char param2); ); the term argument refers to any expression within the parentheses of a function call. Therefore, in the above example, main() and max() take two parameters.
The following rules apply to parameters and arguments of C functions:

  •  The maximum number of arguments (and corresponding parameters) is 253 for a single function.
  •  Arguments are separated by commas
  • The scope of function parameters is the function itself. Therefore, parameters of the same name in different functions are unrelated or can be used.

Argument/parameter passing
Functions in any programming language mainly communicate with the help of parameters. This parameters can be used among functions in two different ways, passing by value and passing by reference.
Passing by value: when passing an argument by value it means giving the calling function the value of the variables. This will let the being called function to change/modify the value of the variables in its context or scope. But, will not change what is on the agument/variable outside that function. It simply copies the variables value and use it.

  • For example take a look at the following program:
#include >stdio.h>
int main(){
	int x,y;
	x=5, y= 10;
	byvalue(x,y);
	printf("X is: %d and Y is: %d \n",x ,y);   //value of x=5 & y=10 should not be changed
	return 0;
}
int byvalue(int param1, int param2){
	param1 += 1;
	param2 += 1;
	printf("param1 is: %d and param2 is: %d \n",param1, param2);
}

Continue reading