472,111 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,111 software developers and data experts.

"Declaration" vs. "Definition"

My question is about the use and meaning of the terms "declaration" and
"definition" as it pertains to the C language.

I've read sources that mix the two up when talking about such things as
setting aside storage for an object, defining/declaring a struct, parts
of a function, referencing an external variable in another module.

sourcefile1.c
==============
extern long globalfoo; /* declaration? */
int main()
{
float b; /* declaration ? */

struct STRUCTFOO
{
int foobar;
}; /* definition ? */

struct STRUCTFOO foostruct; /* declaration? */

return 0;
}

sourcefile2.c
=============

long globalfoo; /* defining instance?? Steve Summit tutorial calls it
this */
--

The simple rule I use is that, if it sets aside storage it's a
declaration, otherwise it's a definition.
Even with this rule, functions seem to be different. We call
prototypes declarations, and headers/bodies definitions (even though
it's the header/body that sets aside code space).

Nov 14 '05 #1
10 20659

"Kobu" <ko********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
My question is about the use and meaning of the terms "declaration" and
"definition" as it pertains to the C language.

I've read sources that mix the two up when talking about such things as
setting aside storage for an object, defining/declaring a struct, parts
of a function, referencing an external variable in another module.

sourcefile1.c
==============
extern long globalfoo; /* declaration? */
Yes.


int main()
{
float b; /* declaration ? */
Yes. Also a definition.

struct STRUCTFOO
{
int foobar;
}; /* definition ? */
Yes. A 'type definition'.


struct STRUCTFOO foostruct; /* declaration? */
Yes. Also a definition.

return 0;
}

sourcefile2.c
=============

long globalfoo; /* defining instance?? Steve Summit tutorial calls it
this */
A definition (also a declaration).


--

The simple rule I use is that, if it sets aside storage it's a
declaration, otherwise it's a definition.
Yes. Except for a struct definition, which is a 'type definition',
and does not allot storage (but a struct definition can be combined
with a definition of an object of that struct type:

struct x
{
int a;
} s;

defines type 'struct x' as well as an object of that type.

Even with this rule, functions seem to be different.
Yes. A function definition is not the same thing as
a function definition.

We call
prototypes declarations,
Yes, but a declaration need not be a prototype.
and headers/bodies definitions (even though
it's the header/body that sets aside code space).


It's the { and } and the code between them, combined
with the 'function header' (name and argument list),
which comprise a function definition.

-Mike
Nov 14 '05 #2
Neo

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:1V****************@newsread1.news.pas.earthli nk.net...

"Kobu" <ko********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
My question is about the use and meaning of the terms "declaration" and
"definition" as it pertains to the C language.

I've read sources that mix the two up when talking about such things as
setting aside storage for an object, defining/declaring a struct, parts
of a function, referencing an external variable in another module.

sourcefile1.c
==============
extern long globalfoo; /* declaration? */
Yes.


int main()
{
float b; /* declaration ? */


Yes. Also a definition.


No, its a definition.

struct STRUCTFOO
{
int foobar;
}; /* definition ? */
Yes. A 'type definition'.


No, Its a declaration.


struct STRUCTFOO foostruct; /* declaration? */
Yes. Also a definition.


No, its a definition.

return 0;
}

sourcefile2.c
=============

long globalfoo; /* defining instance?? Steve Summit tutorial calls it
this */
A definition (also a declaration).


--

The simple rule I use is that, if it sets aside storage it's a
declaration, otherwise it's a definition.


No, The Opposite.
"Definition" refer to the place where the variable is created or assigned
storage; "Declaration" refers to places where the nature of the variable is
stated but no storage is allocated." -K&R II Page 33

So, We can say Definition is also a Declaration. Am I right?

Yes. Except for a struct definition, which is a 'type definition',
and does not allot storage (but a struct definition can be combined
with a definition of an object of that struct type:

struct x
{
int a;
} s;

defines type 'struct x' as well as an object of that type.

Even with this rule, functions seem to be different.
Yes. A function definition is not the same thing as
a function definition.


What do U mean by this???

We call
prototypes declarations,


Yes, but a declaration need not be a prototype.
and headers/bodies definitions (even though
it's the header/body that sets aside code space).


It's the { and } and the code between them, combined
with the 'function header' (name and argument list),
which comprise a function definition.

-Mike


-Neo
Nov 14 '05 #3
Neo

"Mike Wahler" <mk******@mkwahler.net> wrote in message
news:1V****************@newsread1.news.pas.earthli nk.net...

"Kobu" <ko********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
My question is about the use and meaning of the terms "declaration" and
"definition" as it pertains to the C language.

I've read sources that mix the two up when talking about such things as
setting aside storage for an object, defining/declaring a struct, parts
of a function, referencing an external variable in another module.

sourcefile1.c
==============
extern long globalfoo; /* declaration? */
Yes.


int main()
{
float b; /* declaration ? */


Yes. Also a definition.


No, its a definition.

struct STRUCTFOO
{
int foobar;
}; /* definition ? */
Yes. A 'type definition'.


No, Its a declaration.


struct STRUCTFOO foostruct; /* declaration? */
Yes. Also a definition.


No, its a definition.

return 0;
}

sourcefile2.c
=============

long globalfoo; /* defining instance?? Steve Summit tutorial calls it
this */
A definition (also a declaration).


--

The simple rule I use is that, if it sets aside storage it's a
declaration, otherwise it's a definition.


No, The Opposite.
"Definition" refer to the place where the variable is created or assigned
storage; "Declaration" refers to places where the nature of the variable is
stated but no storage is allocated." -K&R II Page 33

So, We can say Definition is also a Declaration. Am I right?

Yes. Except for a struct definition, which is a 'type definition',
and does not allot storage (but a struct definition can be combined
with a definition of an object of that struct type:

struct x
{
int a;
} s;

defines type 'struct x' as well as an object of that type.

Even with this rule, functions seem to be different.
Yes. A function definition is not the same thing as
a function definition.


What do U mean by this???

We call
prototypes declarations,


Yes, but a declaration need not be a prototype.
and headers/bodies definitions (even though
it's the header/body that sets aside code space).


It's the { and } and the code between them, combined
with the 'function header' (name and argument list),
which comprise a function definition.

-Mike


-Neo

Nov 14 '05 #4
Neo wrote:
"Mike Wahler" <mk******@mkwahler.net> wrote in message

Yes. A function definition is not the same thing as
a function definition.


What do U mean by this???


He meant "A function declaration is not the same thing as
a function definition." Give him a break. Typos happen. ;)

Regards,
Jonathan.

--
"I'm learning to program because then I can write
programs to do my homework faster." - Andy Anfilofieff
Nov 14 '05 #5
Neo wrote:
Mike Wahler wrote:
A function definition is not the same thing as
a function definition.
What do U mean by this???

cat main.c

// function declaration
int main(int, char**);

// function definition
int main(int argc, char* argv[]) {
return 0;
}
Nov 14 '05 #6
Kobu wrote:
My question is about the use and meaning of the terms "declaration" and
"definition" as it pertains to the C language.

I've read sources that mix the two up when talking about such things as
setting aside storage for an object, defining/declaring a struct, parts
of a function, referencing an external variable in another module.
Major source of confusion is that these terms are not really mutually
exclusive, but often used as such.

Every "definition" in C is at the same time a "declaration", i.e. when
you see that a "definition" is being referred to as a "declaration" this
is most likely not an error. Moreover, in contexts where the specifics
of "definition" are not important, the term "declaration" is normally
used all the time.

In other contexts, where it is important to differentiate between
"declaration" and "definition" these terms might be used in "mutually
exclusive" sense. However, this should either be explicitly stated in
advance or should be sufficiently clear from the context.
sourcefile1.c
==============
extern long globalfoo; /* declaration? */
Yes. Declaration, not definition.

int main()
{
float b; /* declaration ? */
Definition. I.e. it is a declaration, which also happens to be a definition.
struct STRUCTFOO
{
int foobar;
}; /* definition ? */
Definition of 'struct STRUCTFOO'. I.e. it is declaration of the type and
the tag, which also happens to be a definition of the type. It also
contains declaration of member 'foobar'.
struct STRUCTFOO foostruct; /* declaration? */
Definition of an object 'foostruct'. I.e. it is a declaration of this
object, which also happens to be a definition.
return 0;
}

sourcefile2.c
=============

long globalfoo; /* defining instance?? Steve Summit tutorial calls it
this */
Declaration, which is also a definition. This one, BTW, is what is
called a "tentative definition" in C.
The simple rule I use is that, if it sets aside storage it's a
declaration, otherwise it's a definition.
This rule is applicable to objects (and maybe functions) only. In C
language terms "declaration" and "definition" are also applicable to
types, functions and other entities.
Even with this rule, functions seem to be different.
Not only functions.
We call
prototypes declarations,
Function declarations are not necessarily prototypes. For example. this

void foo();

is a function declaration, but it is not a prototype.
and headers/bodies definitions (even though
it's the header/body that sets aside code space).


--
Best regards,
Andrey Tarasevich
Nov 14 '05 #7
"Neo" <ti***************@yahoo.com> wrote in message
news:35*************@individual.net...

"Mike Wahler" <mk******@mkwahler.net> wrote in message
float b; /* declaration ? */


Yes. Also a definition.


No, its a definition.


It's both. See Andrey's post.
Even with this rule, functions seem to be different.


Yes. A function definition is not the same thing as
a function definition.


What do U mean by this???


I mean I made a typo. :-)

-Mike
Nov 14 '05 #8
On Fri, 21 Jan 2005 10:20:02 -0800, Andrey Tarasevich
<an**************@hotmail.com> wrote:
Every "definition" in C is at the same time a "declaration", i.e. when
you see that a "definition" is being referred to as a "declaration" this
is most likely not an error. Moreover, in contexts where the specifics
of "definition" are not important, the term "declaration" is normally
used all the time.


Snip examples. No wonder I'm always getting the terms mixed up! Plus
in (Btitish) English usage I would normally think of them the other way
round (I think; at least they aren't consistently one or the other).

Chris C
Nov 14 '05 #9
Andrey Tarasevich <an**************@hotmail.com> wrote:
Kobu wrote:
My question is about the use and meaning of the terms "declaration" and
"definition" as it pertains to the C language.

I've read sources that mix the two up when talking about such things as
setting aside storage for an object, defining/declaring a struct, parts
of a function, referencing an external variable in another module.
All definitions are also declarations.
Some declarations are not definitions.

/Declaration/ and /definition/ are _terms_ defined by the Standard.
Declaration is a language construct defined by its grammar (6.7).

(6.7 Declarations)
# 5 A declaration specifies the interpretation and attributes of a set of
# identifiers. A /definition/ of an identifier is a declaration for that
# identifier that:
# - for an object, causes storage to be reserved for that object;
# - for a function, includes the function body;98)
# - for an enumeration constant or typedef name, is the (only) declaration
# of the identifier.
[excellent explanation snipped]

struct STRUCTFOO
{
int foobar;
}; /* definition ? */
Definition of 'struct STRUCTFOO'. I.e. it is declaration of the type and
the tag, which also happens to be a definition of the type. It also
contains declaration of member 'foobar'.


This does not actually fall into any category for a definition above,
therefore it is a declaration. This is a declaration of type
struct STRUCTFOO, declares identifier STRUCTFOO to be the tag of that
type, and also defines structure content (IMO the word "defines" is *not*
used in the sense of the term "definition"). See wording of 6.7.2,
esp. 6.7.2.3.

It is not a definition of a type. Type definitions are introduced
with the keyword `typedef'. (Well, I too think of it as a "defining"
declaration, as it competes the structure type.)

Frankly, I don't see much difference in weight between the two
(ie. struct declarations and type definitions).
Anyone knows what the "real" difference is between "defines an identifier"
and "declares an identifier" (cf. 6.7.7p3), apart of course the strict
terms definition?

--
Stan Tobias
mailx `echo si***@FamOuS.BedBuG.pAlS.INVALID | sed s/[[:upper:]]//g`
Nov 14 '05 #10
S.Tobias wrote:
> struct STRUCTFOO
> {
> int foobar;
> }; /* definition ? */

Definition of 'struct STRUCTFOO'. I.e. it is declaration of the type and
the tag, which also happens to be a definition of the type. It also
contains declaration of member 'foobar'.


This does not actually fall into any category for a definition above,
therefore it is a declaration. This is a declaration of type
struct STRUCTFOO, declares identifier STRUCTFOO to be the tag of that
type, and also defines structure content (IMO the word "defines" is *not*
used in the sense of the term "definition"). See wording of 6.7.2,
esp. 6.7.2.3.

It is not a definition of a type. Type definitions are introduced
with the keyword `typedef'. (Well, I too think of it as a "defining"
declaration, as it competes the structure type.)
...


Yes, you are right. Thanks for the correction. I was thinking in C++, so
to say :)

--
Best regards,
Andrey Tarasevich
Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by cppaddict | last post: by
111 posts views Thread by JKop | last post: by
8 posts views Thread by TTroy | last post: by
14 posts views Thread by Jess | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.