473,387 Members | 1,516 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.

How is an external variable is accessed from one file to another?

Can someone demonstrate how an external variable is accessed from one file to another? Also how do we use it when we have already defined the external variable in the source file?
Jan 25 '11 #1

✓ answered by horace1

you define the variable (allocate memory for it) in one file (and you can initialise it here)
Expand|Select|Wrap|Line Numbers
  1. int data=100;          // define the variable
  2.  
you then declare the variable in all other files that use it by prefixing it with extern (saying it is defined elsewhere)
Expand|Select|Wrap|Line Numbers
  1. extern int data;          // declare the variable
  2.  
you can only initailse the variable when you define it
don't initialise the varaiable when you declare it, e.g.
Expand|Select|Wrap|Line Numbers
  1. extern int data=10;          // declare the variable
  2.  
otherwise the compiler with give warnings or error messages, e.g. from MPLAB
Expand|Select|Wrap|Line Numbers
  1. ADNS6090.c:60: warning: 'data' initialized and declared 'extern'

8 5700
horace1
1,510 Expert 1GB
you use the extern keyword to declate an external identifier, see this discussion on storage specifiers
http://www.codersource.net/c/c-tutor...pecifiers.aspx

e.g. file data.c
Expand|Select|Wrap|Line Numbers
  1. int data;
  2.  
and test.c
Expand|Select|Wrap|Line Numbers
  1. extern int data;              // declare external identifier
  2.  
  3. int main()
  4. {
  5.     data = 10;                // use external identifier
  6.     printf("%d\n", data);
  7. }
  8.  
using gcc compile it so
gcc test.c data.c
Jan 26 '11 #2
okay i got that how to declare....but if i am defining a variable in one file say test.c and i want it to be used in other file..then in test.c,do i have to prefix extern before variable definition,,, like extern int a=20; ...and if not then howcome in any file making an extern declaration of varibale a will help me access the variable -a- of file test.c.........
Jan 26 '11 #3
horace1
1,510 Expert 1GB
you define the variable (allocate memory for it) in one file (and you can initialise it here)
Expand|Select|Wrap|Line Numbers
  1. int data=100;          // define the variable
  2.  
you then declare the variable in all other files that use it by prefixing it with extern (saying it is defined elsewhere)
Expand|Select|Wrap|Line Numbers
  1. extern int data;          // declare the variable
  2.  
you can only initailse the variable when you define it
don't initialise the varaiable when you declare it, e.g.
Expand|Select|Wrap|Line Numbers
  1. extern int data=10;          // declare the variable
  2.  
otherwise the compiler with give warnings or error messages, e.g. from MPLAB
Expand|Select|Wrap|Line Numbers
  1. ADNS6090.c:60: warning: 'data' initialized and declared 'extern'
Jan 26 '11 #4
thank you sir....i am really grateful to you for solving my problems with such ease...can i have your id..i would like to learn more from you..(only if thats okay with you)....
Jan 26 '11 #5
NeoPa
32,556 Expert Mod 16PB
To save Horace1 the embarrasment of having to answer that, I will explain that such a request is considered quite inappropriate. Please do not make any such further requests.
Jan 26 '11 #6
okay,no problem,,,i will keep it in notice from next time.......
Jan 26 '11 #7
supppose i have defined a variable in some other file say external.c but i have not included that file in my present file test.c...and if i make an extern declaration of the same variable..then will i be able to access it...
Feb 26 '11 #8
donbock
2,426 Expert 2GB
"Including" is not important. Your files have to end up looking something like what horace1 showed you. It doesn't matter if you achieve that content with include files or by putting the statements directly into the c source file. Each of your c source files is compiled separately and individually into an object file. Then all of the object files are linked together. It is the link step that resolves references to global variables.
Feb 27 '11 #9

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

Similar topics

1
by: Samunderline | last post by:
Hello, Hi am trying to use the --defaults-file with a compiled version of mysql on a Feodra Core 2 based on Linux-2.6.5 I type the following command : /usr/local/mysql4src/libexec/mysqld ...
10
by: Toke H?iland-J?rgensen | last post by:
Hello. I am quite new to the c++ language, and am still trying to learn it. I recently discovered how using include files would allow me to split up my code into smaller segments, instead of having...
0
by: Glenn Mantle | last post by:
I current have two XSL file for my XML file . They are bothe identical except the one has an extra column in the table the stylesheet created that has some hyperlinks. Considering the two xsl's...
11
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
7
by: Franky | last post by:
Is there anyway i can append a variable name with another variable? For example: $a = 1; While ($a <= 5){ does something in here $result$a = $some result from within this loop $a++; }
7
by: lumpybanana247 | last post by:
is it possible in c++ to get a varible from an external file that is not part of your project? like if i had just a number in a text file, could i use that as a number variable in my project?
1
by: balkuzhali | last post by:
How to call External Java script file in Jsp
2
by: shanmugam | last post by:
Hi Guys, I am new to the perl. I want to know how to get Last accessed file owner of a file (for example a txt file). Thanks, Shanmugam. A
1
karthickbabu
by: karthickbabu | last post by:
In vb.net i create two classes. In First class i define a property to return a single value and in second class define one function it get values from property and return a single value. I call the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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,...
0
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...

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.