A variable is a named storage location in the computer’s memory that can be used to store values, it can take different values but one at a time. These values can be changed during execution of the program.
Consider the following example:
Here 6 is stored in a memory location 10905 and a name x is given to it. Then we are assigning a new value 7 to the same memory location x. This would overwrite the earlier value 6, since a memory location can hold only one value at a time. Since the location whose name is x can hold different values at different times x is known as a variable.
Datatype of Variable
A variable in C language must be given a type, which defines what type of data the variable will hold.
It can be:
- char : Can hold/store a character in it.
- int : Used to hold an integer.
- float : Used to hold a single-precision floating point value.
- double : Used to hold a double-precision floating point value.
- void : Represents the absence of type.
C programming language also allows to define various other type of variables which we will cover in upcoming tutorials like Enumeration, Pointer, Array, Structure, Union etc. For this tutorial, let us study only basic variable types.
Rules to name a Variable
The rules for naming variables are same as that for naming identifiers as shown below.
- The name should consist of only uppercase and lower case letters ( A to Z & a to z ), digits ( 0 to 9 ) and underscore sign ( _ ).
- First character should be an alphabet or underscore.
- The name should not be a keyword.
- Since C is a case sensitive, the uppercase and lowercase letters are considered different. For example level, Level and LEVEL are three different variables.
- A variable name may be arbitrarily long. Some implementations of C recognize only the first eight characters, though most implementations recognize 31 characters. ANSI standard compilers recognize 31 characters.
Declaring and Defining a Variable
Declaration of a variable specifies its name and datatype. The type and range of values that a variable can store depends upon its datatype. Declaration does not create a variable; it only refers to a variable name and what type of data the variable will hold, so memory is not allocated at the time of declaration.
Definition creates the variable, so memory is allocated at the time of definition.
Declarations also serve as definitions, except when the declaration contains the
extern
specifier but no initializer. The syntax of declaration of a variable is:
type variable_list;
Here, type must be a valid C data type including char, int, float, double, or any user defined data type etc., and variable_list may consist of one or more variable names separated by commas. Some valid variable declarations are shown here:
int i, j, k;
float salary;
char grade;
long z;
short y;
Here
i
, j
, and k
is a variable of type int
, salary
is a variable of type float
, and grade
is a variable of type char
. The data type long int
can be written as only long
and short int
can be written as only short
. So z
is declared as a variable of type long int
and y
is declared as a variable of type short int
. Since extern
keyword is not used in declaration therefore the declaration is also defined.
Example 1 : Program to show that the declarations also serve as definitions.
#include <stdio.h>
int main()
{
int i, j, k;
float salary;
char grade;
long z;
short y;
printf("\n\t");
printf("\n\tMemory address of i :: %p", &i);
printf("\n\tMemory address of j :: %p", &j);
printf("\n\tMemory address of k :: %p", &k);
printf("\n\tMemory address of salary :: %p", &salary);
printf("\n\tMemory address of grade :: %p", &grade);
printf("\n\tMemory address of z :: %p", &z);
printf("\n\tMemory address of y :: %p", &y);
return 0;
}
When you compile and execute the above program on Visual Studio 2013 IDE it produces following result on Windows 7:
In the above example memory is allocated for variables i, j, k, salary, grade, z and y. As we studied earlier the memory is allocated at the time of definition which means that the declarations also serve as definitions.
Using extern
keyword in declaration
An
extern
declaration is not a definition and does not allocate storage. In effect, it claims that a definition of the variable exists some where else in the program. A variable can be declared multiple times in a program, but it must be defined only once. Following is the declaration of a variable with extern
keyword.
extern int i;
Example 2 : Program to show that the
extern
declaration is not a definition.
#include <stdio.h>
/* variable declaration */
extern int i;
int main()
{
printf("\n\tMemory address of i :: %p", &i);
return 0;
}
This program throws error in compilation as shown below. Because variable "i" is declared but not defined anywhere. Essentially, the variable "i" isn't allocated any memory. And the program is trying to print the address of a variable that doesn’t exist at all.
Example 3:
#include <stdio.h>
/* variable declaration */
extern int i;
int main()
{
/* variable definition */
int i;
printf("\n\tMemory address of i :: %p", &i);
return 0;
}
Output :
This program will compile successfully. Because variable "i" is declared outside main() function and defined in main() function.
Example 4 :
#include <stdio.h>
extern int i=0;
int main()
{
printf("\n\tMemory address of i :: %p", &i);
return 0;
}
Output :
This program will compile successfully because when a variable is only declared and an initializer is also provided with that declaration, then the memory for that variable will be allocated i.e. that variable will be considered as defined. Therefore, as per the C standard, this program will compile successfully and work.
Initialization of Variables
When a variable is declared it contains undefined value commonly known as garbage value. If we want we can assign some initial value to the variable during the declaration itself, this is called initialization of the variable.
Variables are initialized (assigned an value) with an equal sign followed by a constant expression. The general form of initialization is:
variable_name = value;
Variables can be initialized (assigned an initial value) in their declaration. The initializer consists of an equal sign followed by a constant expression as follows:
type variable_name = value;
Some examples are:
int a=10; /* initializing a. */
float x=9.8,y=11.5; /* initializing x and y. */
char ch=’y’; /* the variable ch has the value ‘y’. */
double pi = 3.14159; /* declares an approximation of pi. */
int l,m,n,total=0; /* Only the variable total is initialized. */
Example 5:
#include <stdio.h>
int main()
{
/*variable declaration(here declarations also serve as definitions) */
int a, b;
int c;
float f;
/* actual initialization */
a = 20;
b = 30;
c = a + b;
printf("Value of c : %d \n", c);
f = 80.0 / 3.0;
printf("Value of f : %f\n", f);
return 0;
}
Output :
Difference Between Identifier and Variable
Identifiers are basically names given to program elements such as variables, arrays, functions, or any other user-defined item. While variable is a named storage location in the computer’s memory that can be used to store values.
Identifier | Variable |
---|---|
Identifier is the name given to program elements such as variables, arrays, functions, or any other user-defined item. | Variable is used to name a memory location, which holds a value. |
All identifiers are not variable. | All variables names are identifier. |
Example :
|
Example :
|
No comments:
Post a Comment