Variables And Data Types
A variable is a temporary storage place for keeping an item of information. This
may then be used or altered later in a program following some calculations.
Each variable holds a specific type of information - for example a whole number, a
decimal number, or a character, or a string (of characters).
The most common types of variables are:
Fundamental/Primary Data Types:
C has the following simple data types (16-bit implementation):
The syntax of the simple type specifier:
Currently the three char data types are guaranteed to be 1 byte in length, but the other
data types are machine architecture dependent. Unsigned can be used with all char and
Identifier names (Declaring Variables):
The C language defines the names that are used to reference variables, functions,
labels, and various other user-defined objects as identifiers.
An identifier can vary from one to several characters. The first character must be a letter
or an underscore with subsequent characters being letters, numbers or under-score.
Correct: Count, test23, High_balances, _name
In-correct: 1 count, hil there, high..balance
In turbo C, the first 32 characters of an identifier name are significant. If two variables
have first 32 characters in common and differ only on the 33rd, TC will not be able to tell
then apart. In C++ identifiers may be any length.
In C, upper and lower case characters are treated as different and distinct.
count, Count, COUNT are three separate identifiers.
To declare a variable in C, do:
var_type list variables;
int i, j, k;
float x, y, z;
Variables are declared in three basic places: inside functions, in the definition of function
parameters and outside of the functions. If they are declared inside functions, they are
called as local variables, if in the definition of functions then formal parameters and out
side of all functions, then global variables.
Global variables are known throughout the program. The variables hold their values
throughout the programs execution. Global variables are created by declaring them
outside of any function.
Global variables are defined above main () in the following way:
short number, sum;
bigsum; char letter;
It is also possible to pre-initialise global variables using the = operator for assignment.
float sum =
0.0; int bigsum
= 0; char letter
This is the same as:
sum = 0.0;
bigsum = 0;
is more efficient.
C also allows multiple assignment statements using =, for example:
a = b = c = d = 3;
which is the same as, but more efficient than:
a = 3;
b = 3;
c = 3;
d = 3;
This kind of assignment is only possible if all the variable types in the statement are the
Variable that are declared inside a function are called ―local variables‖. These variables
are referred to as ―automatic variables‖. Local variables may be referenced only by
statements that are inside the block in which the variables are declared.
Local variables exist only while the block of code in which they are declared is executing,
i.e. a local variable is created upon entry into its block & destroyed upon exit.
Consider the following two functions:
void func1 (void)
x = 10;
void func2 (void)
x = -199;
The integer variable x is declared twice, once in func1 () & once in func2 (). The x in
func1 () has no bearing on or relationship to the x in func2 (). This is because each x is
only known to the code with in the same block as variable declaration.