From the course: Advanced C Programming: Optimize Performance and Efficiency

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Set up an external variable

Set up an external variable

- [Instructor] 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 Exercise Files, 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…

Contents