473,761 Members | 3,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2692
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.co mwrote:
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.co mwrote:
>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.co mwrote:>
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.co mwrote:
>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.securityfoc us.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.goog legroups.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
4405
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 reffers in all objects of this class to the same data Or in other word by using the static keyword all objects of one class can share data. (This is what I want)
2
2444
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
3860
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 storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
5
16607
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 extern storage class: /****************************************************************** SOURCE FILE ONE *******************************************************************/ extern int i; /* Reference to i, defined below */
17
4933
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
2859
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
2914
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 two different libraries, they both choose to
3
4593
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
3683
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 { Version_t *version /* OPTIONAL */; AlgorithmIdentifier_t signature; Name_t issuer; Time_t thisUpdate; struct Time *nextUpdate /* OPTIONAL */;
0
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10111
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9948
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.