GE6151 Computer Programming VARIABLES

VARIABLES

User defined variables must be declared before they can be used in a program. Variables must begin with a character or underscore, and may be followed by any combination of characters, underscores, or the digits 0 – 9

LOCAL AND GLOBAL VARIABLES Local

These variables only exist inside the specific function that creates them. They are unknown to other

functions and to the main program. As such, they are normally implemented using a stack. Local variables cease to exist once the function that created them is completed. They are recreated each time a function is executed or called.

Global

These variables can be accessed (ie known) by any function comprising the program. They are

implemented by associating memory locations with variable names. They do not get recreated if the function is recalled.

Defining Global Variables

/* Demonstrating Global variables */

Example:

#include <stdio.h>

int add_numbers( void ); /* ANSI function prototype */

/* These are global variables and can be accessed by functions from this point on */

int value1, value2, value3;

int add_numbers( void )

{

auto int result;

result = value1 + value2 + value3;

return result;

}

main()

{

auto int result;

result = add_numbers();

printf("The sum of %d + %d + %d is %d\n", value1, value2, value3, final_result);

}

The scope of global variables can be restricted by carefully placing the declaration. They are visible from the declaration until the end of the current source file.

Example:

#include <stdio.h>

void no_access( void ); /* ANSI function prototype */ void all_access( void );

static int n2;/* n2 is known from this point onwards */

void no_access( void )

{

n1 = 10; /* illegal, n1 not yet known */

n2 = 5; /* valid */

}

static int n1; /* n1 is known from this point onwards */

void all_access( void )

{

n1 = 10; /* valid */

n2 = 3; /* valid */

}

AUTOMATIC AND STATIC VARIABLES

C programs have a number of segments (or areas) where data is located. These segments are

typically, _DATA Static data _BSS Uninitialized static data, zeroed out before call to main()

_STACK Automatic data, resides on stack frame, thus local to functions _CONST Constant data, using the ANSI C keyword const

The use of the appropriate keyword allows correct placement of the variable onto the desired data segment.

Example:

/* example program illustrates difference between static and automatic variables */

#include <stdio.h>

void demo( void ); /* ANSI function prototypes */

void demo( void )

{

auto int avar = 0; static int svar = 0;

printf("auto = %d, static = %d\n", avar, svar);

++avar; ++svar;

}

main()

{

int i;

while( i < 3 ) {

demo();

i++;

}

}

Automatic and Static Variables

Example:

/* example program illustrates difference between static and automatic variables */

#include <stdio.h>

void demo( void ); /* ANSI function prototypes */

void demo( void ) {

auto int avar = 0; static int svar = 0;

printf("auto = %d, static = %d\n", avar, svar);

++avar; ++svar;

}

main()

{

int i;

while( i < 3 ) {

demo();

i++;

}

}

Program output

auto

=

0,

static

=

0

auto

auto = 0, static = 2

=

0,

static

=

1

The basic format for declaring variables is

data_type var, var, ... ;

where data_type is one of the four basic types, an integer, character, float, or double type.

Static variables are created and initialized once, on the first call to the function. Subsequent calls to the function do not recreate or re-initialize the static variable. When the function terminates, the variable still exists on the _DATA segment, but cannot be accessed by outside functions. Automatic variables are the opposite. They are created and re-initialized on each entry to the function. They disappear (are de-allocated) when the function terminates. They are created on the

_STACK segment.