473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 20821

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
16
by: cppaddict | last post by:
I thought that: char x; made x into a pointer-to-char. But how come the following code won't compile: int main() { char pbuf;
111
by: JKop | last post by:
Okay here we go, I feel it's about time people conversed about the bullshit aspects of C++ (including the bullshit stuff brought forward from C). I'll begin with a few of my own grievances: 1)...
6
by: kelvSYC | last post by:
This little bit of seeminly innocent code seems to give me these two errors, all on the line that declares check(). Is there some part of C++ that I'm missing out on? class Condition { public:...
5
by: Maett | last post by:
Hi. When I try to compile this piece of Code with VC.NET 2003 // code start #include <vector> template< class T > struct Marker { public:
13
by: baumann.Pan | last post by:
when define char *p = " can not modify"; p ='b' ;is not allowed, but if you declare p as char p = "can modify"; p = 'b'; is ok? why?
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
2
by: hankypan1 | last post by:
Hi All, I need a tree data structure for my application. It is the non - cyclic simple tree where i can have any number of children node and each child can recursively become a sub tree like a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.