Variable declaration Vs Definition

M

manish sahu

Is there any diff between Variable declaration and definition??

Whatever i understood is that
in case of auto,static and register storage class
variable declaration is its definition
(bcoz we are allocation memory while declaration( in register storage
cpu registers) for the variables

int main()
{
int i;
printf("%d",i);
}



but in case of external storage class variable declaration and
definition are different . In external storage class declaration
does'nt allocate memory space for variable it is done by variable
definition.
ex:

int main()
{
extern int a; //declaration
printf("%d",a);
return 0;
}
int a=10; // definition

Is it correct ?
PLz Reply
Thanks to all
 
T

Tim Rentsch

Richard Heathfield said:
In


Declaration is a promise that an object (or function) has been
created. Definition is an instruction to create it. Therefore,
definitions effectively count as declarations, but not vice versa.

A possible clarification:

Because of how terminology is used in the ISO Standard,
in a function like this --

int
foo(){
int x;
x = 3;
return x;
}

the line 'int x;' is a "definition" as the Standard uses
the term, even though many or most developers would refer to
this line just as a declaration.
 
D

David Thompson

Is there any diff between Variable declaration and definition??
Yes. And you've got the basic idea right, but not entirely.
Whatever i understood is that
in case of auto,static and register storage class
variable declaration is its definition
(bcoz we are allocation memory while declaration( in register storage
cpu registers) for the variables
For auto and register, yes, the declaration is always a definition,
which allocates space during the execution of the function or block
containing the definition. The standard doesn't specify how, but in
practice it is usually in CPU registers or in the region of memory at
the logical top (usually physical bottom) of the CPU-defined stack.
Long long ago it could be important which variables you declared
register; nowadays the compiler will usually choose better than you
which variables should (or can) be kept in registers, and often only
for part(s) of their lifetimes.
int main()
{
int i;
printf("%d",i);
}



but in case of external storage class variable declaration and
definition are different . In external storage class declaration
does'nt allocate memory space for variable it is done by variable
definition.

For both 'static' and 'extern' there can be a referring (nondefining)
declaration as well as a definition. However, for 'static' these can
only occur within one source file (or to be exact translation unit,
but that difference is minor and you can ignore it for now). For
'extern' these can be in different source files, and usually should.

Aside: what actually matters is internal or external 'linkage'. The
specifiers 'static' and 'extern' in a declaration usually map to
these, but not in some odd cases; I'll leave that out for now.
ex:

int main()
{
extern int a; //declaration
printf("%d",a);
return 0;
}
int a=10; // definition

Is it correct ?

That is a correct way, but:

(1) it is conventional to put declarations of non-local variables only
at file scope (that is, outside of any function). Especially if they
are in #include'd files, as they usually should be to allow easier
maintenance. The only reason you need to put one within a local scope
is if you are doing tricky things with shadowing (hiding) names, which
is usually a bad idea (and certainly not where you should start).

(2) the *benefit* of 'extern' is mostly in using multiple source files
(translation units). A more demonstrative example would be:

// file main.c
extern int zork;
int main ()
{
printf ("%d\n", zork);
return 0;
}

// file zork.c
int zork = 42;
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top