§ A variable is a
data name that may be used to store a data value.
§ Unlike constants
that remain unchanged, during the execution of a program, a variable may take
different values at different times during execution.
§ A variable name
can be chosen by the programmer in a meaningful way so as to reflect its
function or nature in the program.
Rules for writing variables:
a)
Variable name can be composed of letters (both
uppercase and lowercase letters), digits and underscore '_' only.
b)
The first letter of a variable should be either a
letter or an underscore. But, it is discouraged to start variable name with an
underscore though it is legal. It is because, variable name that starts with
underscore can conflict with system names and compiler may complain.
c)
There is no rule for the length of a variable.
However, the first 31 characters of a variable are discriminated
by the compiler. So, the first 31 letters of two variables in a program should
be different.
d)
Keyword cannot
be used as a variable.
Declaration of Variables:
After
deciding suitable variable names, we must declare them to the compiler. This
declaration does two things:
i.
It
tells the compiler what the variable name is.
ii.
It
specifies what type of data the variable will hold and also the memory
associated with it.
The
syntax
for declaring a variable is as follows:
datatype v1, v2, …., vn;
where v1, v2, …., vn are the names of
variables.
ü Variables are
separated by commas.
ü A declaration
statement must end with a semicolon.
e.g.
int count;
char
grade;
float
ratio;
Assigning values to Variables:
1)
Using Assignment
Operator ( = )
Values can be assigned to variables
using the assignment operator = as follows:
variable_name
= constant_value;
e.g.
count
= 0;
grade
= ‘A’;
ratio
= 6.234;
The assignment statement implies that
the value of the variable on the left of the ‘equal sign’ is set equal to the
value of the quantity on the right.
year = year + 1;
means that the ‘new value’ of year is
equal to ‘old value’ of year plus 1.
It is also possible to assign a value to
a variable at the time the variable is declared.
datatype
variable_name = constant_value;
e.g.
int
sum = 0;
NOTE: The process of
giving initial values to variables is called initialization.
2)
Reading data
from Keyboard
Another way of giving values to
variables is to input data through keyboard using the scanf() function.
The general format of scanf() is as
follows:
scanf(“Format
String”, &v1, &v2,…,&vn);
the format string contains the format of
data being received. The ampersand (&) before each variable name is an
operator that specifies the variable name’s address.
e.g.
int
number;
scanf(“%d”,
&number);
NOTE: When the scanf() statement is
encountered by the compiler, the execution stops and waits for the value of the
variable to be typed in.
Thus, the use of
scanf() function provides an interactive feature and makes the program “user
friendly”.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.