I have a quick questions regarding a a certain style of coding I have seen that I found to be rather unique. It entails including C files within a program to allow for easy changes of constants and other variables on the fly. This method is used because of the ease of finding/changing an automated file(s) that is produced via excel or other programs. I have read that this can create funny complier issues, but I have seen very few issues with the method when in use.

This is used more for embedded programming because it can be tedious to keep track of variables, but could easily be used in other areas. I was just curious as to if this is common, what may be the consequences of something like this, and is there a better way to keep track of data in a program otherwise.

for example:


#include "autoGenConstants.c"

  //Do work using variables in above c file


With such a small example it is hard to see why this would be useful, but in larger projects with many variables and definitions it becomes very helpful.

Thanks for any info.

It seems to me that .h files would be better for this, if all you're doing is managing constants (and not actual executable code).

The standard place to include header files, of course, is at the beginning of a source file. Including it in the body of a function doesn't really make sense unless you're actually declaring local variable by way of the source file. In which case I would say it's a pretty bad practise, because you need to look at another file just to figure out what the local variables are in a function.

I'd probably use macros in this case: you can redefine macros, after all, and in header files if you choose (which you could include at different points in the source file).

It really depends on what's in the file you're including, I guess.

I realize that .h files are the standard practice, which is why I am bringing this questions up. The typical use of this is style is for define macros or really long structures, which could be easily done in an h file as well. But sometimes they are only local variables, and it is occasionally used to do some executable code. It's much easier to change a cell in a master excel file than trying to find the appropriate file and line where you put a command/variable.

Thinking of this on a larger scale is where this makes more sense, with over 50 or so files to keep track of.

And yes the variables are in another file that needs to be looked at, but you can just look at a spread sheet instead now. Otherwise you would have to call up an h file anyways, so its not much different. The complier would also just stick the code in so, there should be no loss in performance of the program for this I believe...

This method is more about efficiency and development modifications than standard techniques. You are basically writing a program (VBA for example) to write/modify parts of your program for you.

Well, if you think it makes the program easier to understand, then what can I say? :)

I really do think that there must be a better way, but unless you post some real code I probably won't think of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.