Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Definition vs Declaration

Reply
Thread Tools

Definition vs Declaration

 
 
shaanxxx
Guest
Posts: n/a
 
      09-20-2006
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;' ?

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      09-20-2006
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)
 
Reply With Quote
 
 
 
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      09-20-2006

"shaanxxx" <> wrote in message
news: 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


 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      09-20-2006
Fred Kleinschmidt wrote:
> "shaanxxx" <> wrote in message
> news: 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.

 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      09-20-2006
Fred Kleinschmidt schrieb:
> "shaanxxx" <> wrote
>
>>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.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-20-2006
Fred Kleinschmidt wrote:
> "shaanxxx" <> wrote in message
> news: 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."
 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      09-20-2006

"Martin Ambuhl" <> wrote in message
news:...
> Fred Kleinschmidt wrote:
>> "shaanxxx" <> wrote in message
>> news: 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."


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


 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-20-2006
Fred Kleinschmidt wrote:
> "Martin Ambuhl" <> wrote in message
> news:...
>> Fred Kleinschmidt wrote:
>>> "shaanxxx" <> wrote in message
>>> news: 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."

>
> 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>.
 
Reply With Quote
 
Ark
Guest
Posts: n/a
 
      09-21-2006
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 <OT> unfortunately removed from C++ </OT>.]
- Ark
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-21-2006
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Run-time template list definition / Run-time variable type definition Pierre Yves C++ 2 01-10-2008 02:52 PM
Automagic determination of definition based on definition location. Jon Slaughter C++ 4 10-26-2005 05:00 PM
can a class definition inside another class's definition Jianli Shen C++ 1 03-13-2005 06:02 PM
XML jargon: declaration vs. definition Razvan XML 4 02-10-2005 07:26 AM
help?: incomplete definition with complete definition in scope Ark C Programming 1 08-07-2004 04:21 PM



Advertisments