Question

I've encountered with declaration definition :

A declaration is a definition unless it declares a function without specifying the function’s body

#include <stdio.h>
void foo()
{
    printf("foo\n");

}



int main()
{
    void foo();
    foo();
}

In the 3.3 said

The scope of a declaration is the same as its potential scope unless the potential scope contains another declaration of the same name.

Question 1. Does it mean that in my case when we redeclare foo into the main function the redeclared foo actually denote the entity different from entity denoted by foo declared into the global scope?

Question 2. Why unqualified name lookup resolution gives an entity denoted by "global" foo?

UPD: If we consider the following

#include <stdio.h>
int a=42;

int main()
{
    int a;
    printf("%d\n",a); //Garbage
}

Then there a inside the function scope doesn't denoted to the global a. How to explain it?

Was it helpful?

Solution

1. No, it denotes the global foo(). In main you are redeclaring the name, not defining it. If there were another definition of foo, then it would violated One Definition Rule, and the program would not compile.

2. I guess the first answer also answers this. There is only one definition of foo(), which is in global scope. So unqualified name lookup will refer that one.

EDIT

In the later example, you are defining a in main, which hides the global a. From your first quote:

A declaration is a definition unless it declares a function without specifying the function’s body

a is not a function, so the declaration is also a definition, which hides the previous definition of a.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top