J
jchludzinski
I have 3 files (see below: a.h, w.c, ww.c). I would like to use a
single x (declared somewhere) which would global to both compilation
units: w.c & ww.c. No matter where I place the "extern" qualifier - it
appears to work: x is shared between w.c and ww.c. Or if I simple
don't use "extern", it works. Why? What is the correct (or simply
preferred) usage?
---John
PS> I'm using gcc (GCC) 4.0.2.
/*---------------a.h-----------------*/
#ifndef _A_H
#define _A_H
extern int x;
void g( void );
#endif /* _A_H */
/*---------------w.c-----------------*/
#include <stdio.h>
#include <stdlib.h>
#include "a.h"
int x;
void g( void )
{
x = 12;
fprintf( stderr, "x = %d\n", x );
}
/*---------------ww.c-----------------*/
#include <stdio.h>
#include <stdlib.h>
#include "a.h"
int x;
int main( int argc, char *argv[] )
{
g();
x = 144;
fprintf( stderr, "x = %d\n", x );
g();
fprintf( stderr, "x = %d\n", x );
}
single x (declared somewhere) which would global to both compilation
units: w.c & ww.c. No matter where I place the "extern" qualifier - it
appears to work: x is shared between w.c and ww.c. Or if I simple
don't use "extern", it works. Why? What is the correct (or simply
preferred) usage?
---John
PS> I'm using gcc (GCC) 4.0.2.
/*---------------a.h-----------------*/
#ifndef _A_H
#define _A_H
extern int x;
void g( void );
#endif /* _A_H */
/*---------------w.c-----------------*/
#include <stdio.h>
#include <stdlib.h>
#include "a.h"
int x;
void g( void )
{
x = 12;
fprintf( stderr, "x = %d\n", x );
}
/*---------------ww.c-----------------*/
#include <stdio.h>
#include <stdlib.h>
#include "a.h"
int x;
int main( int argc, char *argv[] )
{
g();
x = 144;
fprintf( stderr, "x = %d\n", x );
g();
fprintf( stderr, "x = %d\n", x );
}