Understanding Variable Scope

The final topic we'll discuss in this chapter is variable scope—in other words, where variables exist. This is pertinent to functions because variables within a function are not accessible by code outside a function. And, for that matter, variables outside a function are not necessarily accessible inside the function.

Understanding Static Variables

Variables within functions are different not only because of variable scope but also because they can be static. Let's start with this example:

void up_one (void) {
   int num = 0;
   printf ("%d\n", ++num);
}

Even though num is incremented with each up_one() call, its ending value will only be 1 because it's reinitialized to 0 each time the function is called. In other words, ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.