in turboc++
--------------------------------------------------------------------------------------
//program to understand static variable .
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
void function();
int main()
{
function();//function calling
function();
getch();
return 0;
}
void function()//function body line
{
int a=4;//local variable, it can not be accessed from outsides. change this with static int a=4.
printf("a=%d\n",a);//printing value
a=a+1;
}
/* note:
It gives
a=4
a=4
where as it has to be 5 second time.
The computer can not retain its value on second calling.It loses its content (5) and prints same 4.
---------------------------
where as if we take static int a=4
the increased value remains there in memory untill the program is terminated.
We get
a=4
a=5
*/
--------------------------------------------------------------------------------------
//program to understand static variable .
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
void function();
int main()
{
function();//function calling
function();
getch();
return 0;
}
void function()//function body line
{
int a=4;//local variable, it can not be accessed from outsides. change this with static int a=4.
printf("a=%d\n",a);//printing value
a=a+1;
}
/* note:
It gives
a=4
a=4
where as it has to be 5 second time.
The computer can not retain its value on second calling.It loses its content (5) and prints same 4.
---------------------------
where as if we take static int a=4
the increased value remains there in memory untill the program is terminated.
We get
a=4
a=5
*/
-------------------------------------------------------------------------------------
in codeblocks:
-----------------------------------------------------------------------------------------------------
//program to understand static variable .
#include <stdio.h>
#include <stdlib.h>
void function();
int main()
{
function();//function calling
function();
return 0;
}
void function()//function body line
{
int a=4;//local variable, it can not be accessed from outsides. change this with static int a=4.
printf("a=%d\n",a);//printing value
a=a+1;
}
/* note:
It gives
a=4
a=4
where as it has to be 5 second time.
The computer can not retain its value on second calling.It loses its content (5) and prints same 4.
---------------------------
where as if we take static int a=4
the increased value remains there in memory untill the program is terminated.
We get
a=4
a=5
*/
No comments:
Post a Comment