4/2/11

Variables or Identifiers

A variable is a symbol that represent a storage location in the computer’s memory. The information that is stored in that location is called the value of the variable. A given variable can be assigned different values at various places within the program. Thus, the value represented by the variable can change during the execution of the program.

Rules for naming variable
A variable name can be chosen by the programmer in a meaningful way and the rules to be followed are listed below.

1.Variable names must begin with an alphabet or underscore.
2.The first character may be followed by a sequence of letters or digits.
3.There is no limit on the length of the variable, but some compilers recognize only the first eight characters. For example, the variables dataname1 and dataname2 mean the same thing to the compiler.

4.Upper and lower case letters are treated as distinct and different. Though both the cases are allowed, usually C variables are in lowercase. For example, pay, pay are considered as to be three different variable.

5.The variable name should not be a keyword.
6.Special characters such as blank, comma, period, etc. are not allowed in the
variable names.

Example
Interest
Payment
Lastamt-due
Root-1
Flag

Variable Declarations
In C all variables must be declared before their use. A variable is said to be declared, when we indicate
i.The name of the variable and
ii.Its type specifier
(i.e) A declaration of a variable is a statement that gives information about the variable to the C compiler.

Its syntax is
Data-type variable list;

Here the data type is one of the basic or derived type. (i.e) int, long, float, double, struct, array etc.

Each variable declaration line consists of a type followed by a list terminated by a semicolon.

For example, the declaration
Int n;

Tells the compiler two things.
(i) The name of the variable is n
(ii) The variable is type int.

Examples
Int i1, i2, i3;
Short int s1, s2, s3;
Char answer;
Float x, y,z;
Double d1, d2;
Unsigned int m;

Keywords
There are certain reserved words, called keywords, that have standard, pre-defined meanings in C. These keywords can be used only for their intended purpose; they cannot be used as programmer defined variables.
The standard keywords are:
auto
long
break
register
case
return
char
short
const
signed
continue
sizeof
default
static
do
struct
double
switch
else
typedef
enum
union
extern
unsigned
float
void
for
while
goto
gf
int

Some C compilers recognize other keywords. The user is asked to refer a manual to obtain a complete list of keywords.