Sunday, March 18, 2007

Constants & Variable Types

Variables

A variable must be declared before it can be used. Variables can be declared at the start of any block of code, but most are found at the start of each function. Most local variables are created when the function is called, and are destroyed on return from that function.

A declaration begins with the data type, followed by the name of one or more variables.
For example,
int temp1, temp2, temp[20];

The above can be also be declared as follows.

int temp1;
int temp2;
int temp[20];

Variables can also be initialised when they are declared

int temp1=50;

int temp2=25;

Data Types

C provides a wide range of types.
The basic data types are

int -> An integer.
float -> A floating point number.
char -> A single bit of memory, enough to hold a character.

There are some more data types

Short -> An integer, possibly of reduced range.

long -> An integer, possibly of increased range.

unsigned -> An integer with no negative range.

unsigned long -> Like unsigned, possibly of increased range.

double -> A double precision floating point number.

Global Variables

Local variables are declared within the body of a function, and can only be used within that function.

Alternatively, a variable can be declared globally so it is available to all functions. Modern programming practice recommends against the excessive use of global variables. They can lead to poor program structure, and tend to clog up the available name space.

A global variable declaration looks normal, but is located outside any of the program's functions. This is usually done at the beginning of the program file, but after preprocessor directives. The variable is not declared again in the body of the functions which access it.

Static Variables

Another class of local variable is the static type.

A static can only be accessed from the function in which it was declared, like a local variable.The static variable is not destroyed on exit from the function, instead its value is preserved, and becomes available again when the function is next called. Static variables are declared as local variables, but the declaration is preceeded by the word static.

Static Variable is declared as follows

static int counter;

Static variables can be initialised as normal, the initialisation is performed once only, when the program starts up.

Constants

C constant is usually just the written version of a number.

For example 1, 0, 5.73, 12.5e9. We can specify our constants in octal or hexadecimal, or force them to be treated as long integers.

* Octal constants are written with a leading zero - 015.

* Hexadecimal constants are written with a leading 0x - 0x1ae.

* Long constants are written with a trailing L - 890L.

Character constants are usually just the character enclosed in single quotes; 'a', 'b', 'c'. Some characters can't be represented in this way, so we use a 2 character sequence.

'\n' -> Newline

'\t' -> Tab

'\\' -> Backslash

'\'' -> Single Quote

'\0' -> NULL (used automatically to terminate a string)

String Constants are enclosed within double quotes.

................................................................................................................


Arrays

An array is a collection of variables of the same type.Individual array elements are identified by an integer index. Index begins at zero and is always written inside square brackets.

Arrays are declared as follows,

int temp[20];

Arrays can have more dimensions, in which case they might be declared as

int temp_2d[20][5];

int temp_3d[20][5][3];

When passed as an argument to a function, the receiving function need not know the size of the array. So for example if we have a function which sorts a list (represented by an array) then the function will be able to sort lists of different sizes. The drawback is that the function is unable to determine what size the list is, so this information will have to be passed as an additional argument.As an example, here is a simple function to add up all of the integers in a single dimensioned array.

int add_array(int array[], int size)
{ int i;
int total = 0;
for(i = 0; i < size; i++)
total += array[i];
return(total);
}

No comments: