What is Variable in C Language ?
Variable is data name that are stored in the
memory. The characteristics of a
variable are given below:
(i) It has a name.
(ii) It is capable of storing values.
(iii) It provides temporary
storage.
(iv) It is capable of changing its value during program execution.
A
variable have two values that is rvalue and lvalue. The meaning of rvalue is
the real value of the variable and the lvalue is the location value of the
variable where the variable is saved.
Example-: x=25,
y=30,a,b,c;
100 102 104 106 108
25
|
30
|
x a y b c
Here x is a variable and the rvalue of x is 25 and the
lvalue is =100 same as the rvalue of y is 30 and the lvalue is 104.
Declaration of variables in C Language
In order to
use a variable in C, we must first declare it specifying which data type we
want it to be. The syntax to declare a new variable is to write the specifier
of the desired data type- followed by a valid variable identifier.
For example: Data type variable_name ;
int a ;
float mynumber ;
These are two valid declarations of variables. The first
one declares a variable of type int with the identifier a. The second one declares a variable of type float with the identifier mynumber. Once declared, the variables a and mynumber can be used within the rest of their scope in
the program.
If you are going to declare more than one variable of the
same type, you can declare all of them in a single statement by separating
their identifiers with commas. For example:
int a, b, c;
Initialization of variables in C Language :
When
declaring a regular local variable, its value is by default undetermined. But
you may want a variable to store a concrete value at the same moment that it is
declared. In order to do that, you can initialize the variable. There are two
ways to do this in C:
The first one, known as c-like, is done by appending an
equal sign followed by the value to which the variable will be initialized:
Data-Type variable = initial_value
;
For example, if we want to declare an int variable called
a initialized with a value of 0 at the moment in which it is declared, we could
write: int a = 0;
No comments:
Post a Comment