Re: global variable declaration in header



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
.



Relevant Pages