473,320 Members | 1,948 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,320 software developers and data experts.

Use of extern

I have a quick question - I have used extern quite a bit in relation
to variables, however was wondering if its applicable to function
prototypes too?

For instance, I have a file called a.c - a small snapshot of it
contains

extern int a;
void hello(int);

Then in a file called e.c, a small snapshot I have

int a;
void hello(int num)
{
.....
}

These two C files are linked together during compilation, but is there
any reason to add extern in front of the hello function prototype in
a.c (or would adding this not conform to the standard.) On my C
compiler (gcc) it doesnt complain when I add extern in this way, but
doesnt seem to do a huge amount.

Cheers,
Nick
Oct 24 '08 #1
3 2010
On 24 Oct, 12:08, polas <n...@helpforce.comwrote:
I have a quick question - I have used extern quite a bit in relation
to variables, however was wondering if its applicable to function
prototypes too?
sort of
For instance, I have a file called a.c - a small snapshot of it
contains

extern int a;
void hello(int);

Then in a file called e.c, a small snapshot I have

int a;
void hello(int num)
{
....

}

These two C files are linked together during compilation, but is there
any reason to add extern in front of the hello function prototype
only for documentaion reasons
in
a.c (or would adding this not conform to the standard.)
adding extern to a function declaration does conform to
the standard

On my C
compiler (gcc) it doesnt complain when I add extern in this way, but
doesnt seem to do a huge amount.
correct.

function declaration
void hello(int);

function definition:
void hello(int n)
{
}

by default a function definition is has external linkage
(is "visible" throughout the program) if you put static
on the front it only has file scope (it is only "visible"
to the file it is definied in). Adding extern is the same as
not using static. Hence some people use extern on functions
and many do not. I don't use extern on functions.
--
Nick Keighley

"The quality I have in mind is all-absorbing,
not just a way of doing things but a way of being."
Oct 24 '08 #2
polas <ni**@helpforce.comwrites:
I have a quick question - I have used extern quite a bit in relation
to variables, however was wondering if its applicable to function
prototypes too?
Yes, but extern is the default for file-scope function declarations.
It is usual to omit it. You need it if you write a function
declaration at block scope but that is not commonly done.

--
Ben.
Oct 24 '08 #3

"polas" <ni**@helpforce.comwrote in message news:
>I have a quick question - I have used extern quite a bit in relation
to variables, however was wondering if its applicable to function
prototypes too?
Ideally you'd have a hierarchy of exports so you could have a C source file
that was local to a library, but shared within the library.
Unfortunately you can't do this. extern is a bit like "auto".

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Oct 25 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ik | last post by:
Hello all, Under C++ when 'extern' is used with out the string literal "C" does it act the same as a 'static' ? When is it appropriate using 'extern' ? Thanks ~Ik
4
by: John Ratliff | last post by:
I have a few global variables in my app. They are defined in the main application class file and declared (extern) in a header which can be included by anyone who would want to use these variables....
10
by: Mark A. Gibbs | last post by:
I have a question about mixing C and C++. In a C++ translation unit, I want to define a function with internal linkage and C calling convention. Here's a sample of what I want to do: //...
18
by: tweak | last post by:
What's the best way to use extern when using multiplefiles that is easiest to maintain? Is it best to declare: extern int a; in a header file and include the header file in all files except...
12
by: G Patel | last post by:
I've seen some code with extern modifiers in front of variables declared inside blocks. Are these purely definitions (no definition) or are they definitions with static duration but external...
19
by: ccwork | last post by:
Hi all, I am reading "C: A Reference Manual" 4th ed and I get lost for the "extern". It says that global object without specifying the storage-class specifier will have "extern" as the default...
5
by: siliconwafer | last post by:
Hi all, I wanted to know that is use of extern keyword mandatory in case of global variables and functions used in other source files? i.e consider a following piece of code from MSDN explaining...
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 ;
5
by: Christian Christmann | last post by:
Hi, I've tree questions on the storage class specifier "extern": 1) Code example: int main( void ) { int b = -2; // my line 3 if ( a ) {
4
by: mimi | last post by:
The programmer indicates to the compiler that a function is written in a different programming language using a linkage directives.It is intuitive that extern "SomeLanguage" is used to declare...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.