variable declaration and definition!

V

vaib

hi to all.i'd like to know the actual difference between variable
declaration and definition.it would be very helpful if anyone out
there wud help me out with this thing.i'm writing here after here
after a long time since people here also helped me out with my lexer.
thanking in anticipation.
 
S

santosh

vaib said:
hi to all.i'd like to know the actual difference between variable
declaration and definition.it would be very helpful if anyone out
there wud help me out with this thing.i'm writing here after here
after a long time since people here also helped me out with my lexer.
thanking in anticipation.

Declarations and definitions apply to both objects, (the C Standard's term
for what others might refer to as variables), and functions. A declaration
specifies the properties of the object or function to the compiler while a
definition causes an actual instance of an object to be created or output
to be generated in the case of functions.

For example:

struct foo {
int x;
struct bar *pbar;
};

is a declaration of an aggregate type called foo. After this declaration the
compiler has enough "knowledge" to properly handle other instances of foo
occurring in the source.

struct foo foo_arr[10];

is a definition. It causes actual storage to be set aside for an array of
ten foo_arr objects, each of which is of type foo.

Obviously a definition has to follow a declaration. You can also combine a
declaration with a definition as in:

struct foo {
int x;
struct bar *pbar;
} foo_x, foo_y;

A function declaration announces the return type, name, and number and type
of parameters the function accepts. This is to enable proper resolution of
forward references and external linkages. A function definition, i.e., the
actual code of the function, also serves as a declaration.

Objects and functions defined in other translation units usually require a
proper declaration before they can be used.

For more details consult a good text on C like _The C Programming Language_
(2nd Edition) by Kernighan & Ritchie or _C: A Reference Manual_ (5th
Edition) by Harbison & Steele.

Also see:
<http://www.c-faq.com/>
<http://www.clc-wiki.net/>
<http://www.eskimo.com/~scs/cclass/>
<http://cprog.tomsweb.net/>
 
M

Mark McIntyre

hi to all.i'd like to know the actual difference between variable
declaration and definition.

In C, a declaration of X says "there is an object called X, and this
is its type". It doesn't create any storage for the object. You can
have many declarations of an object in a programme (as long as they're
all the same).

A definition however creates the actual object. There can be only one
of it.

You can think of declarations like entries in a Table of Contents of a
book. It tells you there will be a volume 5 chapter 3. You can have
multiple tables of contents, say in different volumes, but only one
actual chapter, the definition.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
R

Richard

Mark McIntyre said:
In C, a declaration of X says "there is an object called X, and this
is its type". It doesn't create any storage for the object. You can
have many declarations of an object in a programme (as long as they're
all the same).

This is news to me. For years I laboured under the understanding that
the declaration created storage too.

int x; /* declare variable x to be of type int */
 
R

Richard Heathfield

Richard said:

For years I laboured under the understanding that
the declaration created storage too.

That's not an understanding; it's a misunderstanding. A definition reserves
storage. All definitions are declarations, but not all declarations are
definitions.
 
M

Mark McIntyre

A definition reserves storage.

Unless its the definition of a struct or union?

struct foo
{
int bar;
}; // definition ? or declaration of type?

struct foo foostruct1; // declaration and definition

void f() {
struct foo foostruct2; // declaration

};


--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
K

Keith Thompson

Specifically, an object definition reserves storage.
Unless its the definition of a struct or union?

struct foo
{
int bar;
}; // definition ? or declaration of type?

