First of all, there is never a reason to use global, non-constant variables in any application. Global, as in available to the whole program.
There might however be some cases where static
file scope variables make sense. Note that the static
keyword blocks the variable from being global, it is only available to the translation unit (meaning c file plus included headers) where it is declared.
Some examples where such designs make sense:
When designing autonomous code modules (call them classes, ADTs or whatever) and you can be sure that only one instance exists, it might be convenient to use static
file scope variables as "poor man's private
". As soon as you provide some sort of API to the caller, you will almost always have a need for private, internal variables.
This practice common in embedded systems single-core programming. Less so in desktop programming, where re-entrant code is much more important.
Special cases. When sharing variables between the caller and an interrupt service routine or a callback function, parameter passing might not be an option. And then you have no other choice but to use static
file scope variables.
(Such variables should also be declared as volatile
to prevent compiler optimizer bugs, in cases where the compiler doesn't realize that the ISR/callback is executed.)
The only reason why you would ever write a file larger than 2k LOC is because you are writing some sort of library that for some reason needs to be delivered as a single file. For example, if you are writing one of the more complex library files of the standard C library.
If that is not what you are doing, there is never any excuse to write a 6k LOC file, period. Let alone to fill it with complex data types.
Even when writing something like the first case above, there is nothing stopping you from splitting the file in several. Most often it is actually the "main.c" that escalates beyond 2k LOC, and that one should be easy to split.
So unfortunately, I suspect that the code you are maintaining is written by a beginner. It is not unusual, pretty much everyone who works as professional programmers write such monsters during their first 5 years or so. Because sound program design is something you mostly learn through experience.