global variable declaration in header

N

nszabolcs

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? (see example below)
Is there any caveats against it (beside the 'common extension'
comment)
example code:

// common.h:
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"
int main() {
i = 0;
print();
inc();
print();
return 0;
}


thanks for your advice
nsz
 
O

osmium

nszabolcs said:
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? (see example below)
Is there any caveats against it (beside the 'common extension'
comment)
example code:

// common.h:
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"
int main() {
i = 0;
print();
inc();
print();
return 0;
}

I can't relate your question to your solution. I don't see "extern" in
common.h. There's only *one* variable, right? Named i?
The rules are different for functions.
 
R

Richard Tobin

nszabolcs said:
So is this style acceptable? (see example below) [...]
// common.h:
int i;
void print();
void inc();

This is not good style. It defines (rather than merely declares) the
variable i in each file that includes common.h. Though, as the
standard and FAQ state, this works on many systems, it is better to
say what you mean, and merely declare i in the header file with
"extern int i", and define it in one of the .c files with "int i".

This matches what you do with functions, except that a function
declaration implicitly has then "extern" anyway. Some people prefer
to explicitly write "extern" even on function declarations, for
consistency.

-- Richard
 
N

nszabolcs

Richard said:
nszabolcs said:
So is this style acceptable? (see example below) [...]
// common.h:
int i;
void print();
void inc();

This is not good style. It defines (rather than merely declares) the
variable i in each file that includes common.h. Though, as the
standard and FAQ state, this works on many systems, it is better to
say what you mean, and merely declare i in the header file with
"extern int i", and define it in one of the .c files with "int i".
thanks
i like minimalistic code, but it seems it's not quite ok to leave out
extern here
 
P

pete

nszabolcs said:
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;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top