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

Extern in c/c++

Hello :

Can anyone please elaborate in detailed use of extern in c/c++.The articles i read

say " its used for those methods/functions defined somewhere else",

e.g.extern 'function name', extern"C" etc..

thanks,
chetan.
Sep 12 '07 #1
5 2255
Hello :

Can anyone please elaborate in detailed use of extern in c/c++.The articles i read

say " its used for those methods/functions defined somewhere else",

e.g.extern 'function name', extern"C" etc..

thanks,
chetan.
----------------------------------------------------------------------------------------------------------------------

When u code, your source code is distributed over multiple files. You compile them separately and link the object code. Now it may happen for instance say you have one variable that has been defined in one file and is used to keep count. Now in some other file you need to have a count. but then if you are including the same file in your project, then will you declare a variable called as count back again, although u have the variable already defined for that purpose.

So in order to remove this wastage of memory, we tell the compiler that i need such variable. but this being already delcared somewhere... i also want to use it in over here. this is extern...
Sep 12 '07 #2
deoashish, can u explain it with a code ?? anyway im not getting what you want to

say, like suppose we have 1.c and 2.c both include 1.h.We have defined count

variable in 1.h which is been included in both 1.c and 2.c , so at second place

you will define count as "extern count " instead of including 1.h ???

is that correct ?
Sep 12 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
say, like suppose we have 1.c and 2.c both include 1.h.We have defined count variable in 1.h
You never define a variable in a header file. Let me repeat that: You never define a variable in a header file.

If you do, then every time you include the header you get a anotrher variable. When your build gets to the link step, it will fail is redefinition errors.

What your do is define the variable in a .c file:

Expand|Select|Wrap|Line Numbers
  1. //in a .c file:
  2.  
  3. int var;
  4.  
To access this variable from another .c file you tell the compiler the variable is defined outside (external to) this file:
Expand|Select|Wrap|Line Numbers
  1. //another .c file.
  2.  
  3. extern int var;
  4.  
  5.  
All the header file does is save you from putting than extern in every .c file. Instead you put the extern in the header and then include the header.
Sep 12 '07 #4
yes i understood.But have some more questions

1) Anyway you are redefining variable again as extern int Var etc.. then whats

the use of defining this variable as extern ( isn't it not as good as defining int Var) ?

2) Do extern variables retain same value as originally defined variables ?
Sep 13 '07 #5
gpraghuram
1,275 Expert 1GB
yes i understood.But have some more questions

1) Anyway you are redefining variable again as extern int Var etc.. then whats

the use of defining this variable as extern ( isn't it not as good as defining int Var) ?

2) Do extern variables retain same value as originally defined variables ?
If u are using extern in the variable declaration it is not definition.
What you are doing is declaration of the variable , means definition of the variable is available elsewhere.

Raghuram
Sep 13 '07 #6

Sign in to post your reply or Sign up for a free account.

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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.