What is Variable? How to Declare and initialize variables? Give examples.

3 years ago
C Programming

Definition of Variable

Variable is an identifier used to name the memory location which holds the value. Variable is an entity whose value can be changed (Not Fixed) or modified during the program execution.

Declaring a Variable

A variable thus has three attributes that are of interest to us: its type, its value and its address and before a C program can utilize memory to store a value or data it must claim the memory needed to store the values for a variable. This is done by declaring variables.

 

Declaring variables is the way in which a C program shows the number of variables it needs, name of the variables, range of values it can represent and how much memory they need.

Within the C programming language, when managing and working with variables, it is important to know the type of variables and the size of these types. Size of the datatypes can be hardware specific – that is, how the language is made to work on one type of machine can be different from how it is made to work on another.

All variables in C are typed. That is, every variable declared must be assigned with certain datatype.

General Syntax for declaring variables:

DataType        Variable-list;

Where,

DataType can be => int, float, char, double, short, unsigned int, long double, etc Variable-list can be => list of variables seperated by comma.

DataType       var1,var2,var3,...,varN;

Example:

int count;         /* It declares a variable count as integer type          */ float average; /* It declares a variable average as floating point type */

int   number_students,   i,    j;   /*    In    this   declaration   it   declares   3 variables, number_students, i and j. */

char code;       /* It declares a variable code as character type */

 

Variables can be declared as

 1)  Local variables:

When variables are declared inside functions as follows they are termed local variables and are visible (or accessible) within the function ( or code block ) only.

For Example,

int main()

{

int i, j;

...

}

In the above example, the variables i and j are local variables, they are created i.e. allocated memory storage upon entry into the code block main() and are destroyed i.e. its memory is released on exit from the block. Therefore i and j are local to main().

2)  Global Variables:

When variables declared outside functions they are termed as global variables and are visible throughout the program. These variables are created at program start-up and can be used for the entire lifetime of the program.

For Example,

int i;

int main()

{ ...   }

 

In this example, i is declared as global variable and it is visible throghout the program.

Initialization or Assignment of a Variable

When variables are declared in a program it just means that an appropriate amount of memory is allocated to them for their exclusive use. This memory however is not initialised to zero or to any other value automatically and so will contain random values unless specifically initialised before use. Hence, variables may need to be assigned and store value in it. It is done using initialzation.

General Syntax for initialization  :-

Initialization of variable is done using assignement operator(=).

variable-name = expression ;

Where,

Expression can be - arithemetic expression, relational expression, logical expression, conditional expression, constant or an identifier that must reduce to a single value.

For Example :-

1)    char ch;

     ch = 'a' ;   /* in this example, a character constant „a‟ is assigned to variabla ch */

2)    double d;

     d = 12.2323 ; /* in this example, a floating point constant is initialized to d */

3)    int i, j,k;

     i = 20 ; j=100;

     k=i+j;        /* in this example, i is initialized to 20, j is initialized to 100 and k is initialized to addition of i and j i.e. 120 */

Combining declaration and initialization of variables

The variables can be declared and initialized at the time of creating i.e allocating memory storage for variables.

Syntax :-                                DataType var-name = constant ;

For Example :-

char ch = 'a' ;

double d = 12.2323 ;

int i, j = 20 ; /* note in this case i is not initialised */

2
Sanisha Maharjan
Jan 20, 2022
More related questions

Questions Bank

View all Questions