Prashant | Wed, 10 Jun, 2020 | 174
There are diffrent types of varivbles in C.
they are:-
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.
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
}
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.
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
}
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);
}
1. Introduction And Getting Started With C
3. Why We Should Use C Language
4. Applications Of C Programming
5. Basic Rules For Writting C Program
8. Tokens In C
9. Difference Between Int Main And Void Main
11. 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