the same id in the same scope refers to different objects

Andrew Makhorin mao@gnu.org
Mon Sep 4 13:14:00 GMT 2017


On Mon, 2017-09-04 at 10:33 +0100, Jonathan Wakely wrote:
> On 3 September 2017 at 11:43, Andrew Makhorin wrote:
> > Why x used to initialize y identifies not the same object as x used in
> > printf though x is the same identifier within the same block scope?
> 
> Because that's how scope and visibility works in the C programming
> language. A name is only in scope after it has been declared. Get a
> good book on C programming.
> 

Thank you for your reply.

I think the ISO C Standard is a good book. The subsection 6.2.1
"Scopes of identifiers" says:

   Different entities designated by the same identifier either have
   different scopes, or are in different name spaces.
   [...]
   Two identifiers have the same scope if and only if their scopes
   terminate at the same point.

In my example program x used in printf has block scope which overlaps
the file scope where other x is declared. Thus, the behavior of my
example program is undefined, because x is used to initialize y before
it is assigned an initial value (this is similar to the case when a
goto jumps into a block bypassing possible initialization of variables
declared within that block).




More information about the Gcc-help mailing list