Use of 'extern' keyword
- From: "siliconwafer" <spdandavate@xxxxxxxxx>
- Date: 28 Jul 2005 03:02:33 -0700
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
.
- Follow-Ups:
- Re: Use of 'extern' keyword
- From: Jack Klein
- Re: Use of 'extern' keyword
- From: Antonio Contreras
- Re: Use of 'extern' keyword
- Prev by Date: Re: gcc 4 signed vs unsigned char
- Next by Date: malloc modifying a passed string
- Previous by thread: question regarding precedence and associativity of ++ and *
- Next by thread: Re: Use of 'extern' keyword
- Index(es):