Re: global variable declaration in header
- From: pete <pfiland@xxxxxxxxxxxxxx>
- Date: Thu, 31 May 2007 18:01:36 GMT
nszabolcs wrote:
According to
http://c-faq.com/decl/decldef.html
global variable declaration should have 'extern' storage class
specifier, but multiple definition with at most one initialisation is
a common extension.
So is this style acceptable?
No. You have defined external variables with external linkage
with the same name in several files.
(see example below)
Is there any caveats against it (beside the 'common extension'
comment)
example code:
// common.h:
int i;
Change the above line to
extern int i;
void print();
void inc();
// io.c:
#include <stdio.h>
#include "common.h"
void print() {
printf("%d\n", i);
}
// calc.c:
#include "common.h"
void inc() {
i++;
}
// main.c:
#include "common.h"
And write here
int i;
int main() {
i = 0;
print();
inc();
print();
return 0;
}
--
pete
.
- References:
- global variable declaration in header
- From: nszabolcs
- global variable declaration in header
- Prev by Date: Re: small C compiler that will run on Vista
- Next by Date: Re: I've read K&R, what now?
- Previous by thread: Re: global variable declaration in header
- Next by thread: can any bdy help me
- Index(es):
Relevant Pages
|