Variable Scope
Automatic variables declared in a body of code are visible from the moment of the declaration until program execution reaches the end of the body.
1: int main( int argc, char *argv[ ] ) 2: { 3: int cnt; 4: /* sum is not visible here as it has not been declared */ 5: { 6: int sum; 7: /* cnt is still visible here as 8: execution has not reached the body 9: in which it was declared */ 10: } 11: /* sum is no longer visible as the body 12: in which it was declared has ended */ 13: }
A variable is said to be in scope when it is visible and out of scope when it is not.
Variables defined outside a function are called global variables.
If a variable declaration occurs outside a function, its scope is extended to all functions and code bodies that are defined after the point of the declaration.
1: int sum; // a global variable visible 2: // until the end of file 3: 4: int main( int argc, char *argv[ ] ) 5: { 6: sum = add2Nums( 1, 2 ); 7: } 8: 9: int add2Nums( int num1, int num2 ) 10: { 11: return( num1 + num2 ); 12: }
This example takes into account nearly everything discussed so far. As required by the GNU C compiler, the function main is defined as returning int and expecting the parameters of argc and argv. When main is called, it assigns the value of the global variable sum the result returned by the function add2Nums.
The function add2Nums expects two arguments of type int, which main provides by passing 1 and 2.
Notice that add2Nums does not have to return anything but could make the assignment directly to sum.
9: void add2Nums( int num1, int num2 ) 10: { 11: sum = num1 + num2; 12: }
As the variable sum is defined before the function main, it is visible to main as well as to add2Nums. Notice that as add2Nums now has a void return type the return statement may be omitted. Further, main would simply invoke add2Nums as
6: add2Nums( 1, 2 );
A significant detail has been overlooked in this example; there is no declaration of add2Nums prior to its invocation by main.
Figure 3.1 shows the error issued when an attempt is made to compile this sample.
The GNU C compiler is relatively clear on what is wrong with the sample code.
The compiler makes up an implicit declaration when no actual declaration is found before a function is called. Implicit declarations are avoided by explicitly defining functions.
Notice the portion of the error output in Figure 3.1 that reads
sample.c:11: warning: 'add2Nums' was previously implicitly declared to return 'int'
This is consistent with the description in Chapter 2, section "Forward Declarations," page 64 of the compiler's behavior. Anything the compiler doesn't recognize is assumed to be an int, which is the default data type in C.
At line 6, according to the error output, the compiler found an implicit declaration. This line corresponds to the first use of the function. Later, at line 11, it found the actual definition of the function that wasn't consistent with what was implied (the compiler's assumption).
To resolve the error, simply add a new line at the beginning of the file declaring the function prior to its use:
0: void add2Nums( int, int );
Knowing how to define, declare, and employ functions and variables correctly is critical in advancing our knowledge of the C programming language. Building on this knowledge, it is important to become familiar with the functions that C provides us.