473,419 Members | 4,195 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,419 software developers and data experts.

variable declaration and definition!

hi to all.i'd like to know the actual difference between variable
declaration and definition.it would be very helpful if anyone out
there wud help me out with this thing.i'm writing here after here
after a long time since people here also helped me out with my lexer.
thanking in anticipation.

Oct 5 '07 #1
15 14879
vaib wrote:
hi to all.i'd like to know the actual difference between variable
declaration and definition.it would be very helpful if anyone out
there wud help me out with this thing.i'm writing here after here
after a long time since people here also helped me out with my lexer.
thanking in anticipation.
Declarations and definitions apply to both objects, (the C Standard's term
for what others might refer to as variables), and functions. A declaration
specifies the properties of the object or function to the compiler while a
definition causes an actual instance of an object to be created or output
to be generated in the case of functions.

For example:

struct foo {
int x;
struct bar *pbar;
};

is a declaration of an aggregate type called foo. After this declaration the
compiler has enough "knowledge" to properly handle other instances of foo
occurring in the source.

struct foo foo_arr[10];

is a definition. It causes actual storage to be set aside for an array of
ten foo_arr objects, each of which is of type foo.

Obviously a definition has to follow a declaration. You can also combine a
declaration with a definition as in:

struct foo {
int x;
struct bar *pbar;
} foo_x, foo_y;

A function declaration announces the return type, name, and number and type
of parameters the function accepts. This is to enable proper resolution of
forward references and external linkages. A function definition, i.e., the
actual code of the function, also serves as a declaration.

Objects and functions defined in other translation units usually require a
proper declaration before they can be used.

For more details consult a good text on C like _The C Programming Language_
(2nd Edition) by Kernighan & Ritchie or _C: A Reference Manual_ (5th
Edition) by Harbison & Steele.

Also see:
<http://www.c-faq.com/>
<http://www.clc-wiki.net/>
<http://www.eskimo.com/~scs/cclass/>
<http://cprog.tomsweb.net/>

Oct 5 '07 #2
On Fri, 05 Oct 2007 00:25:17 -0700, in comp.lang.c , vaib
<va************@gmail.comwrote:
>hi to all.i'd like to know the actual difference between variable
declaration and definition.
In C, a declaration of X says "there is an object called X, and this
is its type". It doesn't create any storage for the object. You can
have many declarations of an object in a programme (as long as they're
all the same).

A definition however creates the actual object. There can be only one
of it.

You can think of declarations like entries in a Table of Contents of a
book. It tells you there will be a volume 5 chapter 3. You can have
multiple tables of contents, say in different volumes, but only one
actual chapter, the definition.
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Oct 5 '07 #3
Mark McIntyre <ma**********@spamcop.netwrites:
On Fri, 05 Oct 2007 00:25:17 -0700, in comp.lang.c , vaib
<va************@gmail.comwrote:
>>hi to all.i'd like to know the actual difference between variable
declaration and definition.

In C, a declaration of X says "there is an object called X, and this
is its type". It doesn't create any storage for the object. You can
have many declarations of an object in a programme (as long as they're
all the same).
This is news to me. For years I laboured under the understanding that
the declaration created storage too.

int x; /* declare variable x to be of type int */
Oct 5 '07 #4
Richard said:

<snip>
For years I laboured under the understanding that
the declaration created storage too.
That's not an understanding; it's a misunderstanding. A definition reserves
storage. All definitions are declarations, but not all declarations are
definitions.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Oct 5 '07 #5
"Richard" writes:
Mark McIntyre <ma**********@spamcop.netwrites:
>In C, a declaration of X says "there is an object called X, and this
is its type". It doesn't create any storage for the object. You can
have many declarations of an object in a programme (as long as they're
all the same).

This is news to me. For years I laboured under the understanding that
the declaration created storage too.

int x; /* declare variable x to be of type int */
That's what the people who drool over such arcane things call a "tentative
definition". Here's one early hit on a web search.

http://publib.boulder.ibm.com/infoce...ative_defn.htm
Oct 5 '07 #6
"osmium" <r1********@comcast.netwrites:
"Richard" writes:
>Mark McIntyre <ma**********@spamcop.netwrites:
>>In C, a declaration of X says "there is an object called X, and this
is its type". It doesn't create any storage for the object. You can
have many declarations of an object in a programme (as long as they're
all the same).

This is news to me. For years I laboured under the understanding that
the declaration created storage too.

int x; /* declare variable x to be of type int */

That's what the people who drool over such arcane things call a "tentative
definition". Here's one early hit on a web search.

http://publib.boulder.ibm.com/infoce...ative_defn.htm
Really, I am amazed and quite prepared to be officially "wrong", but in
20 years or so of programming I have always used "declare x" as opposed
to "define x".
Oct 5 '07 #7
On Fri, 05 Oct 2007 16:18:03 +0000, in comp.lang.c , Richard
Heathfield <rj*@see.sig.invalidwrote:
>A definition reserves storage.
Unless its the definition of a struct or union?

struct foo
{
int bar;
}; // definition ? or declaration of type?

struct foo foostruct1; // declaration and definition

void f() {
struct foo foostruct2; // declaration

};
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Oct 5 '07 #8
Mark McIntyre <ma**********@spamcop.netwrites:
On Fri, 05 Oct 2007 16:18:03 +0000, in comp.lang.c , Richard
Heathfield <rj*@see.sig.invalidwrote:
>>A definition reserves storage.
Specifically, an object definition reserves storage.
Unless its the definition of a struct or union?

