Saturday, May 3, 2014

objective-C global static variables

#include <stdio.h>
#include <stdlib.h>

// Declare a global variable
float lastTemperature;


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 havestatic variables

// Declare a static variable
static float lastTemperature;


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.


No comments:

Post a Comment