By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,382 Members | 1,688 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,382 IT Pros & Developers. It's quick & easy.

global variable declaration in header

P: n/a
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

May 31 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"nszabolcs" writes:
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.
May 31 '07 #2

P: n/a
In article <11**********************@p47g2000hsd.googlegroups .com>,
nszabolcs <ns*******@gmail.comwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
May 31 '07 #3

P: n/a

Richard Tobin wrote:
In article <11**********************@p47g2000hsd.googlegroups .com>,
nszabolcs <ns*******@gmail.comwrote:
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

May 31 '07 #4

P: n/a
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
May 31 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.