struct foo
{
int bar;
}; // definition ? or declaration of type?
The standard calls that a declaration, not a definition (if I'm
reading the grammar correctly). (I probably would have called it a
definition, since it creates the type, but it's not a big deal; either
way, it doesn't reserver any storage.)
struct foo foostruct1; // declaration and definition
Right, that declares and defines an object.
void f() {
struct foo foostruct2; // declaration
That also declares and defines an object (that happens to have
automatic storage duration).

If you had written:

extern struct foo whatever;

that would be a declaration, not a definition.
};
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 5 '07 #9
On Oct 5, 8:25 am, vaib <vaibhavpang...@gmail.comwrote:
hi to all.i'd like to know the actual difference between variable
declaration and definition.it would be very helpful if anyone out
there wud help me out with this thing.i'm writing here after here
after a long time since people here also helped me out with my lexer.
thanking in anticipation.

int i; /* Extern, deFinition */

int i = 7; /* Extern, deFinition */

extern int i; /* Extern, deClaration */

extern int i = 7; /* Extern, deFinition */

static int j; /* Static, deClaration */

static int j = 7; /* Static, deFinition */
Quite funky indeed.

Martin

Oct 6 '07 #10
On Fri, 05 Oct 2007 16:29:10 -0700, in comp.lang.c , Keith Thompson
<ks***@mib.orgwrote:
>Mark McIntyre <ma**********@spamcop.netwrites:
>struct foo foostruct1; // declaration and definition

Right, that declares and defines an object.
at file scope, its a tentative definition I think?
>
>void f() {
struct foo foostruct2; // declaration

That also declares and defines an object (that happens to have
automatic storage duration).
oops, you're right - needs extern in front.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Oct 6 '07 #11
Martin Wells <wa****@eircom.netwrites:
On Oct 5, 8:25 am, vaib <vaibhavpang...@gmail.comwrote:
>hi to all.i'd like to know the actual difference between variable
declaration and definition.
<snip>
static int j; /* Static, deClaration */
Surely this one is a definition as well? It is a tentative
definition, but unless anything perturbs it, it acts as if is a real
one (with an initialiser of 0). Note that your other examples of
definitions that lacked initialisations were also tentative.
Quite funky indeed.
Slightly less so if the above is as much a definition as "int i;".

--
Ben.
Oct 7 '07 #12
On Sun, 07 Oct 2007 00:59:00 +0100, Ben Bacarisse wrote:
Martin Wells <wa****@eircom.netwrites:
>On Oct 5, 8:25 am, vaib <vaibhavpang...@gmail.comwrote:
>>hi to all.i'd like to know the actual difference between variable
declaration and definition.

<snip>
>static int j; /* Static, deClaration */

Surely this one is a definition as well?
No.
It is a tentative definition,
Yes.
but unless anything perturbs it, it acts as if is a real one (with an
initialiser of 0).
A tentative definition is not itself a definition.

static int i[];

This is a tentative definition, and the implicit initialiser would give
it a length of 1, but...

static int i[2];

....might appear later. The actual definition (if no explicit definition
is present) is generated once, regardless of how many tentative
definitions you have, and the actual definition's type is not necessarily
the type of any tentative definition, but rather, the composite type of
all tentative definitions.
Oct 7 '07 #13
Harald van Dijk <tr*****@gmail.comwrites:
On Sun, 07 Oct 2007 00:59:00 +0100, Ben Bacarisse wrote:
>Martin Wells <wa****@eircom.netwrites:
>>On Oct 5, 8:25 am, vaib <vaibhavpang...@gmail.comwrote:
hi to all.i'd like to know the actual difference between variable
declaration and definition.

<snip>
>>static int j; /* Static, deClaration */

Surely this one is a definition as well?

No.
>It is a tentative definition,

Yes.
>but unless anything perturbs it, it acts as if is a real one (with an
initialiser of 0).

A tentative definition is not itself a definition.
Thanks. I thought that a tentative definition could be thought of as
a kind of definition but I can see that would be confusing and is
technically wrong.

"int i;" is also a tentative definition. My point was that Martin
Wells seemed to be suggesting a difference that does not exist between
"int i;" and "static int j;" (at least for file scope identifiers).

--
Ben.
Oct 7 '07 #14
thank u all . i hope i got the point . thank u so much .

Oct 9 '07 #15
On Oct 7, 6:03 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Harald van D k <true...@gmail.comwrites:
On Sun, 07 Oct 2007 00:59:00 +0100, Ben Bacarisse wrote:
Martin Wells <war...@eircom.netwrites:
>On Oct 5, 8:25 am, vaib <vaibhavpang...@gmail.comwrote:
hi to all.i'd like to know the actual difference between variable
declaration and definition.
<snip>
static int j; /* Static, deClaration */
Surely this one is a definition as well?
No.
It is a tentative definition,
Yes.
but unless anything perturbs it, it acts as if is a real one (with an
initialiser of 0).
A tentative definition is not itself a definition.

Thanks. I thought that a tentative definition could be thought of as
a kind of definition but I can see that would be confusing and is
technically wrong.

"int i;" is also a tentative definition. My point was that Martin
Wells seemed to be suggesting a difference that does not exist between
"int i;" and "static int j;" (at least for file scope identifiers).

--
Ben.
for once it seemed all clear but things have started to get jumbled up
again.now wats exactly a tentative declaration ?? and how is int i =
3 ; an external declaration ??

Oct 9 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: John Ratliff | last post by:
I'm having issues with forward declarations and possibly member variables. Can you declare a member variable and pass it parameters. class x { private: y obj(this); } Is that valid? I'm...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
4
by: subramanian100in | last post by:
Consider the program #include <iostream> using namespace std; class Test { public: Test(Test_int c_value)
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
11
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.