declarations and definitions

P

panku

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

Kenny McCormack

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

Jens Thoms Toerring

panku said:
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
 
K

Keith Thompson

panku said:
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.
 
D

David Thompson

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


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.


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.
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top