473,322 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 1868
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Francis Lavoie | last post by:
Hello I have some questions regarding webframework, I must say that I quite lost and these questions are basicly to help me understand the way it work. I have some knowledge with PHP and JSP....
3
by: praba kar | last post by:
Dear All, I am new to Python. I am in need of some sorting functions (eg) numerical sorting functions and alphapetical sorting functions. I have searched through net But I cannot find any...
3
by: Samuel | last post by:
I wrote a very simple httpmodule and tried to compile it with no success. This is my code: ============== Imports System Imports System.Web Imports Microsoft.VisualBasic NameSpace...
7
by: Squignibbler | last post by:
Hi all, I have a question regarding the C++ programming language regarding the nature of the relationship between pointers and arrays. If the statement MyArray is functionally identical to...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
17
by: Tapeesh | last post by:
I would like to know what is the expected behaviour of C compilers when an extern decleration is intialized. When the following code is compiled using gcc //File extern.c int arr ; int a ;
2
by: Dean R. Henderson | last post by:
For an ASP.NET web application, is there a way for one session (with appropriate security authorization) to set a HttpSessionState variable to point to another session and execute the Abandon...
0
by: Husam | last post by:
Hi EveryBody: I got this decleration for function from URLMON.dll as follows: <DllImport("urlmon.dll", ExactSpelling:=True, CharSet:=CharSet.Unicode)> _ Friend Shared Function...
3
by: kasiyil | last post by:
Hello, I have a compilation problem about namespaces: I have a namespace X in which I declera a type definition: In X.h file i write: namespace X { typedef unsigned int WindowHandle_t; }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.