#include <stdio.h>
#define MAX 5
static int data = 10;
int increment(void *);
int increment(void *ptr)
{
int *p = ptr;
static int data =15;
for (int loop=0; loop < MAX; loop++)
{
data++;
}
return data;
}
int main(void)
{
printf("%d\n", data++);
static int data = 25;
for (int loop=0; loop < MAX ; loop++)
{
data++;
}
printf("%d\n", data++);
data = increment(&data);
printf("%d\n", data++);
return 1;
}
I understand that, static remains in memory till the end of the program ends. Then in above code there is a global static int data
and same static int data
in main. In Increment function, I understand that it has a local scope.
According to me it should give multiple definition error. But it didnt given. Why?
How program identifies that here I have to take global data
and here I have to take it main data
?
static int data
gets hidden by the function-scopestatic int data
s from the point of the function-scope declaration onwards. They are different variables.loop
variables in different scopes as well, but also if it's done in the same function. Variables declared in deeper nested scopes hides the variables declared in the outer scopes.