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

Extern

77
hi,
Every one here knows the difference between global variables and extern variables.
Thing am not sure about is; if i have defined a global variable outside main(); before main, it works fine.
if we define it at the same place and use extern in main(), it still works.
It is totally right syntactically and oterwise also.
But isn't it a bit weird?.. i mean it's not defined outside the file and still the use of extern is not raising any error?
I feel that it should raise an error.
Jun 13 '10 #1
3 1832
Banfa
9,065 Expert Mod 8TB
A code example to demonstrate your point would have been good but I assume you mean something like

Expand|Select|Wrap|Line Numbers
  1. int example = 0;
  2.  
  3. int main()
  4. {
  5.     extern int example;
  6.  
  7.     example = 5;
  8.  
  9.     return 0;
  10. }
  11.  
So what you are probably missing is that the compiler works on a single source (cpp) file and and files it includes. It does not consider multiple source files at a time so when you compile a file it does not consider the contents of any other file.

Also you seem to believe that an extern statement indicates that a symbol (variable or function) exists in another file, however what extern really means is that a symbol exists somewhere, I'm not saying where.

Having an extern declaration in the same compilation unit (source file plus all the files it includes) as a definition for the same symbol is usual as that ensures your definition and extern declaration match. Normally the extern declaration would be in a header for inclusion into multiple source files.

So in the above code line 1 defines the symbol example then line 5 declares it as existing somewhere, which is true it exists in this file. There is no syntactic or logic error. Line 5 is superfluous in this instance since definitions are also declarations but it is not breaking an rules that require a warning or error. You can declare a symbol as many times as you want but it must be defined once and only once with-in its scope.
Jun 13 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
The extern keyword has to do with linkage. That is, whether or not the variable is sharable outside the current scope.

A static global variable is not sharable outside the current scope, typically the current file. It is accessible only from the point of definition to the end of the file.

An extern variable is sharable outside the current scope. In the case of the exanple variable, the variable is defined and a value placed in it. The variable is sharable (accessible) from other source files:

Expand|Select|Wrap|Line Numbers
  1. extern int example = 0;
However, if there is no value, then the variable does not need to bed defined. In this case, the extern means there is a sharable variable somewhere else:

Expand|Select|Wrap|Line Numbers
  1. extern int example;  //no variable created
const global variables have internal linkage (static) and so are not sharable with other source files. However, this is the default behavior. You can create a sharable const variable by defining using the extern keyword:

Expand|Select|Wrap|Line Numbers
  1. extern const int example = 0;
and this variable is accessible from another source file by:

Expand|Select|Wrap|Line Numbers
  1. extern const int example;
Jun 13 '10 #3
Time
77
It was helpful enough.
Thanks to both of you..:)
Jun 14 '10 #4

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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.