Hi, here are some remarks for handling .c/.h files:

1) Make a .h file for each .c file and put only the functions into the .c file, all other stuff belongs into the .h file. Make a prototype for each function.

2) Then, in all .c files, only include .h files, which are necessary for the code in your .c file, like called functions, types, etc. In the correspponding .h file, only include .h files as well for the stuff which is necessary in the header file; this is mostly done for self defined types.

3) To enforce the right order of headers, add safe guards. If your file e.g. is named "myFile.h", this is it:

Code:
#ifndef myFile_h
#define myFile_h

//...

#endif /* myFile_h */



This ensures, that header files are only included once and at the very first position in your compiled code, when it is needed.

4) You might wonder where you include the .c files? Yes, I didn't mentioned that. That is a bit tricky, though. In Visual Studio, all .c files are compiled automatically, but in Lite-C only stuff will be compiled, that was included. So, since you only include in .c and .h files header files (.h), here is solution which works for me pretty well: each header includes the .c file at the very bottom, to ensure, that the code is inserted after the header data and prototypes:

myFile.c:
Code:
#ifndef myFile_c
#define myFile_c

#include "myFile.h"

//...

#endif /* myFile_c*/



myFile.h:
Code:
#ifndef myFile_h
#define myFile_h

//...

#include "myFile.c"

#endif /* myFile_h */



This way, the code is included at the very first point in your code, where you need it. If the code itself needs code, it is included beforehand. And since I have different guards around the header and the code, neither code nor headers will be duplicated.

[EDIT]

For globals, try to maintain a clear syntax that points out, that they are globals. I always put a "g_" before the name of the global variable, like int g_lifes = 42; or so. This doesn't look pretty after all - you might ask "why don't writing "lifes" instead? ... Well, this is a valid question and I made the experience, that you often name local variables very "intuitively" and in some rare cases you might use the same name as a global variable and try to process both, which apparently doesn't work, because you use the local variable instead of the global one. This is very frustrating and rare, but you'll be glad if you can circumvent this.

Last edited by HeelX; 02/06/12 13:22.