Storage Classes and Scope

We gave storage classes a passing mention in Chapter 10, "Arrays and Pointers," but let's take a more in-depth look now. As mentioned in Chapter 10, local variables are known only to the functions containing them. However, C also offers the possibility of global variables, those known to several functions. Suppose you want both main() and critic() to have access to the variable Units. You can do this by declaring Units so that it belongs to the external storage class, as shown in Listing 13.1. (Here we follow a common, but not universal, convention of uppercasing the first character of a global variable name.)

Listing 13.1 The global.c program.
 /* global.c -- uses an external variable */ #include <stdio.h> int Units; ...

Get C Primer Plus®, Third Edition 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.