2

For our assignment we have to code a program in C, but it says not to use external variables in the program. Does this mean variables in other files brought into the main code, or am I unable to use variables in the same file if they're not in the same function? (ie: could I pass a value into a function as an argument and have it return a value that may have to do with a variable in that function and set the return value equal to something, or is that using external variables?)

I've Googled around but it's not exactly clear, and I want to make sure, as this is rather important.

Doug Smith
  • 29,668
  • 57
  • 204
  • 388
  • 2
    My guess is that your teacher means global variables - so, most likely, you'll need to pass variables to functions. I would ask your teacher. – Marshall Conover Mar 14 '12 at 22:03
  • Make sure all of your variables are defined inside of a function. – DJ Quimby Mar 14 '12 at 22:03
  • 1
    With the name you have, there should be a rule preventing you to ask beginner programming questions on the internet :) Your homonym wrote one of the best computer games of all times, at a time when games were programmed by a single person. http://en.wikipedia.org/wiki/Lode_Runner – Pascal Cuoq Mar 14 '12 at 22:06
  • maybe he means `extern`? http://en.wikipedia.org/wiki/External_variable – Erik Ekman Mar 14 '12 at 22:06
  • @ErikEkman: I would understand the OP's question as "no variables with external linkage", i.e. non-static globals... – Kerrek SB Mar 14 '12 at 22:08
  • At least it would rule out the use of stdin and stdout ;-) – wildplasser Mar 14 '12 at 22:17

3 Answers3

2

Just to be sure, I'd make all my variables part of a function, and either pass them as parameters or return them from the functions.

There are at least two interpretations of external variables.

First off, we have the extern keyword, which basically symbolizes what you would call a global variable. It's a variable declared in multiple translation unit, but it only exists in one place in memory. It is initialized in a single file and all subsequent changes affect every scope the variable is used in:

//globals.cpp
int x = 1337;

//main.cpp
extern int x;

int main()
{
   //x is 1337 here
   return 0;
}

The second meaning could be a variable that is declared and defined in class scope, but not used as extern. However, you could consider it external to the methods.

//main.cpp
int x = 1337; //is this external?
              //could be, remove it just to be safe

int main()
{
   return 0;
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

Let's go with all the possible cases, because from your question I'm not too sure either:

There is extern as a keyword. In order to understand this, you need to understand compilation units. Basically, each file is a compilation unit - so each .c is compiled to a .o with the headers substituted in place. In each compilation unit, you forward declare symbols you expect to use - functions belonging to other compilation units, for example.

Now, if you declare a global variable in one .c file, it is global wrt that file, but does not exist as a symbol in any other file at all - the compiler will error because it doesn't know where that variable was declared.

(Of course, if you declare the variable in a header - it will exist in all of the objects the header is included in, and then the linker will sulk, because when it links all the objects up some of the symbols will have the same name).

To get around this, it is possible to define a variable with extern int x;, for example. This tells the compiler a) int x should be available to this compilation unit, b) int x is not in this compilation unit and c) the linker should check it exists somewhere in all the units you've put together to form a library or program.

Conceptually, you're doing this all the time with forward-declarations of functions. There's just no way to forward declare a variable. In fact, you can do this with functions too and not bother r.e shared headers, although this is not really a good idea.


The other case is that "external variables" mean something external to a certain scope or module you have. I would check your assignment very carefully and if in doubt ask - whoever set it should be able to explain to you exactly what they mean.

Community
  • 1
  • 1
1

An external variable is a variable with external linkage.

A variable with external linkage is a variable defined at file scope without the static keyword.

int bla = 0;           // external variable
static int blop =  1;  // non-external variable

int main(void)
{
    return bla
}

Note that a variable declared with the extern keyword doesn't necessarily have external linkage. Like const does not mean constant in C, extern does not mean external.

People often use the word external to say that a variable is actually declared in the current translation unit but is defined in another translation unit.

ouah
  • 142,963
  • 15
  • 272
  • 331