what is the difference between declaration and defination?
6 2367
If you look up the noun "promise" in the Meriam Webster dictionary you find this:
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
That is exactly what a declaration is: a promise that some identifier exists,
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.
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:
Please clarify
Qi
Or to put it more clearly: If it occupies memory then it is a definition. Otherwise, it's a declaration.
Banfa 9,065
Expert Mod 8TB
Or to put it more clearly: If it occupies memory then it is a definition. Otherwise, it's a declaration.
But a structure or class definition creates and occupies no memory or is that too a declaration?
Banfa 9,065
Expert Mod 8TB
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:
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.
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;
But a structure or class definition creates and occupies no memory or is that too a declaration?
Yep, it's a structure declaration, not a definition (for reasons of consistency in
the definition (sic) of the names). C gets quite fuzzy about it sometimes; read
their specification of "tentative" definitions:
6.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.
Isn't C a fun language? ;-)
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: CoolPint |
last post by:
Can anyone clearly explain the difference between constant reference to
pointers and reference to constant pointers?
What is const int * & ?
Is it a constant reference to a pointer to an...
|
by: Akseli Mäki |
last post by:
Hi,
the subject say quite a lot. I have about the following code(4.01
transitional):
<p>blaa blaa blaa
<ul>
<li><a href="foo.html">foo</a></li>
<li><a href="bar.html">bar</a></li>
</ul>
|
by: modemer |
last post by:
If I define the following codes:
void f(const MyClass & in) {cout << "f(const)\n";}
void f(MyClass in) {cout<<"f()\n";}
MyClass myclass;
f(myclass);
Compiler complain that it can't find...
|
by: scl |
last post by:
two class with same name exist in different dynamic linked library:
a.so
class REGION() {
public:
....
~REGION() {}
}
b.so
|
by: Anoj Kumar |
last post by:
Hi All ,
can anyone tell me what is the difference between the following declaration
and how it affects
application performance.
1.
Dim cn As ADODB.Connection
Set cn = New ADODB.Connection
|
by: venky |
last post by:
Hi
main() {
int x; /* it declaration or defination??*/
}
|
by: Johnny E. Jensen |
last post by:
Hello
I have a class that inherence from the ToolStrip control called (AWToolbar),
and i added my own methods to that class.
Then i have a usercontroll (QuickNavigator), and I add the AWToolbar,...
|
by: watkinsdev |
last post by:
Hi,
I have created a mesh class in visual studio 6.0 c++.
I can create a device, render objects and can edit the objects by for
instancnce selecting a cluster of vertices and processing the...
|
by: Gunners |
last post by:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct st_base
{
char name;
int roll_no;
int s1,s2,s3,s4,mks;
float percentage;
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |