By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,591 Members | 629 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,591 IT Pros & Developers. It's quick & easy.

Definition vs Declaration

P: n/a
I have following code .
#include<stdio.h>
int i;
int i;
int i;

int main()
{
printf("%d",i);
return 0;
}

It compiles fine .

now i modify above code to

#include<stdio.h>
int i = 0;
int i;
int i;

int main()
{
printf("%d",i);
return 0;
}

again it compiles fine.

now i introduce more change to above code

#include<stdio.h>
int i = 0;
int i = 0;
int i;

int main()
{
printf("%d",i);
return 0;
}

It gives me redefination Error.

Ok it means that statement 'int i;' is just a declaration. I guess, it
is not just a declaration since our very first code(code with all 'int
i;') compiled with no error. There exists atleast one defination of
'i'. what should we say to statement 'int i;' ?

Sep 20 '06 #1
Share this Question
Share on Google+
18 Replies


P: n/a
shaanxxx said:
I have following code .
#include<stdio.h>
int i;
Tentative definition.
int i;
Tentative definition.
int i;
Tentative definition.

Why three?
It compiles fine .
Yes, - but... why?
now i modify above code to

#include<stdio.h>
int i = 0;
Definition and initialisation.
int i;
Tentative definition.
int i;
Tentative definition.

But... *why*?
>
int main()
{
printf("%d",i);
return 0;
}

again it compiles fine.

now i introduce more change to above code

#include<stdio.h>
int i = 0;
Definition and initialisation.
int i = 0;
Definition and initialisation. That's two for-sure definitions with the same
name, which is a for-sure error. And it has to be asked... WHY?
int i;
Tentative definition.
int main()
{
printf("%d",i);
return 0;
}

It gives me redefination Error.

Ok it means that statement 'int i;' is just a declaration. I guess, it
is not just a declaration since our very first code(code with all 'int
i;') compiled with no error. There exists atleast one defination of
'i'. what should we say to statement 'int i;' ?
"Don't do it" is the obvious answer, surely?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 20 '06 #2

P: n/a

"shaanxxx" <sh******@yahoo.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>I have following code .
#include<stdio.h>
int i;
int i;

My compilers all say:
error: redeclaration of 'i'
int i;

int main()
{
printf("%d",i);
return 0;
}

It compiles fine .
<snip>
Get a new compiler
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Sep 20 '06 #3

P: n/a
Fred Kleinschmidt wrote:
"shaanxxx" <sh******@yahoo.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
I have following code .
#include<stdio.h>
int i;
int i;

My compilers all say:
error: redeclaration of 'i'
Your compilers are all non-conforming, then.

Sep 20 '06 #4

P: n/a
Fred Kleinschmidt schrieb:
"shaanxxx" <sh******@yahoo.comwrote
>>I have following code .
#include<stdio.h>
int i;
int i;

My compilers all say:
error: redeclaration of 'i'
Then all your compilers are wrong.
>>int i;

int main()
{
printf("%d",i);
return 0;
}

It compiles fine .
<snip>

Get a new compiler
Maybe you got confused with the difference between declarations
with file scope and linkage and with block scope and no linkage:
,---
#include<stdio.h>
int i;
int i;

int main (void)
{
extern int j;
int j = 10;
int k;
int k = 10;
printf("%d\n",i);
printf("%d\n",j);
printf("%d\n",k);
return 0;
}

`---
$ gcc -std=c89 -pedantic -Wall -O tentative.c -c
tentative.c: In function `main':
tentative.c:10: error: redeclaration of 'k' with no linkage
tentative.c:9: error: previous declaration of 'k' was here
tentative.c:9: warning: unused variable `k'
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 20 '06 #5

P: n/a
Fred Kleinschmidt wrote:
"shaanxxx" <sh******@yahoo.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>I have following code .
#include<stdio.h>
int i;
int i;


My compilers all say:
error: redeclaration of 'i'
You are posting to <news:comp.lang.c>. You are using compilers for a
different language, probably C++, which has a newsgroup of its own,
<news:comp.lang.c++>. Perhaps you should learn to use "My compilers all."
Sep 20 '06 #6

P: n/a

"Martin Ambuhl" <ma*****@earthlink.netwrote in message
news:4n************@individual.net...
Fred Kleinschmidt wrote:
>"shaanxxx" <sh******@yahoo.comwrote in message
news:11**********************@i42g2000cwa.googleg roups.com...
>>I have following code .
#include<stdio.h>
int i;
int i;


My compilers all say:
error: redeclaration of 'i'

You are posting to <news:comp.lang.c>. You are using compilers for a
different language, probably C++, which has a newsgroup of its own,
<news:comp.lang.c++>. Perhaps you should learn to use "My compilers all."
I am using:
gcc on CYGWIN on a PC
/opt/ansic/bin/cc on HP 11.11
/usr/ibmcc/bin/xlc on AIX 5.3
/usr/bin/cc on IRIX 6.5
gcc on SunOS5.8
--
Fred L. Kleinschmidt
Sep 20 '06 #7

P: n/a
Fred Kleinschmidt wrote:
"Martin Ambuhl" <ma*****@earthlink.netwrote in message
news:4n************@individual.net...
>Fred Kleinschmidt wrote:
>>"shaanxxx" <sh******@yahoo.comwrote in message
news:11**********************@i42g2000cwa.google groups.com...
I have following code .
#include<stdio.h>
int i;
int i;

My compilers all say:
error: redeclaration of 'i'
You are posting to <news:comp.lang.c>. You are using compilers for a
different language, probably C++, which has a newsgroup of its own,
<news:comp.lang.c++>. Perhaps you should learn to use "My compilers all."

I am using:
gcc on CYGWIN on a PC
/opt/ansic/bin/cc on HP 11.11
/usr/ibmcc/bin/xlc on AIX 5.3
/usr/bin/cc on IRIX 6.5
gcc on SunOS5.8
You are invoking it as a C++ compiler. When GCC is invoked as a C
compiler, it does not give the diagnostic you report. Please learn to
use "My compilers all" or post your C++ observations to
<news:comp.lang.c++>, not <news:comp.lang.c>.
Sep 20 '06 #8

P: n/a
Ark
Richard Heathfield wrote:
shaanxxx said:
>I have following code .
#include<stdio.h>
int i;

Tentative definition.
>int i;

Tentative definition.
>int i;

Tentative definition.

Why three?
>It compiles fine .

Yes, - but... why?
>now i modify above code to

#include<stdio.h>
int i = 0;

Definition and initialisation.
>int i;

Tentative definition.
>int i;

Tentative definition.

But... *why*?
>int main()
{
printf("%d",i);
return 0;
}

again it compiles fine.

now i introduce more change to above code

#include<stdio.h>
int i = 0;

Definition and initialisation.
>int i = 0;

Definition and initialisation. That's two for-sure definitions with the same
name, which is a for-sure error. And it has to be asked... WHY?
>int i;

Tentative definition.
>int main()
{
printf("%d",i);
return 0;
}

It gives me redefination Error.

Ok it means that statement 'int i;' is just a declaration. I guess, it
is not just a declaration since our very first code(code with all 'int
i;') compiled with no error. There exists atleast one defination of
'i'. what should we say to statement 'int i;' ?

"Don't do it" is the obvious answer, surely?
The OP correctly distilled the case of his predicament to a minimum.
That's a satisfactory answer to "why".
[I am sure Mr. Heathfield does not question the general utility of
tentative definitions <OTunfortunately removed from C++ </OT>.]
- Ark
Sep 21 '06 #9

P: n/a
Ark said:

<snip>
[I am sure Mr. Heathfield does not question the general utility of
tentative definitions
I don't? Why on earth not?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 21 '06 #10

P: n/a
Ark
Richard Heathfield wrote:
Ark said:

<snip>
>[I am sure Mr. Heathfield does not question the general utility of
tentative definitions

I don't? Why on earth not?
Oh, you do then.
Here's what I use them for.
Consider a data arrangement of elements linking to one another (a
directed graph that is) such that there are loops of links as graph
edges. The simplest thing is (where QUAL is a set of qualifiers)
typedef QUAL struct loop_t {
QUAL struct loop_t *next;
SOMETYPE *data;
} loop_t;

loop_t A; //tentative
loop_t B = {&A, B_Data};
loop_t C = {&B, C_Data};
loop_t A = {&C, A_Data);

That technique is *very* useful in e.g. embedded systems, especially
when QUAL is static const (or static: the point is that you don't have
extern to plug the hole of a forward reference).

<OTGranted, one can perhaps arrange the data better (e.g. by using
indices into a single array), but to do it in a maintainable way one
needs an external preprocessor. </OT>

Regards,
Ark
Sep 22 '06 #11

P: n/a
Ark said:
Richard Heathfield wrote:
>Ark said:

<snip>
>>[I am sure Mr. Heathfield does not question the general utility of
tentative definitions

I don't? Why on earth not?
Oh, you do then.
Here's what I use them for.
I'm not denying one can find some kind of bizarre use for them. You can find
a use for a glass slipper if you try hard enough. (Indeed, there are some
indications that this may already have been done.) That doesn't mean that
glass slippers are generally useful or wise items of footwear. Similarly,
the fact that it is possible to find a use for a tentative definition does
not mean that they are generally useful. Nor does it mean that there is not
some other way to achieve the same goal.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 22 '06 #12

P: n/a
Ark
Richard Heathfield wrote:
Ark said:
>Richard Heathfield wrote:
>>Ark said:

<snip>

[I am sure Mr. Heathfield does not question the general utility of
tentative definitions
I don't? Why on earth not?
Oh, you do then.
Here's what I use them for.

I'm not denying one can find some kind of bizarre use for them. You can find
a use for a glass slipper if you try hard enough. (Indeed, there are some
indications that this may already have been done.) That doesn't mean that
glass slippers are generally useful or wise items of footwear. Similarly,
the fact that it is possible to find a use for a tentative definition does
not mean that they are generally useful. Nor does it mean that there is not
some other way to achieve the same goal.
1. AFAIK, a glass slipper is a misnomer resulting from an error in the
first translation from Old French; it meant to be a fur slipper. The
intention of this remark is to demonstrate that even you might not be
infallible.
2. Of course, you, like everyone else, are free not to use faculties you
are not comfortable with. However, it doesn't seem appropriate to pass
baseless value judgments.
3. In particular, w.r.t. 'bizarre', I believe the C++ standard
elaborates on removing tentative definitions and explicitly discusses an
example of a circular data structure like in the example I gave, that is
expressible in C but not in C++.

--
Ark Khasin
"Life is hard but short"
akhasin at macroexpressions dot com
www.macroexpressions.com where Unimal comes from
Sep 22 '06 #13

P: n/a
Ark said:
Richard Heathfield wrote:
>Ark said:
>>Richard Heathfield wrote:
Ark said:

<snip>

[I am sure Mr. Heathfield does not question the general utility of
tentative definitions
I don't? Why on earth not?

Oh, you do then.
Here's what I use them for.

I'm not denying one can find some kind of bizarre use for them. You can
find a use for a glass slipper if you try hard enough. (Indeed, there are
some indications that this may already have been done.) That doesn't mean
that glass slippers are generally useful or wise items of footwear.
Similarly, the fact that it is possible to find a use for a tentative
definition does not mean that they are generally useful. Nor does it mean
that there is not some other way to achieve the same goal.
1. AFAIK, a glass slipper is a misnomer resulting from an error in the
first translation from Old French; it meant to be a fur slipper.
That may or may not be true, but it has no bearing on my point.
The intention of this remark is to demonstrate that even you might not be
infallible.
I have never claimed to be infallible, but your remark fails to demonstrate
that I am.
2. Of course, you, like everyone else, are free not to use faculties you
are not comfortable with. However, it doesn't seem appropriate to pass
baseless value judgments.
I have passed no value judgements on other people. If others wish to use
tentative definitions, that is entirely up to them. I am, however,
perfectly entitled to make a value judgement about whether *I* consider
tentative definitions to be sufficiently useful to compensate for their
counter-intuitive nature.
3. In particular, w.r.t. 'bizarre', I believe the C++ standard
elaborates on removing tentative definitions and explicitly discusses an
example of a circular data structure like in the example I gave, that is
expressible in C but not in C++.
If one is prepared to set aside constness, the data structure you showed is
perfectly expressible in C++, and indeed in C, without the use of tentative
definitions. It is even possible to regain constness to some extent, by
setting up the data structure as part of an abstract data type, and
refraining from providing interfaces that allow the modification of that
structure from outside the type's implementation.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 22 '06 #14

P: n/a
On Fri, 22 Sep 2006 12:59:03 -0400, Ark <ak*****@macroexpressions.com>
wrote:
<OT>
>1. AFAIK, a glass slipper is a misnomer resulting from an error in the
first translation from Old French; it meant to be a fur slipper. The
intention of this remark is to demonstrate that even you might not be
infallible.
Actually, the French mis-translated it from the Chinese - the slipper
was embroidered silk.

Of course, all these were merely precursors of the true Cinderella,
invented by Walt Disney.

More seriously, the 1697 French version (Charles Perrault) actually
said glass (verre), though some scholars are of the opinion that
Perrault actually meant fur (vair).
</OT>

--
Al Balmer
Sun City, AZ
Sep 22 '06 #15

P: n/a
Richard Heathfield wrote:
Ark said:
1. AFAIK, a glass slipper is a misnomer resulting from an error in
the first translation from Old French; it meant to be a fur slipper.

That may or may not be true, but it has no bearing on my point.

OT, but it seems likely that it's not true. Some had speculated on such
an error, but there's little evidence of it.


Brian
Sep 22 '06 #16

P: n/a
Ark
Richard Heathfield wrote:
<snip>
I have passed no value judgements on other people.
<snip>
Please, kindly re-read your advice to the OP.
>
>3. In particular, w.r.t. 'bizarre', I believe the C++ standard
elaborates on removing tentative definitions and explicitly discusses an
example of a circular data structure like in the example I gave, that is
expressible in C but not in C++.

If one is prepared to set aside constness, the data structure you showed is
perfectly expressible in C++, and indeed in C, without the use of tentative
definitions. It is even possible to regain constness to some extent, by
setting up the data structure as part of an abstract data type, and
refraining from providing interfaces that allow the modification of that
structure from outside the type's implementation.
Richard,
Please don't get me wrong: I, too, do not find tentative definitions
particularly elegant. They do the job though.
I would really appreciate your elaborating on representing cyclical
dependencies without tentative definitions (generally and with const if
possible since that's important for resource consumption considerations).
If you are correct and an equivalent job can be done by other means, I'd
be more than happy to abandon them tentatives

--
Ark Khasin
"Life is hard but short"
akhasin at macroexpressions dot com
www.macroexpressions.com where Unimal comes from
Sep 22 '06 #17

P: n/a
Ark said:
Richard Heathfield wrote:
<snip>
>I have passed no value judgements on other people.
<snip>
Please, kindly re-read your advice to the OP.
Q: "It gives me redefination Error. [...] what should we say to statement
'int i;' ?"

A: '"Don't do it" is the obvious answer, surely?'

My advice to the OP is clear - if it gives you a redefinition error, don't
do it. I stand by that advice, and it is not a value judgement on a person.
It is, perhaps, a value judgement on erroneous code. <shrug>
If you are correct and an equivalent job can be done by other means, I'd
be more than happy to abandon them tentatives
It's entirely up to you. No skin off my nose.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 22 '06 #18

P: n/a
Ark
Richard Heathfield wrote:
Ark said:
>Richard Heathfield wrote:
<snip>
>>I have passed no value judgements on other people.
<snip>
Please, kindly re-read your advice to the OP.

Q: "It gives me redefination Error. [...] what should we say to statement
'int i;' ?"

A: '"Don't do it" is the obvious answer, surely?'

My advice to the OP is clear - if it gives you a redefinition error, don't
do it. I stand by that advice, and it is not a value judgement on a person.
It is, perhaps, a value judgement on erroneous code. <shrug>
I meant, all the way, judgment on language constructs
>If you are correct and an equivalent job can be done by other means, I'd
be more than happy to abandon them tentatives

It's entirely up to you. No skin off my nose.
Oh yes I tried. And the only thing that works was using tentatives. If
you care to show a different way, please do so. Until then, the
tentatives shall be considered vindicated.

--
Ark Khasin
"Life is hard but short"
akhasin at macroexpressions dot com
www.macroexpressions.com where Unimal comes from
Sep 23 '06 #19

This discussion thread is closed

Replies have been disabled for this discussion.