The Following Are The Types Of Variables In C Language.
Local Variable
Global Variable
Static Variable
Automatic Variable
We Will Write int, float, In The Place Of 'void'
'void' is returntype which is of empty space.
we will declare local variable inside the function or block
ends with return 0;
Example
All The Programs I Explained Before This Topic.
A Variable That Is Declared Inside The Function Or Block.
It Must Be Declared At The Start Of The Block
Syntax
returntype fuctionname()
{
datatype variable;
}
A Variable That Is Declared Outside The Function Or Block.
Any Function Can Change The Value Of Global Variable.
It Is Available To All The Functions.
Example
#include
#include
int a=10;
void fuction1() //Declaring The Function (Global Variable)//
{
printf("a=%d\n",a);
}
voidmain() // This Is Main //
{
clrscr();
}
function1(); // Here In Main We Are Calling The Function Which Is Declared Out Side Main //
a+=10; // a=10,a+=10 means a=10+10 a=20 //
printf("a=%d",a);
getch();
}
A Variable That Is Declared With Static Keyword.
It Returns The Value Between Multiple Function Calls.
Example
#include
#include
void fuction1() // Declaring The Function (Static Variable) //
{
int x=10;
static int y=10;
x=x+1;
y=y+1;
printf("x=%d\t y=%d\n",x,y);
}
void main()
{
clrscr();
fuction1(); // Here In The Main Function We Are Calling The Static Variable //
fuction1();
fuction1();
fuction1();
getch();
}
x=11 y =11
x=11 y =12
x=11 y =13
x=11 y =14
All Variables In C That Is Declared Inside The Block Is Automatic Variables By Default. We Can Explicitly Declare Variable By Using Auto Keyword.
Read the full article