| | |
external variables in a multifile program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 60
Reputation:
Solved Threads: 1
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____________________________
________________file 2____________________________
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)
#include <iostream> using namespace std; int a=6; int f(int); main() { cout << f(2) << endl; }
C++ Syntax (Toggle Plain Text)
int f(int x) { return (a*x); }
•
•
•
•
Originally Posted by murschech
Does anyone have any insights to offer?
•
•
•
•
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.
•
•
•
•
Originally Posted by murschech
If the line "int a" is added to file 2 it compiles and runs, giving the wrong answer 0.
•
•
•
•
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)?
[aside]Using K&R1 as a reference for C++98 seems to leave room for a number of gaps.[/aside]
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Similar Threads
- Program that Evaluates lowest cost (VB.NET)
- C++ nest loop program - Help (C++)
- Using data i read from *.* files (C++)
- Simple C++ program terminate prematurely (C++)
- what is "unresolved external..."??? (C)
Other Threads in the C++ Forum
- Previous Thread: Problem with Character Arrays
- Next Thread: HELP With functions
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






