confused about extern use

L

Lalatendu Das

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
 
I

Ian Collins

Lalatendu said:
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.
 
L

Lalatendu Das

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.
 
C

Chris Dollin

Lalatendu said:
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.
 
N

Nick Keighley

it would have helped if you'd left the example in...
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).
 
C

CBFalconer

Lalatendu said:
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
 
D

David Wade

Lalatendu Das said:
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...
 
P

pete

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.
 
L

Lalatendu Das

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
 
I

Ian Collins

Lalatendu said:
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.
 

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,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top