"Declaration" vs. "Definition"

K

Kobu

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

Mike Wahler

Kobu said:
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
 
N

Neo

Mike Wahler said:
Yes. Also a definition.

No, its a definition.
Yes. A 'type definition'.

No, Its a declaration.
Yes. Also a definition.

No, its a definition.
A definition (also a declaration).

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.



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

What do U mean by this???
Yes, but a declaration need not be a prototype.


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
 
N

Neo

Mike Wahler said:
Yes. Also a definition.

No, its a definition.
Yes. A 'type definition'.

No, Its a declaration.
Yes. Also a definition.

No, its a definition.
A definition (also a declaration).

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.



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

What do U mean by this???
Yes, but a declaration need not be a prototype.


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
 
J

Jonathan Burd

Neo said:
>
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.
 
A

Andrey Tarasevich

Kobu said:
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.
 
C

Chris Croughton

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
 
S

S.Tobias

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]

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?
 
A

Andrey Tarasevich

S.Tobias said:
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 :)
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top