O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required