0
#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?

5
  • 4
    The file-scope static int data gets hidden by the function-scope static int datas from the point of the function-scope declaration onwards. They are different variables.
    – Ian Abbott
    Commented Jul 8 at 11:21
  • It's not an error to have a variable with the same name as another (at a different scope). The language allows it. It commonly is a sign of a problem though. Commented Jul 8 at 11:23
  • Actually, I should have wrote block scope in my comment above, not function scope.
    – Ian Abbott
    Commented Jul 8 at 11:30
  • As mentioned by others, C allows you to declare variables with the same name in different scopes. That's what allows you to use the 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. Commented Jul 8 at 11:41
  • scoping, visibility and lifetime are different. Both have lifetime the whole program execution lifetime, but one is scoped at global while the other is scoped inside main/increment. A variable is visible as soon as it is declared until the end of the scope. A variable declared in a inner scope hides a one declared with the same name in any outer scope. Commented Jul 8 at 14:42

1 Answer 1

5

From C17, 6.2.1 (Scope of identifiers), paragraph 4:

… If an identifier designates two different entities in the same name space, the scopes might overlap. If so, the scope of one entity (the inner scope) will end strictly before the scope of the other entity (the outer scope). Within the inner scope, the identifier designates the entity declared in the inner scope; the entity declared in the outer scope is hidden (and not visible) within the inner scope.

OP's code has three different static int data variables with different scopes.

  • The first one declared outside a function has file scope and its scope begins at the point of declaration. (Emphasis mine.)
  • The second one is declared within the increment function and has block scope extending from the point of declaration to the end of the containing block. Within this scope, the local variable has the inner scope and the global variable has the outer scope for the identifier (within a namespace). The variable with the outer scope is hidden by the inner scope, so the only way to access it would be via a pointer or via a call to a function where the hidden variable is in scope.
  • The third one is declared within main function and has block scope extending from the point of declaration to the end of the containing block. Again, the variable with the outer scope is hidden by the inner scope.

The use of data within the main function before the local declaration of static int data denotes the global data variable with file scope. Other uses of data within the main function after the local declaration of static int data denote the local data variable with block scope.

Not the answer you're looking for? Browse other questions tagged or ask your own question.