473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

confused about extern use

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
9 2667
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
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
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
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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
2
by: ik | last post by:
Hello all, Under C++ when 'extern' is used with out the string literal "C" does it act the same as a 'static' ? When is it appropriate using 'extern' ? Thanks ~Ik
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
5
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining...
17
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
5
by: Christian Christmann | last post by:
Hi, I've tree questions on the storage class specifier "extern": 1) Code example: int main( void ) { int b = -2; // my line 3 if ( a ) {
6
by: pookiebearbottom | last post by:
Let's say I have headers Sal.h with this class class Sal { public: int doit() { return 1;} }; now I know that the compilier can choose NOT to inline this function. So if I include this in...
3
by: arnuld | last post by:
1) int i = 3; 2.) int* pi; 3.) int* pi2 = &i 4.) char* pc; 5.) char c; 6) char c2 = 'a' 7.) char* pc2 = &c2 8.) char* ps = "Stroustroup"; 9.) extern double d;
6
by: James H. Newman | last post by:
I am playing with some code that has been automatically generated from ASN.1 data specification found in RFC 3280. One of the structures generated reads as follows: typedef struct TBSCertList {...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.