Is this well defined behaviour or is it undefined / somehow else defined which foo
(data type or identifier) sizeof
will be operating on ?
C does not have explicit scope resolution, so identifiers (variable names, typedef names, struct names, etc etc) can be reused and overridden when a new scope is opened. When an identifier is reused, the previous context held by that identifier is no longer visible.
In your particular code, the scope of the typedef
is global, so the typedef is visible everywhere in your compile package. However, you open a new scope with the function declaration, and in that new scope you define a variable that uses the same identifier as the typedef
. Now, that identifier refers to the variable instead of the type; meaning, until the scope of the variable ends (the end of the function), the typedef
is completely hidden.
Recall that C is compiled linearly, so you could do something like this as a way around the shielding that occurs:
#include
typedef int foo;
int main()
{
printf ("%zu\n", sizeof (foo)); /* #1 */
char foo;
printf ("%zu\n", sizeof foo); /* #2 */
return 0;
}
At point #1, note that the scope of the variable char foo
has not yet opened since the compiler hasn't reached its declaration. (All the compiler will do is allocate the space on the stack for the variable).
So the usage of foo
at that point is still in reference to the globally defined typedef
.
By the time you hit #2, the variable is declared and the lifetime of the variable is formally started, meaning the identifier is now in use for a different entity. It shields the current block scope (started by the function declaration) from the global definition of foo
.
This is well-document behavior; there is a draft of the C standard up online, but the published standard has to be purchased. The draft says in section 6.2.1:
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. source
Note that this isn't magical or anything...this is all done at compile-time. The compiler has a table of identifiers and the things they reference, and new scopes create new tables for these. It so happens that at point #1 in the code above, the compiler hasn't yet populated the table with the new char foo
(this is due to the linear compilation). So when it translates the first printf
line, it looks through all the active scopes to find the identifier foo
, and sees the typedef
, and uses that. At the second printf
, it looks through all the active scopes and finds a more recent use of the identifier foo
and uses that.