Types Of Variables In C

Prashant | Wed, 10 Jun, 2020 | 190

Types of variables in C

There are diffrent types of varivbles in C.

they are:- 

  1. local variable
  2. global variable
  3. static variable
  4. automatic variable
  5. external variable

1. Local variable

Local variables are those variables which are declared inside a function or a block is called local variable.

It must be declared in the starting of a block.

void main(){
 int var=10; // local variable
}

You should always initialise a variable before it is used or it will thorw an error.

2. Global Variable

A variable that is declared outsid of all the blocks or functions are said to be global variables, Also any function can chage the value of global variable as it is not constant, it is just a global variable which can be accessed from any where.

It should always be decalared at the beginning.

int value=20;//global variable  
void function1(){  
int x=10;//local variable  
}  

3. Static Variable

A variable that is declared with the static keyword is called static variable.

It retains its value between multiple function calls.

void function1(){  
int x=10;//local variable  
static int y=10;//static variable  
x=x+1;  
y=y+1;  
printf("%d,%d",x,y);  
}  

If you call this function many times, the local variable will print the same value for each function call, e.g, 11,11,11 and so on. But the static variable will print the incremented value in each function call, e.g. 11, 12, 13 and so on.

4. Automatic variable

All variables in C that are declared inside the block, are automatic variables by default. We can explicitly declare an automatic variable using auto keyword.

void main(){  
int x=10;//local variable (also automatic)  
auto int y=20;//automatic variable  
}  

5. External variable

We can share a variable in multiple C source files by using an external variable. To declare an external variable, you need to use extern keyword.

myfile.h

extern int x=10;//external variable (also global)  

program

#include "myfile.h"  
#include <stdio.h>  
void printValue(){  
    printf("Global variable: %d", global_variable);  
}  

 

 

 

 

 

 

Related Articles :- 

1Introduction And Getting Started With C

2Basic Of C Language

3. Why We Should Use C Language

4. Applications Of C Programming

5. Basic Rules For Writting C Program

6. Keywords In C Language

7. Comments In C Programming

8. Tokens In C

9. Difference Between Int Main And Void Main

10. C Processor And Macros

11. Variables In C

12. Types Of Variables In C

13. Difference Between Local And Global Variable

14. Difference Bwtween Auto / Extern / Static Variable

15. Constant In C

16. How To Access Global Variable Using Extern Keyword In C

17. Exit And Return Staterment In C

18. Print Float Value Upto N Decimals In C Programming

19. How To Print Multiline Message Using Single Printf In C Programming ?

20. What Value Returned By Scanf Function In C Language ?

21. What Value Is Returned By Printf And Scanf In C

22. What Do 'Lvalue' And 'Rvalue' Mean In C/C++?

23. Errors In C Programming ?

0 comments
Leave a comment