what is the difference between declaration and defination?
what is the difference between declaration and defination?
Collapse
X
-
Tags: None
-
If you look up the noun "promise" in the Meriam Webster dictionary you find this:
That is exactly what a declaration is: a promise that some identifier exists,1 a: a declaration that one will do or refrain from doing something specified b: a legally binding declaration that gives the person to whom it is made a right to expect or to claim the performance or forbearance of a specified act
2: reason to expect something <little promise of relief>; especially : ground for expectation of success, improvement, or excellence <shows considerable promise>
3: something that is promised
somewhere and with a particular type and if there is more to that identifier
(as in a function) that is promised (i.e. declared) as well.
A definition is nothing more than a fulfillment of that promise.
kind regards,
Jos -
Or to put it more clearly: If it occupies memory then it is a definition. Otherwise, it's a declaration.Comment
-
weaknessforcats -
int a;
is it not occupying memory? what does it do if it doesn't.
does it atleast reserve required portion of memory - as using it.?
if so, then how the above statement can become definition - by this e.g:
[code=c]
int a =10;
[/code]
Please clarify
Qi
Originally posted by weaknessforcatsOr to put it more clearly: If it occupies memory then it is a definition. Otherwise, it's a declaration.Comment
-
Assuming that the int a; is not inside a class or structure but rather is global or inside a function then this does occupy memory and is a definition.Originally posted by questionitint a;
is it not occupying memory? what does it do if it doesn't.
does it atleast reserve required portion of memory - as using it.?
if so, then how the above statement can become definition - by this e.g:
[code=c]
int a =10;
[/code]
Adding the = 10 does not make it a definition, it was always a definition all it does is add an initialisation to the definition.
A declaration would look something like
extern int a;Comment
-
Yep, it's a structure declaration, not a definition (for reasons of consistency inOriginally posted by BanfaBut a structure or class definition creates and occupies no memory or is that too a declaration?
the definition (sic) of the names). C gets quite fuzzy about it sometimes; read
their specification of "tentative" definitions:
Isn't C a fun language? ;-)Originally posted by C Standard6.9.2 External object definitions
Semantics
[#1] If the declaration of an identifier for an object has
file scope and an initializer, the declaration is an
external definition for the identifier.
[#2] A declaration of an identifier for an object that has
file scope without an initializer, and without a storage-
class specifier or with the storage-class specifier static,
constitutes a tentative definition. If a translation unit
contains one or more tentative definitions for an
identifier, and the translation unit contains no external
definition for that identifier, then the behavior is exactly
as if the translation unit contains a file scope declaration
of that identifier, with the composite type as of the end of
the translation unit, with an initializer equal to 0.
[#3] If the declaration of an identifier for an object is a
tentative definition and has internal linkage, the declared
type shall not be an incomplete type.
[#4] EXAMPLE 1
int i1 = 1; // definition, external linkage
static int i2 = 2; // definition, internal linkage
extern int i3 = 3; // definition, external linkage
int i4; // tentative definition, external linkage
static int i5; // tentative definition, internal linkage
int i1; // valid tentative definition, refers to previous
int i2; // 6.2.2 renders undefined, linkage disagreement
int i3; // valid tentative definition, refers to previous
int i4; // valid tentative definition, refers to previous
int i5; // 6.2.2 renders undefined, linkage disagreement
extern int i1; // refers to previous, whose linkage is external
extern int i2; // refers to previous, whose linkage is internal
extern int i3; // refers to previous, whose linkage is external
extern int i4; // refers to previous, whose linkage is external
extern int i5; // refers to previous, whose linkage is internal
[#5] EXAMPLE 2 If at the end of the translation unit
containing
int i[];
the array i still has incomplete type, the implicit
initializer causes it to have one element, which is set to
zero on program startup.
kind regards,
JosComment
Comment