This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: multiple declaration / definition
- To: Aparna Ranish <aparna_ranish at rediffmail dot com>
- Subject: Re: multiple declaration / definition
- From: Ingo Krabbe <ikrabbe at earthling dot net>
- Date: Tue, 10 Jul 2001 10:17:12 +0200 (CEST)
- cc: "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>, "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Reply-To: Ingo Krabbe <i dot krabbe at dokom dot net>
On 10 Jul 2001, Aparna Ranish wrote:
> Hi
>
> I have a small piece of code which according to me should give a compilation error.
>
>
> test.c
> ----------
> int i = 2;
> int i;
> int main ()
> {
> return 0;
> }
> See according to me <int i> gives both declaration and definition for i. So, the program should give multiple definition error. But it does not.
>
> Once the definition is over does it take <int i>
> as declaration ??
>
>
> another test
> ----------------
>
> int main ()
> {
> int i = 2;
> int i;
> return 0;
> }
> But in this case it gives redclaration error.it says i previously declared here.
> Actually it is supposed to give the error in both the
> cases.
>
> Can some body expalin me, where I am going wrong ??
You have to read it as
extern int i;
int i = 2;
which could be found for global variables in header files. Making it static
static int i;
int i = 2;
gives you the "desired" error.
But shouldn't it be static by default ?
CU INGO