Originally Posted by
Salem
The point is for YOU to experiment with calling the same code, where the only difference is the use of the static keyword. In the hope you'll learn something..
I did the two experiment. I printed value with static and without static
Program with static
Code:
#include<stdio.h>
void loop (void)
{
static int count = 0;
printf("Number : %d \n", count++);
}
int main(void)
{
for(int i = 0; i < 5; ++i)
loop();
return 0;
}
Number : 0
Number : 1
Number : 2
Number : 3
Number : 4
If I write static before variable , so every call of the function number would change.
If I don't write static before variable , so every call of the function number would be 0
Code:
#include<stdio.h>
void loop (void)
{
int count = 0;
printf("Number : %d \n", count++);
}
int main(void)
{
for(int i = 0; i < 5; ++i)
loop();
return 0;
}
Number : 0
Number : 0
Number : 0
Number : 0
Number : 0
Originally Posted by
Salem
Seriously, you need to pick up the pace of learning if you ever hope to write a meaningful program.
I wrote this program
Code:
#include <stdio.h>
void loop(void)
{
int a = 0;
static int static_a = 0;
a += 5;
static_a += 5;
printf(" \n a = %d, static_a = %d", a, static_a);
}
int main(void)
{
int i;
for (i = 0; i < 5; ++i)
loop();
return 0;
}
a = 5, static_a = 5
a = 5, static_a = 10
a = 5, static_a = 15
a = 5, static_a = 20
a = 5, static_a = 25
Originally Posted by
Salem
you're bordering on being a troll..
Why do you think that? I do my best to solve problrm