Scope Rules

F

fctk

are the following rules correct in C89/C90?

----

SCOPE RULES
1) the scope of an identifier declared inside a block is the block in
which it is declared;
2) when you have nested blocks (extern block and internal block), an
identifier declared in the external block is known also in the internal
block, while an identifier declared in the internal block is not known
in the external block; (*)
3) when you have parallel blocks (first block and second block), an
identifier declared in the first block is not known in the second block.

(*)
- for variable names, an identifier declared in the external block is
known also in the internal block only if it is not re-declared in the
internal block
- for function names, an identifier declared in the external block
cannot be re-declared in the internal block

----

these rules are not written in any book; i wrote them myself. i'd only
want to know if they are correct.

thanks.
 
B

Ben Pfaff

fctk said:
are the following rules correct in C89/C90?

They are approximately correct for declarations of objects that
occur within a block. They don't correctly cover scopes of other
entities (such as macro names, macro parameters, argument names
in function prototypes, structure, union, or enumeration members,
or label names), nor do they cover declarations outside blocks,
nor do they precisely define where a scope begins.
SCOPE RULES
1) the scope of an identifier declared inside a block is the block in
which it is declared;
2) when you have nested blocks (extern block and internal block), an
identifier declared in the external block is known also in the internal
block, while an identifier declared in the internal block is not known
in the external block; (*)

The terms "internal" and "external" are already used for linkage,
so it's better to use the words "inner" and "outer" here, as the
Standard does.
3) when you have parallel blocks (first block and second block), an
identifier declared in the first block is not known in the second block.

If the objects have external linkage and the same name then they
are the same object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top