Data Types

A variable is just a named area of storage that can hold a single value (numeric or character). C is very fussy about how we create variables and what we store in them. It demands that we declare the name of each variable that we are going to use and its type, or class, before we actually try to do anything with it.

In this section we are only going to be discussing local variables. These are variables that are used within the current program unit/function in a later section we will looking at global variables - variables that are available to all the program's functions.

There are five basic data types associated with variables:

int - integer: a whole number.
float - floating point value: ie a number with a fractional part.
double - a double-precision floating point value.
char - a single character.
void - valueless special purpose type which we will examine closely in later sections.
Note: all C's variables must begin with a letter or a "_" (underscore) character.

Integer Number Variables

The first type of variable we need to know about is of class type int - short for integer. An int variable can store a value in the range -32768 to +32767.

To declare an int you use the instruction: int variable name;

For example:
int a;

declares that you want to create an int variable called a.

To assign a value to our integer variable we would use the following C statement:

a=5;

The C programming language uses the "=" character for assignment. A statement of the form a=5; should be interpreted as take the numerical value 5 and store it in a memory location associated with the integer variable a. The "=" character should not be seen as an equality otherwise writing statements of the form:

a=a+5;
Decimal Number Variables

As described above, an integer variable has no fractional part. Integer variables tend to be used for counting, whereas real numbers are used in arithmetic. C uses one of two keywords to declare a variable that is to be associated with a decimal number: float and double. They are each offer a different level of precision as outlined below.

float

A float, or floating point, number has about seven digits of precision and a range of about 1.E-36 to 1.E+36. A float takes four bytes to store.

double

A double, or double precision, number has about 13 digits of precision and a range of about 1.E-303 to 1.E+303. A double takes eight bytes to store.

For example:
float total;
double sum;

To assign a numerical value to our floating and double precision variables we would use the following C statement:

total=0.0;
sum=25.34;
Character Variables

C only has a concept of numbers and characters.

To declare a variable of type character we use the keyword char. - A single character stored in one byte.

For example:
char c;

To assign, or store, a character value in a char data type is easy - a character variable is just a symbol enclosed by single quotes.

For example:
c='A'
Assignment Statement

Using a variable means storing something in it. You can store a value in a variable using:

name = value;
For example:
a=5;

Consider four very simple mathematical operations: add, subtract, multiply and divide. Let us see how C would use these operations on two float variables a and b.

add
subtract
multiply
divide

a+b
a-b
a*b
a/b

Note that we have used the following characters from C's character set:

+ for add
- for subtract
* for multiply
/ for divide

simple calculation

a=5/3

The answer depends upon how a was declared. If it was declared as type int the answer will be 1; if a is of type float then the answer will be 1.666.

Two points to note from the above calculation:

C ignores fractions when doing integer division, when doing float calculations integers will be converted into float.

For example of another simple program using int and printf

#include
main()
{
int a,b,average;
a=22;
b=6;
average = ( a+b ) / 2 ;
printf("Here ");
printf("is ");
printf("the ");
printf("answer... ");
printf("\n");
printf("%d.",average);
}