[ANSYS, Inc. Logo] return to home search
next up previous contents index

A.5.2 External Variables

If you have a global variable that is declared in one source code file, but a function in another source file needs to use it, then it must be defined in the other source file as an external variable. To do this, simply precede the variable declaration by the word extern as in

extern real volume;

If there are several files referring to that variable then it is convenient to include the extern definition in a header ( .h) file, and include the header file in all of the .c files that want to use the external variable. Only one .c file should have the declaration of the variable without the extern keyword. Below is an example that demonstrates the use of a header file.

figure   

extern can be used only in compiled UDFs.



Example


Suppose that there is a global variable named volume that is declared in a C source file named file1.c

#include "udf.h"
real volume;      /* real variable named volume is declared globally */

DEFINE_ADJUST(compute_volume, domain)
{
   /* code that computes volume of some zone  */
   volume = ....
}

If multiple source files want to use volume in their computations, then volume can be declared as an external variable in a header file (e.g., extfile.h)

/* extfile.h  
   Header file that contains the external variable declaration for
   volume  */

extern real volume;

Now another file named file2.c can declare volume as an external variable by simply including extfile.h.

/* file2.c  
 
#include "udf.h"
#include "extfile.h"  /* header file containing extern declaration 
                         is included */

DEFINE_SOURCE(heat_source,c,t,ds,eqn)
{
   /* code that computes the per unit volume source using the total  
      volume computed in the compute_volume function from file1.c   */

   real total_source = ...;
   real source;

   source = total_source/volume;
   return source;
}


next up previous contents index Previous: A.5.1 Declaring Variables
Up: A.5 Variables
Next: A.5.3 Static Variables
Release 12.0 © ANSYS, Inc. 2009-01-14