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

confused about extern use

P: n/a
I have seen a header file in which one structure is defined with
extern declaration in a header file and declared one variable of that
structure in one C-file. The code goes as mentioned below . I am
confused with the way it is declared and defined.

a.h
-------

:
:
:struct abc {
unsigned long a;
unsigned long b;
};
extern struct abc abc;
:
:

file.c
-------

:
#include <a.h>
:
:
struct abc abc;
:
:

Here in the above example I am confused about, what extra the coder
going to achieve by declaring it as extern in the header file a.h.

Thanks
Das

Feb 21 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Lalatendu Das wrote:
>
Here in the above example I am confused about, what extra the coder
going to achieve by declaring it as extern in the header file a.h.
So it can be used in another compilation unit.

--
Ian Collins.
Feb 21 '07 #2

P: n/a
On Feb 21, 12:22 pm, Ian Collins <ian-n...@hotmail.comwrote:
Lalatendu Das wrote:
Here in the above example I am confused about, what extra the coder
going to achieve by declaring it as extern in the header file a.h.

So it can be used in another compilation unit.
Actually what u mean by another compilation unit. If my assumption is
not wrong do u mean If I will try to compile another C-program no need
to include this header file or what ?

I don't think so it will work in that case. And in any case if I have
to include this header file to define one variable of structure type
"abc" then why to declare it extern there.
Actually I might have some wrong notion, please explain through
example if u want to?
thanks for ur replay.
--
Ian Collins.

Feb 21 '07 #3

P: n/a
Lalatendu Das wrote:
On Feb 21, 12:22 pm, Ian Collins <ian-n...@hotmail.comwrote:
>Lalatendu Das wrote:
Here in the above example I am confused about, what extra the coder
going to achieve by declaring it as extern in the header file a.h.

So it can be used in another compilation unit.
Actually what u mean by another compilation unit.
There is no `u`.

A compilation unit is the thing you feed to the C compiler: a
source file and its #includes.
If my assumption is
not wrong do u mean If I will try to compile another C-program no need
to include this header file or what ?
A compilation unit [which need not be a program] which references
the stuff in file.c typically does so by #including "a.h". That
makes available the struct definition and the extern.

[Actually, it's more likely that you'll have a header foo.h
corresponding to the file foo.c; ie the name is the same but
the suffix is different. That makes it easier to keep track of
what's related to what. Sometimes more complex schemes are
needed, but this is a good place to start.]
I don't think so it will work in that case. And in any case if I have
to include this header file to define one variable of structure type
"abc" then why to declare it extern there.
Because it's /defined/ somewhere else.

--
Chris "electric hedgehog" Dollin
Meaning precedes definition.

Feb 21 '07 #4

P: n/a
On 21 Feb, 08:36, "Lalatendu Das" <lalat...@gmail.comwrote:
On Feb 21, 12:22 pm, Ian Collins <ian-n...@hotmail.comwrote:>
Lalatendu Das wrote:
Here in the above example I am confused about, what extra the coder
going to achieve by declaring it as extern in the header file a.h.
it would have helped if you'd left the example in...
So it can be used in another compilation unit.

Actually what u mean by another compilation unit.
abbreviations like "u" don't add to the clarity.

"another compilation" unit is basically another C file (actually it's
the C file and its associated includes). A complete program is
composed on one or more compilation units.
If my assumption is
not wrong do u mean If I will try to compile another C-program no need
to include this header file or what ?
you'll need the header file to included in each C file that
uses the shared variable.

I don't think so it will work in that case. And in any case if I have
to include this header file to define one variable of structure type
"abc" then why to declare it extern there.
Actually I might have some wrong notion, please explain through
example if u want to?
thanks for ur replay.
each external object may have many declaration but only one
definition. Essentially you *declare* the type wherever it is
needed (often in an H file) but *define* the storage used in
only one place (usually a C file).
a.h
-------

/* declare struct abc type */
struct abc {
unsigned long a;
unsigned long b;
};

/* declare abc_var to be type struct abc */
extern struct abc abc_var;
file.c
-------

