473,805 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2032
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
2445
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
2595
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. Two of the globals are pointers which are initialized at runtime. The others are classes whose values are initialized in the global space. When I had just the pointers, I didn't need to include the extern header in the defining class file. But...
10
6202
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: // main.cpp // This is defined in a C module extern "C" void fake_qsort(void*, std::size_t, std::size_t, int (*compare)(const void*, const void*));
18
4293
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 where it's defined.
12
2730
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 linkage? Not much on this in the FAQ or tutorials.
19
3868
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 storage-class specifier. My (little) C experience tells me that an object with "extern" is to let the linker knows that the object is referencing the object defined in somewhere, and this "somewhere" object does not have the storage-class specifier...
5
16611
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 extern storage class: /****************************************************************** SOURCE FILE ONE *******************************************************************/ extern int i; /* Reference to i, defined below */
17
4936
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
2862
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
6260
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 functions written in the "SomeLanguage". But I am quite confused what information does the linkage directive tells the compiler.The "generated" function name? The way the arguments are ordered?Or something else? And I am still wondering why extern...
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10609
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10360
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9185
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5542
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.