Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » CGoto page 1, 2  Next

extern

 
Jump to:  
 
Bill Cunningham
PostPosted: Tue Sep 09, 2008 5:20 pm    Post subject: extern
       
Is extern used to externalize functions as well as variables? If I have
this function,

int num(int n);

Should it be declared as above in the file in which main is called and
compiled with the file that contains num's body? Or should it be declared as
this,

extern num (int n);

Bill
 

 
Bill Cunningham
PostPosted: Tue Sep 09, 2008 5:32 pm    Post subject: Re: extern
       
I just don't quite understand extern. I am wanting to put functions in
file and compile as object files and link. Is a header required?

Bill
 

 
Stephen Sprunk
PostPosted: Tue Sep 09, 2008 5:36 pm    Post subject: Re: extern
       
Bill Cunningham wrote:
Quote:
Is extern used to externalize functions as well as variables? If I have
this function,

int num(int n);

Should it be declared as above in the file in which main is called and
compiled with the file that contains num's body? Or should it be declared as
this,

extern num (int n);

The usual convention is that you would put the declaration ("int num(int
n);") in a header (.h) file and then include that header file into any
source (.c) file that called that function plus the source file that
actually defined it.

"extern" is the default for function declarations, so it's not
necessary. If you wanted to share a variable across multiple source
files (a "global" variable), though, you would need the "extern" on its
declaration in the header file.

S
 

 
Bill Cunningham
PostPosted: Tue Sep 09, 2008 5:48 pm    Post subject: Re: extern
       
"Stephen Sprunk" <stephen@sprunk.org> wrote in message
news:_dAxk.589$Z64.389@flpi143.ffdc.sbc.com...

Quote:
"extern" is the default for function declarations, so it's not necessary.
If you wanted to share a variable across multiple source files (a "global"
variable), though, you would need the "extern" on its declaration in the
header file.

I see. But if you declared a variable in a header shared by several
source files without extern, would it be global?

Bill
 

 
Harald van Dijk
PostPosted: Tue Sep 09, 2008 5:49 pm    Post subject: Re: extern
       
On Tue, 09 Sep 2008 15:20:10 -0400, Bill Cunningham wrote:
Quote:
Is extern used to externalize functions as well as variables?

No.

Quote:
If I have this function,

int num(int n);

Should it be declared as above in the file in which main is called and
compiled with the file that contains num's body? Or should it be
declared as this,

extern num (int n);

No. Why did you decide to remove "int"?

You can declare it as either "int num(int n);" or
"extern int num(int n);", either in a header file or directly in the file
where you want to call num. "extern" doesn't mean anything extra here.
 

 
Bill Cunningham
PostPosted: Tue Sep 09, 2008 6:11 pm    Post subject: Re: extern
       
"Harald van D?k" <truedfx@gmail.com> wrote in message
news:8a6b2$48c6d35b$541ad5a0$20514@cache5.tilbu1.nb.home.nl...

Quote:
No. Why did you decide to remove "int"?

Sorry typo.

Bill
 

 
Stephen Sprunk
PostPosted: Tue Sep 09, 2008 6:39 pm    Post subject: Re: extern
       
Bill Cunningham wrote:
Quote:
"Stephen Sprunk" <stephen@sprunk.org> wrote in message
news:_dAxk.589$Z64.389@flpi143.ffdc.sbc.com...
"extern" is the default for function declarations, so it's not necessary.
If you wanted to share a variable across multiple source files (a "global"
variable), though, you would need the "extern" on its declaration in the
header file.

I see. But if you declared a variable in a header shared by several
source files without extern, would it be global?

If you did not declare the variable "extern", you would likely end up
with separate copies of the variable (using the same name) for each file
that included your header. That is not good if you want a single
"global" variable shared across all those source files.

S
 

 
Amandil
PostPosted: Thu Sep 11, 2008 6:11 pm    Post subject: Re: extern
       
On Sep 9, 4:39 pm, Stephen Sprunk <step...@sprunk.org> wrote:
Quote:
Bill Cunningham wrote:
"Stephen Sprunk" <step...@sprunk.org> wrote in message
news:_dAxk.589$Z64.389@flpi143.ffdc.sbc.com...
"extern" is the default for function declarations, so it's not necessary.
If you wanted to share a variable across multiple source files (a "global"
variable), though, you would need the "extern" on its declaration in the
header file.

    I see. But if you declared a variable  in a header shared by several
source files without extern, would it be global?

If you did not declare the variable "extern", you would likely end up
with separate copies of the variable (using the same name) for each file
that included your header.  That is not good if you want a single
"global" variable shared across all those source files.

S

Worse, it would give you a link error, for finding several global
variables with the same name.

-- Marty
 

 
Richar
PostPosted: Fri Sep 12, 2008 7:58 am    Post subject: Re: extern
       
Harald van Dijk <truedfx@gmail.com> writes:

Quote:
On Tue, 09 Sep 2008 15:20:10 -0400, Bill Cunningham wrote:
Is extern used to externalize functions as well as variables?

No.

If I have this function,

int num(int n);

Should it be declared as above in the file in which main is called and
compiled with the file that contains num's body? Or should it be
declared as this,

extern num (int n);

No. Why did you decide to remove "int"?

You can declare it as either "int num(int n);" or
"extern int num(int n);", either in a header file or directly in the file
where you want to call num. "extern" doesn't mean anything extra here.

In a header file which is included by files which want to use "num()" it
would be quite common to see num() defined as

extern int num(int n);

C isn't strict enough in terms of these things IMO.

Declaring a function inside the file in which it is actually defined is,
IMO ridiculous half the time and totally unnecessary.
 

 
Nick Keighley
PostPosted: Fri Sep 12, 2008 9:40 am    Post subject: Re: extern
       
On 9 Sep, 20:49, Harald van D©¦k <true...@gmail.com> wrote:
Quote:
On Tue, 09 Sep 2008 15:20:10 -0400, Bill Cunningham wrote:

Is extern used to externalize functions as well as variables?

No.

? I'd have said "yes"


Quote:
If I have this function,

int num(int n);

Should it be declared as above in the file in which main is called and
compiled with the file that contains num's body? Or should it be
declared as this,

extern num (int n);

No. Why did you decide to remove "int"?

You can declare it as either "int num(int n);" or
"extern int num(int n);", either in a header file or directly in the file
where you want to call num. "extern" doesn't mean anything extra here.

I don't actually use extern on functions as by default they are
extern.
If you want to confine them to a single file use static.

Bill, try reading section 4.3 of K&R 2e. Though for once I found K&R
a bit heavy going in this area.

Unfortunatly ANSI had a bit of a job here. They try to explain
both what you should do (best practice) and what you can do
(so as not to break existing code)


--
Nick Keighley

quark: The sound made by a well bred duck.
 

Page 1 of 2 .:. Goto page 1, 2  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

książka telefoniczna osób prywatnych super strony koszulki chełm opakowania foliowe