473,480 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Definition vs Declaration

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
18 2468
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

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

"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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1627
by: Razvan | last post by:
Hi ! Consider the following XML element: <!ELEMENT value (#PCDATA)> <!ATTLIST value year CDATA #IMPLIED>
8
2442
by: newmans | last post by:
Perhaps one of the experts can straighten me out on this point... In Bjarne Stroustrup's book 'The C++ Programming Language' 3rd Edition, section 4.9, indicates that typedef complex<short>...
19
2866
by: J. J. Farrell | last post by:
After many years of dealing with definition and linkage issues in ways that I know to be safe, I've decided it's time to try to understand this area properly. Consider a header file with the file...
10
20829
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as...
2
2672
by: xllx.relient.xllx | last post by:
I have a few quetions about definitions vs declarations concerning defined types (user defined), specifically classes. From what I understand, a class contained in a header file is a definition as...
8
1686
by: caferias | last post by:
Hello, I'm curious about the difference between the term "declaration" and "definition" in the context of XML. Where is the difference? Greetings: Fernando
9
8857
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
2
4677
by: Laurent Deniau | last post by:
I would like to know why the following small program does not compile (checked with gcc 4.1.2) and if the compiler behavior is correct: struct A; typedef void (T)(struct A*); void f(void) {...
15
14885
by: vaib | last post by:
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...
5
1190
by: Tony Johansson | last post by:
Hello! I'm reading in a book and sometimes they use the term declaration and sometimes they use the term definition. I just wonder if there exist any kind of definition about what exactly a...
0
7041
marktang
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,...
0
6908
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...
0
7084
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...
1
4779
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4481
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...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
181
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.