What is Variable In C Language?

Variable in C language?

·       Variable is nothing but a term used to represent the name assigned to available memory cell in Memory.


 



· We use different types of data in our program and to store them we use memory cell but it’s hard to understand in which memory cell data is stored; therefor to identify and to access specific memory cell we give it a name which is known as Variable name.

·       Value hold by variable is changeable it is not fixed.

·    Variable is nothing but identifier which means variable can be defined as per user wish but following some conventions for it.

·    Whenever we required data we stored at particular memory location; we can access that data/values with the help of variable name.

·   We can perform different type operations like addition, subtraction and other using different variables which hold different values.

·       There are two steps to use variable name:-

o   Variable Declaration:-

§  It is nothing but a declaration of variable which tells compiler that we are using this particular variable in our C program.

§  Syntax - data_type variable_name;

§  Ex – int number;
§  Here we have declared integer type of variable whose name is number.

o   Variable Initialization:-

§  It is nothing but a way of assigning the value to our newly declared variable at the very first time.

§  This way we are putting some value inside our newly   created variable.

§  We can assign or change value of variable many times as we want that’s how variable differ from Constant.

§  We will see constant after variable topics.

§  Syntax:- Data_type Variable_name=value;

§  Ex:- int number=3;

§  Here in above example we are assigning value 3 to variable number (memory cell). 
 



·       There are 6 types of variable in C Language which are given below:-

1.  Local Variable:-Local variable are the variables which are declare inside function body.

§  Ex:- void abc()
{
          Int a=3;   //local variable
}

§  You must have to initialize the local variable before it is used.

2.    Global Variable:-

§  A variable which is declare outside function is called as Global variable.

§  Any function can change values of global variable.

§  Ex :- int value=30;//global variable  
void abc()
{  
int x=1;//local variable  
}  

3.    Static Variable:-

§  We use static keyword to use static variable.

§  Once static variable get initialized by some value it get access by any function and if its value get modified then it will remain with same value last time modified or assign.

§  Ex:- void abc()
{  
int a=1; //local variable  
static int b=7; //static variable  
a=a+1;  
b=b+1;  
printf("%d,%d",x,y);  
}  

4.    Automatic Variable:-

§  All variable we declare in any C Program are Automatic variable by default.

§  We can declare them explicitly using auto keyword.

§  Ex:- void main()
{  
int i=2;//local variable (also automatic)  
auto int j=4;//automatic variable  
}  

5.    External Variable:-

§  If we wanted to share any variable with multiple source file then we have to use External variables.

§  We can declare external variable by declaring them using extern keyword.

§  Ex:-   extra.h
extern int x=10; // external variable

abc.c
#include<stdio.h>
#include “extra.h”

Void abc()
{
 Print(“value of external variable is  = %d”,x);
                                     }





  • You Can Also Visit Youtube Channel TechGeek-Mayur or click on link given below to get Videos on C Language Tutorial.


  • Thanks for visiting our blog. Please follow our page for more updates.

  • If you have any query regarding any topics just put it on Comment section we will cover that query in detail with solution.

Comments

Popular Posts