What is a Variable? How to Declare and Assign values to Variables?
https://www.computersprofessor.com/2016/05/what-is-variable-how-to-declare-and.html?m=0
Variable:
Variable is
a named memory location that can store some value unlike constants a Variable may
take different values at different times during execution.
A Variable name can be
chosen by the programmer in a meaningful way. Variable names may consist of letters,
digits & the underscore character.
|
Rules
for declaring
|
They
must begin with a letter.
|
ANSI
standard recognizes a length of 31 characters. However, length should not be
normally more than eight characters, since only the 1st eight
characters are treated as significant by may compilers.
|
Upper case
& lower case letters are different i.e. ‘A’ is not the same as ‘a’ .
|
It
should not be a keyword.
|
White
space is not allowed
|
Declaration
of a
After
designing suitable Variable names, we must declare then to the compiler.
Declaration does (or tells) 3 things.
|
1. It
tells the compiler what the Variable name is.
2. It
specifies what type of data the Variable will hold.
3. The
place of declaration decides the scope of Variable In the program.
The declaration of Variables must be
done before they are used in the program. A Variable can be used to store a value
of any data type.
Syntax: datatype V1, V2,………Vn;
V1,
V2,………Vn are the names of Variable. Variables are separated by
commas. A declration statement must end with a semicolon.
Ex: int number, total;
Float ratio;
|
Assigning
values to Variables:
|
Variables must be given a value after
it has been declared & before it is used in any expression.It can
be done in 2 ways:
1. By
using an assignment
2.
Reading data from keyboard (using scanf( ) statement.)
Assignment statement:
Values can be assigned to Variables
using the assignment operator = as follows.
Syntax:Variable_name = constant;
Ex :
initial_balance = 0;
Balance = 75.84;
yes = ‘x’;
C
permits multiple assignments in one line
Ex: initial_value = 0; final_value =
100;
It is also possible to assign a
value to a Variable at the time the Variable is declared.
Syntax: datatype Variable_name = constant;
Ex :
int final_value = 100;
double balance = 72.82;
The
process of giving initial values to Variables is called initialization.
C
permits the initialization of more than one Variables in one statement using multiple
assignment operators.
Ex :
p = q = r = 0;
The
first statement initializes the Variables p, q & r to 0.
Reading
data from keyboard:
Another
way of giving values to Variable is to input data through keyboard using the scanf
function.
Syntax: scanf (“ control string”, &Variable1, &Variable2, …..);
The ampersand symbol &
before each Variable name is an operator that specifies the Variable name’s address.
Ex: scanf
(“ % d”, & no);
When this statement is encountered by
the computer, the execution stops & waits for the value of the Variable no.
to be typed in. since the control string %d specifies that an integer value
is to be read from the terminal.
|