Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   declarations and definitions (http://www.velocityreviews.com/forums/t742905-declarations-and-definitions.html)

panku 02-01-2011 08:30 AM

declarations and definitions
 
what is diff the declaration and definition?
void main()
{
extern int i;
i=90; //Error: i is undefined
}

as extern int i is there it is giving error and also when we write
extern int i=90 then also it gives error ? why???????????????????????

Kenny McCormack 02-01-2011 09:33 AM

Re: declarations and definitions
 
In article <95336a7d-6e87-4903-8b53-755a5bedae35@k17g2000pre.googlegroups.com>,
panku <pankaj10111991@gmail.com> wrote:
>what is diff the declaration and definition?
>void main()
>{
> extern int i;
> i=90; //Error: i is undefined
>}
>
>as extern int i is there it is giving error and also when we write
>extern int i=90 then also it gives error ? why???????????????????????


Put this line before the "void main()" line:

#define extern

That should fix it.

--
One of the best lines I've heard lately:

Obama could cure cancer tomorrow, and the Republicans would be
complaining that he had ruined the pharmaceutical business.

(Heard on Stephanie Miller = but the sad thing is that there is an awful lot
of direct truth in it. We've constructed an economy in which eliminating
cancer would be a horrible disaster. There are many other such examples.)

Jens Thoms Toerring 02-01-2011 11:35 AM

Re: declarations and definitions
 
panku <pankaj10111991@gmail.com> wrote:
> what is diff the declaration and definition?
> void main()


Make that

int main( void )

since main() is supposed to return an int.

> {
> extern int i;
> i=90; //Error: i is undefined
> }


> as extern int i is there it is giving error and also when we write
> extern int i=90 then also it gives error ? why???????????????????????


What you have with

extern int i;

isn't a defininition but just a declaration. With a decla-
ration you merely tell the compiler that something (an ob-
ject or a function) exists somewhere and that it shouldn't
worry about that it doesn't know where that thing actually
is. So, in plain words, it says: "Somewhere but not here
an int variable named 'i' is defined and I would like you
to use that when I write 'i' in the current context."

But to get that to work you must also supply a definition of
what you promised the compiler to exist. Otherwise the linker
will catch up with you and tell you that it's not defined (via
the dreaded "undefined reference" linker error message). One
way to do so would be to change your program to

int main( void ) {
extern int i;
i = 90;
return 0;
}
int i;

Now you have the required definition of 'i' in the last
line - it makes the compiler create an object of type int
and name 'i' that the merely declared 'i' in main() can
be associated with. Another way would be to have 'i' de-
fined in another source file and then link them together.

It's rather similar to function declarations: you proba-
bly wouldn't be too surprised if the linker complained
about

extern int foo( double );

int main( void ) {
foo( 3.14 );
return 0;
}

Here you also just tell the compiler that some function
foo() exists somewhere else but there's no definition that
would tell what foo() is actually doing. Obviously, without
that the program can't be created and you get a linker error
about an undefined reference to 'foo'. (Note that the 'extern'
qualifier is redundant for function declarations, so you will
not see it used that often with function declarations.)

Another point: since

external int i;

does not define a variable but merely declares it you also
can't initialize it. Initialization can only happen at the
place where something is defined. Consider what should hap-
pen if it would be possible and you would do

int main( void ) {
extern int i = 90;
return 0;
}
int i = -42;

You'd have two contradictory initializations for the same
variable! What should the poor compiler do with that?

But when you instead have

int main( void ) {
extern int i;
i = 90;
return 0;
}
int i = -42;

things are fine again - 'i' will be initialized to -42
while it's created and then later in main() its value
gets changed to 90.
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de

Keith Thompson 02-01-2011 04:19 PM

Re: declarations and definitions
 
panku <pankaj10111991@gmail.com> writes:
> what is diff the declaration and definition?
> void main()


This should be "int main(void)". If your C book tells you that
"void main()" is correct, get a better book.

> {
> extern int i;


This declares that there's an object of type int named "i".
It doesn't create such an object.

> i=90; //Error: i is undefined


The above statement is legal *if* you've defined i somewhere (and if
it's defined in a translation unit that's linked into your program).
You should have gotten an error message from the linker, not from
the compiler.

> }
>
> as extern int i is there it is giving error and also when we write
> extern int i=90 then also it gives error ? why???????????????????????


That's a different error, one that should be reported by the compiler.
You can't initialize an object unless you're creating it. The "extern"
says you're *not* creating it.

BTW, it would be helpful if you'd post the actual error messages. The
problem was obvious enough in this case, but in general more information
is better.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

osmium 02-01-2011 07:42 PM

Re: declarations and definitions
 
"Kenneth Brody" wrote:

> On 2/1/2011 4:33 AM, Kenny McCormack wrote:
>> In
>> article<95336a7d-6e87-4903-8b53-755a5bedae35@k17g2000pre.googlegroups.com>,
>> panku<pankaj10111991@gmail.com> wrote:
>>> what is diff the declaration and definition?
>>> void main()
>>> {
>>> extern int i;
>>> i=90; //Error: i is undefined
>>> }
>>>
>>> as extern int i is there it is giving error and also when we write
>>> extern int i=90 then also it gives error ? why???????????????????????

>>
>> Put this line before the "void main()" line:
>>
>> #define extern
>>
>> That should fix it.

>
> If you use the word "fix" in the same sense as a veterinarian uses it.


LOL!



Hans Vlems 02-03-2011 12:24 PM

Re: declarations and definitions
 
On 1 feb, 20:40, Kenneth Brody <kenbr...@spamcop.net> wrote:
> On 2/1/2011 4:33 AM, Kenny McCormack wrote:
>
>
>
>
>
> > In article<95336a7d-6e87-4903-8b53-755a5beda...@k17g2000pre.googlegroups.com>,
> > panku<pankaj10111...@gmail.com> *wrote:
> >> what is diff the declaration and definition?
> >> void main()
> >> {
> >> * extern int i;
> >> * i=90; * * * * * * * * *//Error: i is undefined
> >> }

>
> >> as extern int i is there it is giving error and also when we write
> >> extern int i=90 then also it gives error ? why???????????????????????

>
> > Put this line before the "void main()" line:

>
> > #define extern

>
> > That should fix it.

>
> If you use the word "fix" in the same sense as a veterinarian uses it.
>
> --
> Kenneth Brody- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -


More like the way contract killers use it

David Thompson 02-11-2011 05:15 AM

Re: declarations and definitions
 
On Tue, 01 Feb 2011 08:19:25 -0800, Keith Thompson <kst-u@mib.org>
wrote:

> panku <pankaj10111991@gmail.com> writes:


> > {
> > extern int i;

>
> This declares that there's an object of type int named "i".
> It doesn't create such an object.
>
> > i=90; //Error: i is undefined

>
> The above statement is legal *if* you've defined i somewhere (and if
> it's defined in a translation unit that's linked into your program).
> You should have gotten an error message from the linker, not from
> the compiler.
>
> > }
> >
> > as extern int i is there it is giving error and also when we write
> > extern int i=90 then also it gives error ? why???????????????????????

>
> That's a different error, one that should be reported by the compiler.
> You can't initialize an object unless you're creating it. The "extern"
> says you're *not* creating it.
>

Nit: Only an error for declaration at block scope, as it was here.
(Or precisely a constraint violation and required diagnostic.)

At file scope the initializer effectively overrides 'extern' and the
declaration is a definition. This inconsistency can be confusing and
arguably better style is to avoid it, but it is allowed.



All times are GMT. The time now is 02:06 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.