Professional Documents
Culture Documents
When called with swap(x, y); When called with swap(&x, &y); where x and y
x and y will remain the same are int values, then a points to x and b points
to y, so the values pointed to are swapped,
not the pointers themselves
extern
• Global variables are available within a file by declaring
variables prior to the functions
– Global variables are available between files using the reserved word
extern to define them
• The extern declaration appears in the files that are using the externally
declared variables, not in the file where they were originally declared
– So, if file 1 declares variables and file 2 wants to use those variables, you use an
#include statement to load file 1 and extern statements to declare any of file 1’s
global variables to be used in file 2
– Take care when using global variables, they are not encouraged and
lead to hard-to-read and hard-to-debug code
• In fact, I would recommend that you avoid them if at all possible!
• NOTE that extern defines an external variable, but does not
declare it, a separate declaration is required although they can
be combined into a single statement as in:
– extern int x;
Scope Rules
• Scope for a variable (or a function) are the locations
within a program where that variable (or function) can
be accessed
• There are generally two types of scope: local and global
– A local variable is defined within the local environment: the
current block or the current function
• Variables declared within { } are local to that block, whether the block
is a block within a function or the function itself
• Variables declared within a for-loop are local to that for-loop
• Parameters in a function header are local to that function
• Note that if two variables share the same name but are in different
blocks or functions, then the variable declared in this environment will
be the one used in a reference
– A global variable is defined outside of the local environment
and available anywhere within the file, or in other files by
using extern
Scope Example with Blocks
#include <stdio.h> void func1(int x, int y)
{
void func1(int, int); x++; y--;
void func2(int, int); printf("Values inside func1:
%d %d\n", x, y);
void main() }
{ void func2(int x, int y)
int x = 5, y = 10; {
printf("Values before first function x+=2; y-=2;
call: %d %d\n", x, y); printf("Values inside func2:
func1(x, y); %d %d\n", x, y);
{ {
int x = 15, y = 20; int x = 0, y = 1;
printf("Values inside printf("Values inside func2's
block: %d %d\n", x, y); block: %d %d\n", x, y);
func2(x, y); }
} }
printf("Values before after block:
%d %d\n", x, y);
}
Header Files
• In order to call upon functions compiled in separate files, you
need to include their definition as a declaration or a prototype
– for simplicity, if you have functions in several files, each of which
call upon some of the same functions, you can place the prototypes
in a single file, called a header file
• all other shared definitions and declarations can go here as well
• A header files typically only contain definitions and
declarations, not executable code
– consider as an example a calculator program that has its’ functions
split into multiple files:
• a main function in one file which calls upon
• stack operations in a file stack.c
• a parsing operation to get tokens from a string in the file getop.c
• a function to get char input in the file getch.c
• a header file contains prototypes and common declarations called calc.h
Static and Register
• The reserved word • The register reserved word is
static is used to define used to suggest to the compiler
a variable as being that a particular variable should
static, typically used be moved to and kept in a
with a variable register
defined using extern – The idea is to give the compiler
– Static means that the some advice
storage of that variable – The register declaration is only
remains in existence permitted for local variables and
• Local variables are parameters and the advice is not
removed from memory
once the function
necessarily taken by the compiler
terminates, a static • Just because you place register in
variable remains in front of a variable’s declaration
memory so that it can does not mean that the variable will
be accessed later be placed into a register
Macro Substitution
• We saw #define can be used for constants
– Form: #define name substitution-text
• Example: #define MAX 10
– But unlike a constant in other languages as a variable whose
value does not change
• here the definition is a form of macro substitution
– all instances of the name are replaced by the value
• this can be used in some interesting ways, such as in the following:
– #define forever for( ; ; ) /* infinite loop */
– #define max(A, B) ((A) > (B) ? (A) : (B)) /* defines a max operation */