- In the C language, variables are local to the function that contains them. Outside that function, the variable's name and its value hold no significant meaning. That is, unless you declare an external variable, also known as a global variable. In that case, the variable is available to all functions in the code. Open ExerciseFile 03-01_extern1. You see two functions: f and main. Each one has its own int variable x. The value of x is set differently in each function, and that value is unaffected by whatever happens in the other function.
Build and run the code. The output shows that setting the variable x to negative one in the f function doesn't affect the value of variable x in the main function. That's pretty basic C language stuff. When you need functions to share the same variable, you create a global variable. That way the variable can be used by all functions in the code, and its value remains consistent. Open ExerciseFile 03-01_extern2. Here you see a rewrite of the previous code, but integer x is declared as an external variable.
You see it on line three. Any variable declared outside the scope of a function is external, or global. Its value is accessible to all functions. So both function f and the main function manipulate its value. Build and run the code. And you see the value of x is changed to negative one in the f function. And it retains that value when control returns to the main function. As a final example, I'll show you a Code::Blocks project file where two separate source code files, or modules, are used to hold all the code.
In Code::Blocks, you need to start a new project. Choose a console application using the C programming language. Type 03-01_extern3 as the project name. You can save it in your Lynda exercise folders. Create a release version only. Once the project file appears, open the sources branch and remove main.C. Right-click the project folder and add the two files 03-01_extern3main and 03-01extern3manipulate to the project.
In the main version of the source code file, you see that the array variable data is declared externally. It's a global variable. The source code file manipulateC also makes use of that variable, but to access it, the compiler has to be made aware of its existence. That happens on line six with a declaration, extern int data. This statement identifies the external integer array data, which is declared in another module, in this case in the main.C module.
The linker will assure that it exists when the program is built. Build and run this code. The linker combines the compiled source code files into a project file, which it then runs. Now, you may have some trouble running the file if the pathname contains spaces. If so, you see a return code of 127 when Code::Blocks tries to run the program. If that happens, go back and take the spaces out of the filename. And remember that you don't need to use the extern keyword unless there's an external or global variable defined in another module.
So now the question arises: Why not just make all variables external? The answer is that doing so would simply be sloppy programming practice. C is efficient with its variables, tossing away their storage after a function is called. Having variables local to a function makes it easier for anyone reading the code to see their declarations. Modifying a program with all external variables would be a nightmare. Normal practice is to pass variables to functions, and if required, return their values from the function.
Or you can use pointers to reference remote variables within your code. The only time I've found that external variables are required is when working with structures in a function. That topic is covered in another movie.
- Using assignment operators
- Working with arguments in the main function
- Setting up global variables
- Using static variables
- Sorting an array
- Building an array of structures
- Working with the ampersand (&) and asterisk (*) pointer operators