943,946 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 12003
  • C++ RSS
Jul 28th, 2005
0

external variables in a multifile program

Expand Post »
I have a question on external variables in a multifile program.
Quoting from K&R (first addition, page 72)

"By default, external variables are also "global", so that all references to such a variable by the same name (even from functions compiled separately) are references to the same thing."

Here's an example that doesn't work. The variable 'a' in file 1 is external (since it's defined outside of any function) so, according to the above paragraph, it should have the same value in file 2. The
program, however, doesn't compile. If the line "int a" is added to
file 2 it compiles and runs, giving the wrong answer 0. If instead the
line "extern int a" is added then it runs and gives the correct
answer 12. I understand why the last version works, but shouldn't
the first one also (according to K&R)?

Does anyone have any insights to offer?

_____________________file 1____________________________
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int a=6;
  5. int f(int);
  6. main()
  7. { cout << f(2) << endl;
  8. }
________________file 2____________________________
C++ Syntax (Toggle Plain Text)
  1. int f(int x)
  2. { return (a*x);
  3. }
Similar Threads
Reputation Points: 21
Solved Threads: 1
Junior Poster in Training
murschech is offline Offline
60 posts
since Dec 2004
Jul 28th, 2005
0

Re: external variables in a multifile program

Quote originally posted by murschech ...
Does anyone have any insights to offer?
I'll try.

Quote originally posted by murschech ...
Here's an example that doesn't work. The variable 'a' in file 1 is external (since it's defined outside of any function) so, according to the above paragraph, it should have the same value in file 2. The program, however, doesn't compile.
As it shouldn't. Identifiers need to be declared. The linkage of an identifier is a bit separate.

Quote originally posted by murschech ...
If the line "int a" is added to file 2 it compiles and runs, giving the wrong answer 0.
I get a linker error because adding this definition conflicts with the definition in the other file.

Quote originally posted by murschech ...
If instead the line "extern int a" is added then it runs and gives the correct answer 12. I understand why the last version works, but shouldn't the first one also (according to K&R)?
I'll borrow Chris Torek's words of wisdom: "At file scope, 'extern' means, approximately, 'read this as a declaration, not a definition'".


[aside]Using K&R1 as a reference for C++98 seems to leave room for a number of gaps.[/aside]
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem with Character Arrays
Next Thread in C++ Forum Timeline: HELP With functions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC