|
Conclussion:
|
|
int main( int argc, char *argv[] )
{
printf("x = %d\n", x);
}
int x = 4; /* Definition of global var x */
|
When you compile this program, you will get this error:
declare1.c: In function 'main':
declare1.c:7:23: error: 'x' undeclared (first use in this function)
printf("x = %d\n", x);
^
|
How to run the program:
|
Reason:
|
|
In a 1-pass compiler, at the time the compiler processes a variable name:
|
|
Note:
|
extern dataType varName ; |
Example:
extern int x; |
int main( int argc, char *argv[] )
{
printf("x = %d\n", x);
}
int x = 4; /* Definition of global var x */
|
We can fix this error (without moving the definition of the variable x) using a declaration:
extern int x; // Declare x
int main( int argc, char *argv[] )
{
printf("x = %d\n", x);
}
int x = 4; /* Definition of global var x */
|
I'll show you that it fixes the problem in class by compiling the updated program
|
Example:
extern int x; // Declare x
extern int x; // Declare x again
extern int x; // Declare x and again
int main( int argc, char *argv[] )
{
printf("x = %d\n", x); // Use x in a statement
}
int x = 4; /* Definition of global var x */
// Cannot have 2 definitions of x !!!
|
The variable declaration is typically used when:
|
We will postpone the discussion of multi-file C programs for a later time