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 - C Tutorial
From the course: Advanced C Programming: Optimize Performance and Efficiency
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Set up an external variable4m 16s
-
(Locked)
Typecast variables4m 46s
-
(Locked)
Challenge: Specifying a cast26s
-
(Locked)
Solution: Specifying a cast51s
-
(Locked)
Take advantage of static variables3m 20s
-
(Locked)
Challenge: Setting up a static variable23s
-
(Locked)
Solution: Setting up a static variable54s
-
(Locked)
-
-
-