/* pull in declarations */
#include <a.h>

/* define abc_var in one place */
struct abc abc_var;

void f()
{
/* use abc_var */
abc_var.a = 1;
}
/* another compilation unit */
file2.c
-------

/* pull in declarations */
#include <a.h>

/* DON'T re-define abc_var */
void g()
{
/* use abc_var again */
abc_var.a = 0;
}
the program consists of two compilation units that both
access abc_var. How they are joined together ("linked")
is implementaion dependent (see your compiler
documentation).

--
Nick Keighley


Feb 21 '07 #5

P: n/a
Lalatendu Das wrote:
Ian Collins <ian-n...@hotmail.comwrote:
>Lalatendu Das wrote:
>>Here in the above example I am confused about, what extra the coder
going to achieve by declaring it as extern in the header file a.h.

So it can be used in another compilation unit.

Actually what u mean by another compilation unit. If my assumption is
not wrong do u mean If I will try to compile another C-program no need
to include this header file or what ?
I don't see 'u' in the attributions above. I believe he has not
posted here for some time.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

Feb 21 '07 #6

P: n/a

"Lalatendu Das" <la******@gmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
I have seen a header file in which one structure is defined with
extern declaration in a header file and declared one variable of that
structure in one C-file. The code goes as mentioned below . I am
confused with the way it is declared and defined.
So are many folks....
a.h
-------

:
:
:struct abc {
unsigned long a;
unsigned long b;
};
extern struct abc abc;
:
:

file.c
-------

:
#include <a.h>
:
:
struct abc abc;
:
:

Here in the above example I am confused about, what extra the coder
going to achieve by declaring it as extern in the header file a.h.
Its been a while but I think as the above struct is not declared as extern,
this will define a local struct that is internal to "file.c" and separate to
the one in "a.h", but of course I am probably wrong...
Thanks
Das

Feb 22 '07 #7

P: n/a
Chris Dollin wrote:
A compilation unit is the thing you feed to the C compiler: a
source file and its #includes.
N869
5.1 Conceptual models
5.1.1 Translation environment
5.1.1.1 Program structure
A source file together with all the headers and
source files included via the preprocessing directive
#include is known as a preprocessing translation unit.

--
pete
Feb 22 '07 #8

P: n/a
Hi all,
I really appreciate all answers relating to the post particularly
of Mr Nick Keighley.
it is very lucid to understand. And I am sorry for using
abbreviations like "u".
But still I have some doubt. I read about extern that whenever we
need to use the same global
variable across different C-files we need to use extern keyword
and declare it so that it will
refer to same variable where it got defined.
So the above style ( whatever Nick have given as example) can be
re-written like define the
structure in the header-file. i.e
just struct abc {
int a;
int b;
};
Then define a variable of the structure in the required C-
file and in above case in file-1.c
as : struct abc abc_var1; //as a global variable. Then If u
want to use it in another C-file
file-2.c we can sdeclare as: extern struct abc abc_var1; //It
will refer to the abc_var1 of file1.c

I can understand that whatever Nick explained the same thing will
happen (If I am not wrong)
In the preprocessing operation header file will populate the c-
file with the extern declaration.
But I think in file-1.c the extern declaration and the defination
of variable both will apear
after preprocessing header file. i.e

extern struct abc abc_var1
struct abc abc_var1
Is this not a problem.Though not a error but We can avoid this
by define the structure in a header file and then define a variable of
the structure type in a c-file then use extern keyword to refer to the
same variable wherever we want. This solves the whole purpose what we
can achieve out of
doing in the manner Mick replied or i have posted in the querry.

Thanks
Das

Feb 22 '07 #9

P: n/a
Lalatendu Das wrote:
But I think in file-1.c the extern declaration and the defination
of variable both will apear
after preprocessing header file. i.e

extern struct abc abc_var1
struct abc abc_var1
Is this not a problem.
No, this isn't an error, it is normal when a variable is declared
'extern' in a header.

--
Ian Collins.
Feb 22 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.