Rrjrjtlokrthjji
Rrjrjtlokrthjji

Reputation: 612

Variables in header file to be shared to many .c files

I create a header file named variables.h to store all the variables to be used in the program.Then i cropped the program into seperate prototypes .h files and .c files.

I decided to create a variables.c file to define the variables.In variables.h they have the extern keyword and in the c file they are defined.However when i include "variables.h" in the other c files,i get errors like "missing binary operator", "var_1 undefined" etc.

Suggestions??

Upvotes: 0

Views: 706

Answers (1)

Adam Mihalcin
Adam Mihalcin

Reputation: 14458

I have a suggestion: make use of scope.

Well-written C programs rarely need many global variables, and most global variables that a well-written C program needs are needed by only one or two .c files. It doesn't make sense to use the same global variables throughout all the .c files in a large program, because then you lose the benefits of information hiding and modularity. Your program becomes brittle and hard to change, because a change to one part of the program affects the global variables and thus affects the entire rest of the program.


EDIT: Suggestions for avoiding global variables

Divide the code into modules that each have one job to do, and then implement that job without depending on any global variables visible outside the module. Note that not depending on any global variables at all fits this criterion as well, and is indeed preferred. Also, I like the suggestion given in the StackOverflow answer here.

Let's look at a quick example of dividing a program into modules. Commonly, a user-visible program will have some user interface logic, some business logic, and maybe a database connection overlay. Each one of these things represents at least one module, probably several. As our example, let's leave off the database and just consider a program like the Unix wc. It reads a file, counts the words, and prints the number of words. Simple, right? Well, even this simple program should have one module for parsing command-line arguments and putting those arguments into a struct, one module for actually counting the words, and one module for printing the word count. The user interface module (in this case, one file with the main function) passes the command-line arguments to the parser, which returns a pointer to a heap-allocated struct containing information about the command-line arguments. Then, the main module calls the word counting module once for every filename found on the command line, and the word counting module returns the word count information. Then, the main module calls into the printing module, which uses the command-line flags to determine which information to print and how to format it.

Now let's say that you have designed your modules, and you have one or more .c files per module. There may still be some global state that you need. In this case, each module's .c file should declare all the global variables, and unless the module has more than one .c file, there should be no other file in the entire project that uses that variable, either by including with an extern or in some other way.

Upvotes: 1

Related Questions