Variable in C
Variable in C
Introduction:
When a
person sits in a chair then it takes some physical space. Like that in a
programming concept when a variable is declared it takes some logical memory
space . So we can say that a variable is a memory location which is used to
store some data values. Someone can also say that a variable is a place holder
because it holds some memory. At different times during program execution, a
variable can take different data values. But at a single time the variable
cannot take many values instead of single values. A variable name should be
given by the programmer in such a way that it is easy to execute the program.
Tips to remember: Try to choose a simple variable name .So that
it easy to perform the operation/implementation.
Declaration
of variable:
The
declaration of a variable consist of two parts, i.e., one is data types and
other is variable name. Data types determine what types of data values the
variable will hold. It may determine integer type values, float type, char type
or double type values. The variable name means the programmer has given the
name of the variable which should be simple. The declaration of a variable is
necessary because it can be used later in a program.
The syntax of declaring a single variable is:
data-type
variable_name;
The syntax of multiple variable in one
statement is:
data-type
variable1, variable2 ________ variablen-n;
Initialization
of variable:
When a
programmer declares the variable it automatically assigns the undefined values.
The undefined values may be positive or negative integer values depending on
the computer and which are called garbage values or uninitialized values. When
the programmer assigns some values to that variable then it is called initialization
of variables. So initialization of variable means giving values to variable.
The syntax of initialization of variable is:
data-type
variable_name=constants;
Rules of
naming a variable:
The
programmer cannot give any name to the variable. The name of the variable
follows some specific rules and regulations followed by C compiler. The
variable name is the combination of letters, digits, and underscores, character
according to specific conditions.
The conditions are,
1. The variable name
can begin with underscore(_), letter(it may be a lower case letter or upper
case letter).
Examples:_Total;
or Total; or total; or TOTAL;
2. The length of a
variable should not exceed 31 characters. But many compilers allowed normally
maximum eight characters.
3. The variable
names are case sensitive, i.e., upper case and lower case alphabets are
different, e.g., number is not
same as NUMBER.
4. Keywords are not
allowed, e.g., for, if, while, do while.
Example:
Wrong: int for;
Correct: int student_id;
5. While space/blank
spaces are not allowed.
Example:
Wrong: int student id;
Correct is: int student_id;
6. Special
characters e.g. %, $, #,@ are not allowed.
Example:
Wrong: int #student_id;
Correct is: int student_id;
|
Example:
A program to add two number in C.
#include
<stdio.h>
#include
<conio.h>
void main
()
{
clrscr ();
int a, b,
sum;
printf (“
Enter a number a”);
scanf (“%d”,&a);
printf (“
Enter a number b”);
scanf (“%d”,&b);
sum = a+b;
printf (“
Sum of two number is %d”,sum);
getch();
}
OUTPUT
Enter a
number a 3
Enter a
number b 4
sum of two
number is 7
Leave a Comment