Skip to Main Content or Page Contents
Variables are the names that refer to sections of memory into which data can be stored.
To help you think of this as a picture, imagine that memory is a series of different size boxes. The box size is memory storage area required in bytes.
TIPS:
Before you can use a variable you must declare it. Decide what type of data is going to be stored in variable. The main categories of data are:
integers | Whole numbers e.g. 0, 1, 99, 12345 |
floating point numbers | Numbers that have a fractional part, they have a decimal point e.g. 12.543, 0.5, 1.00. Also referred too as real numbers. |
characters | A single character e.g. a, Z, 3, # etc. Note: Characters are stored as a number that is the ASCII code for that character. See ASCII codes |
strings | Strings can't be declared as a string. They are regarded as an array of characters, and are dealt with later. |
Some of these data categories are split into more than one data type as per table 3.1 below. 'C' Numeric data types. |
Variable Type | Keyword | Range | Storage in Bytes |
Character | char | -127 to 127 | |
Unsigned character | unsigned char | 0 to 255 | |
Integer | int | -32,768 to 32,767 | |
Unsigned integer | unsigned int | 0 to 65,535 | |
Short integer | short | -32,768 to 32,767 | |
Unsigned short integer | unsigned short | 0 to 65,535 | |
Long integer | long | -2,147,483,648 to 2,147,483,647 | |
Unsigned long integer | unsigned long | 0 to 4,294,967,295 | |
Single precision floating point | float | 1.2E-38 to 3.4E38, approx. range precision = 7 digits. | |
Double precision floating point | double | 2.2E-308 to 1.8E308, approx. range precision = 19 digits. | |
The main variable types that you will use most are char, int, float. |
int age;
float amountOfMoney;
char initial;
The scope of a variable is
The scope of a variable depends on where it is declared.
Variables can be declared
Declared before the start of the main( ) function
Scope: In the small
programs that are used in these tutorials the scope is
any where in the program, this includes main and all the
other functions.
Life Span: While the program is running.
Global variables are automatically initialised to 0 (zero),
but get into the practice of initialising ALL variables.
It is usually better to avoid the use of global variables.
When static is used with a global variable it indicates that the variable is local to the current file. This is more advanced and static global variables are not used in these beginners tutorials.
Declared within a function including the main() function.
The declaration is placed after the { start brace
of any function including main, and before any function
statements.
Scope of a local variable
is limited to the function it is declared in.
Life Span:Local variables are destroyed when a
function is exited, and a new one is created when a function
is visited again, they exist in memory on a temporary basis. This temporary basis can be made permanent by the
use of the static keyword. See below
Example of declaring an integer variable
named aVariable within the function someFunctionName
Local variables MUST be assigned a value to initialise the variable before they are used.
someFunctionName( ) { int aVariable; /* rest of function code */ }
If the programmer would like the value of the variable to be
remembered when the function is revisited then that variable
must be declared as static. E.g. static int aVariable =
1;
In the example the initialisation to 1 occurs only on the first
execution of this line of code, it is disregarded on further
executions of the line.
static has a different meaning when used with global variables.
See above.
Declared within the { } braces of a block of code.
The declaration is placed after the { (start
brace of the block of code) and before any statements in
that block.
Scope: of a local local variable is limited to the
braces of the portion of code it is declared in.
Life Span:Local local variables are destroyed when the
end } brace is reached.
Assigning a value into a VariableIn the following When you assign a value into a variable, the previous value in that variable is overwritten and therefore destroyed. Variable Declaration and Initialising Examplesint age = 21, houseNumber = 0; Note: When initialising a variable, also known as assigning a value to a variable do not think of = (the assignment operator) being the same as a mathematical = (equals), because it is not.
Imagine that you wish to swap the values that are held in 2
variables named x and y after reading the above you will understand
that the following will NOT work. The result of the above is that both x and y will now contain the original value of y. Incrementing and decrementing a variableWhen you remember how assignments are made makes it easier
to understand this mathematically impossible, but very
common code used in programming. Code explanationLooking at this in more detail let's add another line of code that assigns num the value 4. num = 4; num = num + 1; The value that is being assigned into num is 5, i.e. the current value of num which is 4 and then 1 is added to it. Simply put the above code increments the value of num. The reverse of the above is to decrement num with this
code ShortcutsIncrementing, decrementing and doing calculations
on a variable is a very common programming task and C has quicker
ways of writing the code. The code is rather cyptic in appearence
|