Variable in C Language

Hello viewer in this page we will discuss with more about the variable with practical knowledge. Till now we have covered the variable declaration rules with some examples in Token section.

For complete understanding the variable we have some more questions like

  • how to declare variable
  • how to initialize variable
  • how to use variable
  • how variable behave differently due to storage classes.

Variable Declaration in C Language- 

To declare a variable we have to follow the syntax of variable declaration given below-

Data_Type Variable_Name;

Data_Type illustrate about the variable storing ability means which kind of value variable can hold and about variable capacity(int variable capacity is of 2 Byte).

Variable_Name illustrate the name of variable or the location name inside the memory through which processor can get the address and fetch the value from that address. It must follow the variable naming rules which we have already covered.

Example: int a;

here int is a data type that illustrate the ability of integer valued and 2 Byte capacity and a is a variable name which hold the address in memory of 2 Byte for storing integer values.

Variable Initialization in C Language-

To initialize variable we have to follow the Syntax of variable initialization in two cases-

1. Initialization at the time of variable declaration-

Data_Type Variable_Name = Value ;

Example: int a=5;

Here value 5 is directly assigned to variable a at the time of variable declaration. 

2. Initialization after Variable Declaration-

Data_Type Variable_Name;

Variable_Name = Value;

Here first we declare a Variable with Variable_Name and then in next instruction we have to assign a value to it.

Example: int a;

a=5;

How to use Variable in C Language-

When we are writing code in C Language or other programming language we have to make variable name must be familiar with that program means meaningful name must be used.
Example: when we are writing a program for simple interest the variable name must be familiar with program like Price, Rate, Time, S_Interest not like a,b,c,d. Name of Variable must be familiar with program although there will be no error by compiler but for good practice every programmer should follow this.

Now let us take an example to understand variable completely by adding two values-

#include<stdio.h>

int main()

{
 
    int a=6,b=7,c; //int is datatype for 2 Byte integer values and a,b and c are integer variable.

    c=a+b;

    printf("c=%d", c);   // processor fetch the value from memory location name is c.

    return 0;

}  

Output:-

c=13

Memory Mapping-

Variable Allocation
variable allocation

c variable name 13 storing value at the assuming address of 65523.

Storage Class in C Language➤ 

No comments:

Post a Comment