468,766 Members | 1,333 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,766 developers. It's quick & easy.

regarding decleration

Hi Everyone,

I had a doubt regarding extern decleration, i tried this is one
source file,

extern int sample;
extern int sample;

int main()
{
return(0);
}

It compiled properly, can anyone explain how this works?

Jan 8 '07 #1
10 1635
sa*****@yahoo.co.in writes:
Hi Everyone,

I had a doubt regarding extern decleration, i tried this is one
source file,

extern int sample;
extern int sample;

int main()
{
return(0);
}

It compiled properly, can anyone explain how this works?
There will a rush of people telling you about non ISO C, but what is
more important in a question like this is : what is that surprises you
about it compiling? How does what work?

--
Jan 8 '07 #2
sa*****@yahoo.co.in said:
Hi Everyone,

I had a doubt regarding extern decleration, i tried this is one
source file,

extern int sample;
extern int sample;

int main()
{
return(0);
}

It compiled properly,
Sure. Why wouldn't it?
can anyone explain how this works?
Declarations that are not definitions do not reserve storage. You've told
the compiler that there exists an object of type int, named 'sample', in
some other translation unit. The compiler believes you, and then you tell
it again. In neither case does the compiler create an object, so there is
no name clash. It's no different in principle from writing two function
prototypes in your code for the same function.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 8 '07 #3
Richard said:
sa*****@yahoo.co.in writes:
>Hi Everyone,

I had a doubt regarding extern decleration, i tried this is one
source file,

extern int sample;
extern int sample;

int main()
{
return(0);
}

It compiled properly, can anyone explain how this works?

There will a rush of people telling you about non ISO C,
Why? What's wrong with it? What did I miss?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 8 '07 #4
sa*****@yahoo.co.in wrote:
Hi Everyone,

I had a doubt regarding extern decleration, i tried this is one
source file,

extern int sample;
extern int sample;

int main()
{
return(0);
}

It compiled properly, can anyone explain how this works?
An extern declaration informs the compiler that the concerned object
exists in a seperate translation unit. No storage is allocated for the
declaration itself. Writing the declaration multiple times is poor
style and an invitation for future problems.

Incidentally, your signature for main is not compliant with the
standard.
>From C99 draft:
<quote>
5.1.2.2.1 Program startup

[#1] The function called at program startup is named main.
The implementation declares no prototype for this function.
It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv,
though any names may be used, as they are local to the
function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;8) or in some other implementation-defined
manner.

8) Thus, int can be replaced by a typedef name defined as
int, or the type of argv can be written as char ** argv,
and so on.
</quote>

Jan 8 '07 #5
Richard Heathfield <rj*@see.sig.invalidwrites:
Richard said:
>sa*****@yahoo.co.in writes:
>>Hi Everyone,

I had a doubt regarding extern decleration, i tried this is one
source file,

extern int sample;
extern int sample;

int main()
{
return(0);
}

It compiled properly, can anyone explain how this works?

There will a rush of people telling you about non ISO C,

Why? What's wrong with it? What did I miss?
OK, OK : not ISO. But to use the EXIT codes and the necessary includes
etc.

But your cleverness aside, my point was valid. Why did HE not expect it
to compile.

I expect he was referring to the extern statements, but who knows?
--
Jan 8 '07 #6
santosh said:
sa*****@yahoo.co.in wrote:
>Hi Everyone,

I had a doubt regarding extern decleration, i tried this is one
source file,

extern int sample;
extern int sample;

int main()
{
return(0);
}

It compiled properly, can anyone explain how this works?

An extern declaration informs the compiler that the concerned object
exists in a seperate translation unit. No storage is allocated for the
declaration itself. Writing the declaration multiple times is poor
style and an invitation for future problems.

Incidentally, your signature for main is not compliant with the
standard.
Yes, it is.

