Definition vs Declaration

S

shaanxxx

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;' ?
 
R

Richard Heathfield

shaanxxx said:
I have following code .
#include<stdio.h>
int i;

Tentative definition.

Tentative definition.

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.

Tentative definition.

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?

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?
 
M

Michael Mair

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

Then all your compilers are wrong.
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
 
M

Martin Ambuhl

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

You are posting to < You are using compilers for a
different language, probably C++, which has a newsgroup of its own,
<Perhaps you should learn to use "My compilers all."
 
F

Fred Kleinschmidt

Martin Ambuhl said:
You are posting to < 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
 
M

Martin Ambuhl

Fred said:
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
<not <news:comp.lang.c>.
 
A

Ark

Richard said:
shaanxxx said:


Tentative definition.


Tentative definition.


Tentative definition.

Why three?


Yes, - but... why?


Definition and initialisation.


Tentative definition.


Tentative definition.

But... *why*?


Definition and initialisation.


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?


Tentative definition.


"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 <OT> unfortunately removed from C++ </OT>.]
- Ark
 
A

Ark

Richard said:
Ark said:

[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).

<OT> Granted, 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
 
R

Richard Heathfield

Ark said:
Richard said:
Ark said:

[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.
 
A

Ark

Richard said:
Ark said:
Richard said:
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++.
 
R

Richard Heathfield

Ark said:
Richard said:
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.
 
A

Al Balmer

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>
 
D

Default User

Richard said:
Ark said:

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
 
A

Ark

Richard Heathfield wrote:
I have passed no value judgements on other people.

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
 
R

Richard Heathfield

Ark said:
Richard Heathfield wrote:

<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 said:
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.
 
A

Ark

Richard said:
Ark said:
Richard Heathfield wrote:

<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
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top