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

extern storage class specifier for variables

P: n/a

hi all.
what's the advantage of prefixing a varible with "extern" storage class
specifier other than its default initializion .
you can mail to me :
ra****************@rediffmail.com

--
Posted via http://dbforums.com
Nov 13 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
ravi_shankar <me*********@dbforums.com> wrote:

hi all.
what's the advantage of prefixing a varible with "extern" storage class
specifier other than its default initializion .
The extern specifier doesn't make the variabel initialize. All variables
with file scope (that is all that are not within block scope) are
initialized to 0 if there is no explicit initialization.

Extern tells the compiler that you are not defining the variable here,
but rather declaring that this variable exists somewhere else in another
translation unit and you are referring to that.
you can mail to me :
ra****************@rediffmail.com


You post here, you read here.

--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 13 '05 #2

P: n/a

"Zoran Cutura" <zo**********@daimlerchrysler.com> wrote in message news:be**********@news.sns-felb.debis.de...
ravi_shankar <me*********@dbforums.com> wrote: [...]
Extern tells the compiler that you are not defining the variable here,
but rather declaring that this variable exists somewhere else in another
translation unit and you are referring to that.


Provided that the (file scope) declaration has no initializer; but
it's very bad to provide both of "extern" and an initializer to a
file scope declaration.
--
Jun, Woong (my******@hanmail.net)
Dept. of Physics, Univ. of Seoul

Nov 13 '05 #3

P: n/a
In 'comp.lang.c', ravi_shankar <me*********@dbforums.com> wrote:
what's the advantage of prefixing a varible with "extern" storage class
specifier other than its default initializion .


It's ignored when you define the variable, but it allows a separated
declaration of this variable when you want it to be shared in different
modules.

1 - Are you sure you need a global scope variable?
2 - If so, define it in a source file (say data.c)
3 - Declare it in a header (say data.h>
4 - For consitency, include this header in th definition file (data.c) and
all the files that need a reference to this variable.
5 - For readability and maintenance, I suggest that the global scope
variables are prefixed with G_

/* data.h */
#ifndef H_DATA
#define H_DATA
extern int G_myglobal;
extern int G_myarray[123];
#endif

/* data.c */
#include "data.h"

int G_myglobal;
int G_myarray[];

--
-ed- em**********@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 13 '05 #4

P: n/a
ravi_shankar wrote:
hi all.
what's the advantage of prefixing a varible with "extern" storage class
specifier other than its default initializion .
Prefixing a variable declaration with `extern' says: "This variable is
defined in another compilation unit." Nothing more, nothing less.
you can mail to me :
ra****************@rediffmail.com

Post here. Read here.

HTH,
--ag
--
Artie Gold -- Austin, Texas

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #5

P: n/a
In <30****************@dbforums.com> ravi_shankar <me*********@dbforums.com> writes:

what's the advantage of prefixing a varible with "extern" storage class
specifier other than its default initializion .


Without extern, the declaration becomes a definition (in the absence
of another definition for that identifier, in that file).

With extern, the declaration remains a declaration.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #6

P: n/a
On Fri, 04 Jul 2003 11:43:39 +0000, in comp.lang.c , ravi_shankar
<me*********@dbforums.com> wrote:

hi all.
what's the advantage of prefixing a varible with "extern" storage class
specifier other than its default initializion .
sounds like a homework question...
Anyway, an extern variable is one that is defined elsewhere.
you can mail to me :
ra****************@rediffmail.com


post here, read here.

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.