<snip>
It shall be defined with a return type of int and with no
parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv,
though any names may be used, as they are local to the
function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;
In a definition (such as the OP's), int main() and int main(void) are
equivalent.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 8 '07 #7
Richard wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:
>Richard said:
>>sa*****@yahoo.co.in writes:

Hi Everyone,

I had a doubt regarding extern decleration, i tried this is one
source file,

extern int sample;
extern int sample;

int main()
{
return(0);
}

It compiled properly, can anyone explain how this works?
There will a rush of people telling you about non ISO C,

Why? What's wrong with it? What did I miss?

OK, OK : not ISO. But to use the EXIT codes and the necessary includes
etc.
Neither is needed in this example: exiting with 0 is well-defined,
so no macro is needed, so no #include is.

(The parentheses are redundant and I would unhesitatingly discard them.)

--
Chris "first on the Underground!" Dollin
"People are part of the design. It's dangerous to forget that." /Star Cops/

Jan 8 '07 #8
Richard said:
Richard Heathfield <rj*@see.sig.invalidwrites:
>Richard said:
>>sa*****@yahoo.co.in writes:
<snip>
>>> extern int sample;
extern int sample;
int main()
{
return(0);
}
It compiled properly, can anyone explain how this works?

There will a rush of people telling you about non ISO C,

Why? What's wrong with it? What did I miss?

OK, OK : not ISO. But to use the EXIT codes
What's wrong with 0?
and the necessary includes
What necessary includes?
etc.

But your cleverness aside, my point was valid. Why did HE not expect it
to compile.
Well, we can reasonably guess that he wasn't worried about int
main(){return(0);}, which only leaves the declaration. The fact that
there's two of it suggests strongly that he wouldn't have a problem with
one of it. So he's curious about why it is possible for there to be more
than one declaration - which suggests to me that he's had experience of the
fact that multiple *definitions* are, modulo nit-picking, illegal.
I expect he was referring to the extern statements, but who knows?
I do. In some parts of the world, noggins are still in constant use.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 8 '07 #9
Richard wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:
>Richard said:
>>sa*****@yahoo.co.in writes:

Hi Everyone,

I had a doubt regarding extern decleration, i tried this is one
source file,

extern int sample;
extern int sample;

int main()
{
return(0);
}

It compiled properly, can anyone explain how this works?

There will a rush of people telling you about non ISO C,
Why? What's wrong with it? What did I miss?

OK, OK : not ISO. But to use the EXIT codes and the necessary includes
etc.
No 'EXIT code' is needed; 'return 0' is a perfectly valid way to exit
from main.
There are no missing 'necessary headers.'
What are you on about?
But your cleverness aside, my point was valid.
Actually, it's your 'cleverness' in referring to 'non ISO C', 'EXIT
codes', and 'necesaary headers', none of which make a lick of sense,
that is in question.
Jan 8 '07 #10
<sa*****@yahoo.co.inwrote in message
news:11**********************@51g2000cwl.googlegro ups.com...
Hi Everyone,

I had a doubt regarding extern decleration, i tried this is one
source file,

extern int sample;
extern int sample;

int main()
{
return(0);
}

It compiled properly, can anyone explain how this works?
One has to distinguish between the declaration of a variable and the
definition. Declarations let the compiler know about a variable (name,
type) defined in another software module.

The most robust system is to have what is called "full linkage" (for both
functions and variables). Full linkage is:

a)Definition checked against public declaration in .H file.

b)Public declaration in .H file checked against (or necessary for) all
references or invocations.

c)Transitively, definition checked against references or invocations.

With a variable, the most common scheme is to use a preprocessor trick so
that the same text that defines the variable in the owning module declares
it for clients. (That way, the name and type are guaranteed same, as they
are actually the same text.)

For example:

FILE MYSTFF.H
--------
.... Some preprocessor magic goes here ...

DECMOD_MYSTUFF int sample;
//Expands to "int sample" when compiling MYSTUFF.C.
//Expands to "extern int sample" when compiling any other software
module.

If you want more details, just write me at DT*@E3FT.COM and make it by my
SPAM filters.

Dave.
Jan 8 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Francis Lavoie | last post: by
3 posts views Thread by praba kar | last post: by
3 posts views Thread by Samuel | last post: by
17 posts views Thread by Tapeesh | last post: by
2 posts views Thread by Dean R. Henderson | last post: by
reply views Thread by Husam | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.