Global and static variables

In this chapter, we talked about local variables that only exist while a function is running. There are also variables that can be accessed from any function at any time. We call these global variables. To make a variable global, you declare it outside of a particular function. For example, you could add a lastTemperature variable that holds the temperature that was converted from Celsius. Add a global variable to the program:

#​i​n​c​l​u​d​e​ ​<​s​t​d​i​o​.​h​>​
#​i​n​c​l​u​d​e​ ​<​s​t​d​l​i​b​.​h​>​

/​/​ ​D​e​c​l​a​r​e​ ​a​ ​g​l​o​b​a​l​ ​v​a​r​i​a​b​l​e​
f​l​o​a​t​ ​l​a​s​t​T​e​m​p​e​r​a​t​u​r​e​;​

f​l​o​a​t​ ​f​a​h​r​e​n​h​e​i​t​F​r​o​m​C​e​l​s​i​u​s​(​f​l​o​a​t​ ​c​e​l​)​
{​
 ​ ​ ​ ​l​a​s​t​T​e​m​p​e​r​a​t​u​r​e​ ​=​ ​c​e​l​;​
 ​ ​ ​ ​f​l​o​a​t​ ​f​a​h​r​ ​=​ ​c​e​l​ ​*​ ​1​.​8​ ​+​ ​3​2​.​0​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​%​f​ ​C​e​l​s​i​u​s​ ​i​s​ ​%​f​ ​F​a​h​r​e​n​h​e​i​t​​n​"​,​ ​c​e​l​,​ ​f​a​h​r​)​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​f​a​h​r​;​
}​
i​n​t​ ​m​a​i​n​(​i​n​t​ ​a​r​g​c​,​ ​c​o​n​s​t​ ​c​h​a​r​ ​*​ ​a​r​g​v​[​]​)​
{​
 ​ ​ ​ ​f​l​o​a​t​ ​f​r​e​e​z​e​I​n​C​ ​=​ ​0​;​
 ​ ​ ​ ​f​l​o​a​t​ ​f​r​e​e​z​e​I​n​F​ ​=​ ​f​a​h​r​e​n​h​e​i​t​F​r​o​m​C​e​l​s​i​u​s​(​f​r​e​e​z​e​I​n​C​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​W​a​t​e​r​ ​f​r​e​e​z​e​s​ ​a​t​ ​%​f​ ​d​e​g​r​e​e​s​ ​F​a​h​r​e​n​h​e​i​t​​n​"​,​ ​f​r​e​e​z​e​I​n​F​)​;​
 ​ ​ ​ ​p​r​i​n​t​f​(​"​T​h​e​ ​l​a​s​t​ ​t​e​m​p​e​r​a​t​u​r​e​ ​c​o​n​v​e​r​t​e​d​ ​w​a​s​ ​%​f​​n​"​,​ ​l​a​s​t​T​e​m​p​e​r​a​t​u​r​e​)​;​
 ​ ​ ​ ​r​e​t​u​r​n​ ​E​X​I​T​_​S​U​C​C​E​S​S​;​
}​

Any complex program will involve dozens of files containing different functions. Global variables are available to the code in every one of those files. Sometimes sharing a variable between different files is what you want. But, as you can imagine, having a variable that can be accessed by multiple functions can also lead to great confusion. To deal with this, we have static variables. A static variable is like a global variable in that it is declared outside of any function. However, a static variable is only accessible from the code in the file where it was declared. So you get the non-local, exists outside of any function benefit while avoiding the you touched my variable! issue.

You can change your global variable to a static variable, but because you have only one file, main.c, it will have no effect whatsoever.

/​/​ ​D​e​c​l​a​r​e​ ​a​ ​s​t​a​t​i​c​ ​v​a​r​i​a​b​l​e​
s​t​a​t​i​c​ ​f​l​o​a​t​ ​l​a​s​t​T​e​m​p​e​r​a​t​u​r​e​;​

Both static and global variables can be given an initial value when they are created:

/​/​ ​I​n​i​t​i​a​l​i​z​e​ ​l​a​s​t​T​e​m​p​e​r​a​t​u​r​e​ ​t​o​ ​5​0​ ​d​e​g​r​e​e​s​
s​t​a​t​i​c​ ​f​l​o​a​t​ ​l​a​s​t​T​e​m​p​e​r​a​t​u​r​e​ ​=​ ​5​0​.​0​;​

If you don’t give them an initial value, they are automatically initialized to zero.

In this chapter, you have learned about functions. When we get to Objective-C in Part III, you will hear the word method – a method is very, very similar to a function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset