S
siliconwafer
Hi all,
I wanted to know that is use of extern keyword mandatory in case of
global variables and functions used in other source files?
i.e consider a following piece of code from MSDN explaining extern
storage class:
/******************************************************************
SOURCE FILE ONE
*******************************************************************/
extern int i; /* Reference to i, defined below */
void next( void ); /* Function prototype */
void main()
{
i++;
printf( "%d\n", i ); /* i equals 4 */
next();
}
int i = 3; /* Definition of i */
void next( void )
{
i++;
printf( "%d\n", i ); /* i equals 5 */
other();
}
/******************************************************************
SOURCE FILE TWO
*******************************************************************/
extern int i; /* Reference to i in */
/* first source file */
void other( void )
{
i++;
printf( "%d\n", i ); /* i equals 6 */
}
Here,if I drop the extern keyword from source file 2 and compile and
link the 2 source files I get the same result as with 'extern' keyword.
i.e extern keyword is optional.same is the case with functions.
If so in which cases is the use of 'extern' mandatory?
regards,
-Siliconwafer
I wanted to know that is use of extern keyword mandatory in case of
global variables and functions used in other source files?
i.e consider a following piece of code from MSDN explaining extern
storage class:
/******************************************************************
SOURCE FILE ONE
*******************************************************************/
extern int i; /* Reference to i, defined below */
void next( void ); /* Function prototype */
void main()
{
i++;
printf( "%d\n", i ); /* i equals 4 */
next();
}
int i = 3; /* Definition of i */
void next( void )
{
i++;
printf( "%d\n", i ); /* i equals 5 */
other();
}
/******************************************************************
SOURCE FILE TWO
*******************************************************************/
extern int i; /* Reference to i in */
/* first source file */
void other( void )
{
i++;
printf( "%d\n", i ); /* i equals 6 */
}
Here,if I drop the extern keyword from source file 2 and compile and
link the 2 source files I get the same result as with 'extern' keyword.
i.e extern keyword is optional.same is the case with functions.
If so in which cases is the use of 'extern' mandatory?
regards,
-Siliconwafer