I have a project in C (don't ask why but it needs to be in C), where I need to track multiple pieces of data and commands for different modules. Some actions that the program will take depend on the interactions between these different pieces of data (eg. if foo == 3 and bar == 4 do X, otherwise do Y). Some of these pieces of data will be structs grouping together similar information, some will just be primitives.

I am wondering what the best way to implement this would be. Personally, I think that each piece of data should be its own variable like so:

static int module_state;
static M1_Data module_1_data;
static int last_m1_command;
static M2_Data module_2_data;

One of my coworkers is suggesting instead that I manage everything in a super struct. His reasoning is that everything can be passed around at once if need be, and it will make it easier to track where everything is, rather than having a bunch of lose variable. His idea is for something like so:

int module_state;
M1_Data module_1_data;
int last_m1_command;
M2_Data module_2_data;
} SuperState;

static SuperState state

I am not sure I agree with his reasoning. What would the preferred way to handle this, and what are the upsides/downsides of both methods other than the ones listed?

  • You should probably avoid having many global variables... (for readability reasons)... So having a somehow big struct is often better. But it is a matter of opinion. Jun 17, 2015 at 14:18
  • 1
    Any time I see variables numbered in this way, I wonder why an array, list or other data structure is not being used instead. Jun 17, 2015 at 14:37
  • Each module has its own pieces of data that are of differing types, so it would not be possible to store them all in an array. Otherwise, I would probably agree.
    – Joe
    Jun 17, 2015 at 14:39
  • possible duplicate of Confusion of scope of a variable: Is Global Variable a solution?
    – gnat
    Jun 17, 2015 at 14:41
  • @gnat: Ehm, not really. Jun 17, 2015 at 14:41

1 Answer 1


Personally, I would rather use structures, but not one common, I would use one for each module instead, with the properties I need to "publish" for other modules to be able to use.

Simple global variables are more difficult to maintain.

Using a structure per module will get rid of unnecessary dependencies (data related to a single module, and only to that module, is in the same place) between modules, and a structure is easy to pass all in once by pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.