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

variable declaration and definition!

P: n/a
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.

Oct 5 '07 #1
Share this Question
Share on Google+
15 Replies


P: n/a
vaib wrote:
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/>

Oct 5 '07 #2

P: n/a
On Fri, 05 Oct 2007 00:25:17 -0700, in comp.lang.c , vaib
<va************@gmail.comwrote:
>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
Oct 5 '07 #3

P: n/a
Mark McIntyre <ma**********@spamcop.netwrites:
On Fri, 05 Oct 2007 00:25:17 -0700, in comp.lang.c , vaib
<va************@gmail.comwrote:
>>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).
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 */
Oct 5 '07 #4

P: n/a
Richard said:

<snip>
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.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 5 '07 #5

P: n/a
"Richard" writes:
Mark McIntyre <ma**********@spamcop.netwrites:
>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 */
That's what the people who drool over such arcane things call a "tentative
definition". Here's one early hit on a web search.

http://publib.boulder.ibm.com/infoce...ative_defn.htm
Oct 5 '07 #6

P: n/a
"osmium" <r1********@comcast.netwrites:
"Richard" writes:
>Mark McIntyre <ma**********@spamcop.netwrites:
>>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 */

That's what the people who drool over such arcane things call a "tentative
definition". Here's one early hit on a web search.

http://publib.boulder.ibm.com/infoce...ative_defn.htm
Really, I am amazed and quite prepared to be officially "wrong", but in
20 years or so of programming I have always used "declare x" as opposed
to "define x".
Oct 5 '07 #7

P: n/a
On Fri, 05 Oct 2007 16:18:03 +0000, in comp.lang.c , Richard
Heathfield <rj*@see.sig.invalidwrote:
>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
Oct 5 '07 #8

P: n/a
Mark McIntyre <ma**********@spamcop.netwrites:
On Fri, 05 Oct 2007 16:18:03 +0000, in comp.lang.c , Richard
Heathfield <rj*@see.sig.invalidwrote:
>>A definition reserves storage.
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.
};
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 5 '07 #9

P: n/a
On Oct 5, 8:25 am, vaib <vaibhavpang...@gmail.comwrote:
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

Oct 6 '07 #10

P: n/a
On Fri, 05 Oct 2007 16:29:10 -0700, in comp.lang.c , Keith Thompson
<ks***@mib.orgwrote:
>Mark McIntyre <ma**********@spamcop.netwrites:
>struct foo foostruct1; // declaration and definition

Right, that declares and defines an object.
at file scope, its a tentative definition I think?
>
>void f() {
struct foo foostruct2; // declaration

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
Oct 6 '07 #11

P: n/a
Martin Wells <wa****@eircom.netwrites:
On Oct 5, 8:25 am, vaib <vaibhavpang...@gmail.comwrote:
>hi to all.i'd like to know the actual difference between variable
declaration and definition.
<snip>
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;".

--
Ben.
Oct 7 '07 #12

P: n/a
On Sun, 07 Oct 2007 00:59:00 +0100, Ben Bacarisse wrote:
Martin Wells <wa****@eircom.netwrites:
>On Oct 5, 8:25 am, vaib <vaibhavpang...@gmail.comwrote:
>>hi to all.i'd like to know the actual difference between variable
declaration and definition.

<snip>
>static int j; /* Static, deClaration */

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.
Oct 7 '07 #13

P: n/a
Harald van Dijk <tr*****@gmail.comwrites:
On Sun, 07 Oct 2007 00:59:00 +0100, Ben Bacarisse wrote:
>Martin Wells <wa****@eircom.netwrites:
>>On Oct 5, 8:25 am, vaib <vaibhavpang...@gmail.comwrote:
hi to all.i'd like to know the actual difference between variable
declaration and definition.

<snip>
>>static int j; /* Static, deClaration */

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.
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).

--
Ben.
Oct 7 '07 #14

P: n/a
thank u all . i hope i got the point . thank u so much .

Oct 9 '07 #15

P: n/a
On Oct 7, 6:03 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Harald van D k <true...@gmail.comwrites:
On Sun, 07 Oct 2007 00:59:00 +0100, Ben Bacarisse wrote:
Martin Wells <war...@eircom.netwrites:
>On Oct 5, 8:25 am, vaib <vaibhavpang...@gmail.comwrote:
hi to all.i'd like to know the actual difference between variable
declaration and definition.
<snip>
static int j; /* Static, deClaration */
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.

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).

--
Ben.
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 ??

Oct 9 '07 #16

This discussion thread is closed

Replies have been disabled for this discussion.