Scopes of Identifiers in C Programming

0
In C programming, an identifier is a name that can denote various entities such as an object, function, tag, member of a structure, union, or enumeration, typedef name, label name, macro name, or macro parameter. The same identifier can denote different entities at different points in the program. 

Types of Scopes

There are four kinds of scopes: function, file, block, and function prototype. The scope of an identifier is the region of the program text within which it is visible. 
  • Function Scope: A label name is the only kind of identifier that has function scope. It can be used anywhere in the function in which it appears, and is declared implicitly by its syntactic appearance.
  • File Scope: If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope.
  • Block Scope: If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope.
  • Function Prototype Scope: If the declarator or type specifier that declares the identifier appears within the list of parameter declarations in a function prototype (not part of a function definition), the identifier has function prototype scope.

Overlapping Scopes

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.

Special Cases

Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. Any other identifier has scope that begins just after the completion of its declarator.

As a special case, a type name (which is not a declaration of an identifier) is considered to have a scope that begins just after the place within the type name where the omitted identifier would appear were it not omitted.
Tags

Post a Comment

0Comments
Post a Comment (0)