Variables

In C language Variable must be a type and it tells which type of data this variable can hold. Variable is the name of memory location and it can used to store the data. Value of variable may get change in the program and it can be reused many times. This is the way to represent memory location as a symbol which can be easily understood.

The Example of declare variable are given below :

  • int a;
  • float f=34.8;
  • char c=’A’;

Rules for define variable:

  • A variable name should be alphabet, underscore and digits.
  • Variable name start with only alphabet and underscore.It cannot start a variable name with a digit.
  • White space is not allowed in variable name.
  • Variables are case-sensitive.

Valid variable name

  • int a;
  • int _jk;
  • int s89;

Invalid variable name

  • int 5;
  • int s t;
  • int long;

Types of Variables in C

 There are many types of variable in C

  • Local Variable
  • Global Variable
  • Static Variable
  • Automatic Variable
  • External Variable
  •  Local Variable: local variable are declared within the function or block they cannot used outside the function.It must be declared at the start of the block.
    void function(){
    int x=30;// local variable
    }
  • Global Variable: These variable can be accessed from everywhere in the program.global variable is defined outside the function and it is available for all the functions.Any function can change the value of global variable.
    int y=50;//global variable
    void function(){
    int x=30;//local variable
    }
  • Static Variable: Variable is declared with the static keyword is called static variable.A variable that retains its value between multiple function calls.
    void function(){
    int x=30;//local variable
    static int z=30; //static variable
    x=x+1;
    y=y+1;
    printf("%d,%d",x,y);
    }

    In this example, local variable will always print same value whenever function call but the static variable will print the incremented value in each function call.

  • Automatic Variable: All variables are declared inside the block or function are automatic variables by default. We can explicitly declared automatic variable using auto keyword.
    void main(){  
    int x=30;//local variable (also automatic)  
    auto int y=20;//automatic variable  
    }
  • External Variable: You can declare a external variable using extern keyword.we can share a multiple variable files in c sources files using an external variable.
    //mywork.h
    // extern int x=30;//external variable (also global) 
    
    
    program.c
    #include "mywork.h"  
    #include   
    void printValue(){  
        printf("Global variable: %d", global_variable);  
    }

Submit your review
1
2
3
4
5
Submit
     
Cancel

Create your own review

The Technical Funda
Average rating:  
 0 reviews