The standard calls that a declaration, not a definition (if I'm
reading the grammar correctly). (I probably would have called it a
definition, since it creates the type, but it's not a big deal; either
way, it doesn't reserver any storage.)
struct foo foostruct1; // declaration and definition

Right, that declares and defines an object.
void f() {
struct foo foostruct2; // declaration

That also declares and defines an object (that happens to have
automatic storage duration).

If you had written:

extern struct foo whatever;

that would be a declaration, not a definition.
 
M

Martin Wells

hi to all.i'd like to know the actual difference between variable
declaration and definition.it would be very helpful if anyone out
there wud help me out with this thing.i'm writing here after here
after a long time since people here also helped me out with my lexer.
thanking in anticipation.


int i; /* Extern, deFinition */

int i = 7; /* Extern, deFinition */

extern int i; /* Extern, deClaration */

extern int i = 7; /* Extern, deFinition */

static int j; /* Static, deClaration */

static int j = 7; /* Static, deFinition */


Quite funky indeed.

Martin
 
M

Mark McIntyre

Right, that declares and defines an object.

at file scope, its a tentative definition I think?
That also declares and defines an object (that happens to have
automatic storage duration).

oops, you're right - needs extern in front.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
B

Ben Bacarisse

static int j; /* Static, deClaration */

Surely this one is a definition as well? It is a tentative
definition, but unless anything perturbs it, it acts as if is a real
one (with an initialiser of 0). Note that your other examples of
definitions that lacked initialisations were also tentative.
Quite funky indeed.

Slightly less so if the above is as much a definition as "int i;".
 
B

Ben Bacarisse

Harald van Dijk said:
A tentative definition is not itself a definition.

Thanks. I thought that a tentative definition could be thought of as
a kind of definition but I can see that would be confusing and is
technically wrong.

"int i;" is also a tentative definition. My point was that Martin
Wells seemed to be suggesting a difference that does not exist between
"int i;" and "static int j;" (at least for file scope identifiers).
 
?

=?iso-2022-kr?q?=1B=24=29CHarald_van_D=0E=29=26=0F

Surely this one is a definition as well?
No.

It is a tentative definition,
Yes.

but unless anything perturbs it, it acts as if is a real one (with an
initialiser of 0).

A tentative definition is not itself a definition.

static int i[];

This is a tentative definition, and the implicit initialiser would give
it a length of 1, but...

static int i[2];

....might appear later. The actual definition (if no explicit definition
is present) is generated once, regardless of how many tentative
definitions you have, and the actual definition's type is not necessarily
the type of any tentative definition, but rather, the composite type of
all tentative definitions.
 
V

vaib

Thanks. I thought that a tentative definition could be thought of as
a kind of definition but I can see that would be confusing and is
technically wrong.

"int i;" is also a tentative definition. My point was that Martin
Wells seemed to be suggesting a difference that does not exist between
"int i;" and "static int j;" (at least for file scope identifiers).

for once it seemed all clear but things have started to get jumbled up
again.now wats exactly a tentative declaration ?? and how is int i =
3 ; an external declaration ??
 
B

Ben Bacarisse

Best not to quote sigs (the lines including and after the "-- ").
for once it seemed all clear but things have started to get jumbled up
again.now wats exactly a tentative declaration ?? and how is int i =
3 ; an external declaration ??

First, you are confusing 'declaration' and 'definition'. I was
talking only about definitions and tentative definitions.

Second, 'int i = 2;' is an external definition of x because the
standard says it is. A declaration of a file-scope object with an
initializer constitutes an external definition (6.9.2 p1).

If a file-scope object is declared without an initializer and without
a storage class (or with storage class static) it is called a
tentative definition. E.g. both

int i;
static int j;

are tentative definitions. Why is this? The reason is to allow these
to co-exist with at most one declaration that *does* include an
initialiser. The standard wants to allow:

int x;
...
int x = 42;

(these can be in either order) and also to allow

int y;

on its own[1] with the meaning that y is defined and initialized to 0.
To do this, they introduce the idea of tentative definition. To have
simply said that 'int x;' is the same as 'int x = 0;' would have made
the example above illegal (since x would be defined twice).

[1] "on its own" means in a translation unit that may have multiple
tentative definitions of the same identifier but which has no external
definitions for it.
 
V

vaib

Best not to quote sigs (the lines including and after the "-- ").
for once it seemed all clear but things have started to get jumbled up
again.now wats exactly a tentative declaration ?? and how is int i =
3 ; an external declaration ??

First, you are confusing 'declaration' and 'definition'. I was
talking only about definitions and tentative definitions.

Second, 'int i = 2;' is an external definition of x because the
standard says it is. A declaration of a file-scope object with an
initializer constitutes an external definition (6.9.2 p1).

If a file-scope object is declared without an initializer and without
a storage class (or with storage class static) it is called a
tentative definition. E.g. both

int i;
static int j;

are tentative definitions. Why is this? The reason is to allow these
to co-exist with at most one declaration that *does* include an
initialiser. The standard wants to allow:

int x;
...
int x = 42;

(these can be in either order) and also to allow

int y;

on its own[1] with the meaning that y is defined and initialized to 0.
To do this, they introduce the idea of tentative definition. To have
simply said that 'int x;' is the same as 'int x = 0;' would have made
the example above illegal (since x would be defined twice).

[1] "on its own" means in a translation unit that may have multiple
tentative definitions of the same identifier but which has no external
definitions for it.

thank u so much.i did a little research and got the point.
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top