extern storage class specifier for variables

R

ravi_shankar

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 :
(e-mail address removed)
 
Z

Zoran Cutura

ravi_shankar said:
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 :
(e-mail address removed)

You post here, you read here.
 
J

Jun Woong

Zoran Cutura said:
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.
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
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[];
 
A

Artie Gold

ravi_shankar said:
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 :
(e-mail address removed)
Post here. Read here.

HTH,
--ag
 
D

Dan Pop

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
 
M

Mark McIntyre

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 :
(e-mail address removed)

post here, read here.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top