CMP EMBEDDED.COM

Login | Register     Welcome Guest  
HOME DESIGN PRODUCTS COLUMNS E-LEARNING CONFERENCES CODE FORUMS/BLOGS NEWSLETTERS CONTACT FEATURES RSS RSS

Scope regions in C and C++



Embedded Systems Design

Declarations and definitions
A declaration is a construct in the source code that introduces one or more names into a translation unit and associates attributes with those names. Alternatively, a declaration might simply redeclare a name introduced by a declaration that appeared earlier in the translation unit.

A declaration might also be a definition. Informally, a definition is declaration that not only says "here's a name", but also "here's all the information the compiler needs to create the code for that name."

For functions and objects, a definition is a declaration that generates storage. It's easy to tell when a function declaration is also a definition--a function definition has a body, which generates storage in the code space. For objects, the distinction is not so simple--it depends on the object's scope, linkage, and initializer. We'll get there in due time.

In C, a struct declaration never generates storage, so C doesn't distinguish struct definitions from other struct declarations. C++ does. In C++, a struct declaration is also a definition if it has a body, as in:


struct widget   // a definition
    {
    ...
    };

It's just a declaration if it lacks a body, as in:


struct widget;  // just a declaration

The C standard uses more complicated verbiage to distinguish these different forms of struct declarations. I prefer C++'s approach.

In both C and C++, all typedef and enumeration constant declarations are also definitions.

Scope regions in C
When the compiler encounters the declaration of a name, it stores that name and its attributes in a symbol table. When the compiler encounters a reference to a name, it looks up the name in the symbol table to find those attributes. Each declared name is visible--can be found by lookup--only within a portion of the translation unit called its scope.

Some programming languages use dynamic scoping, in which name lookup is done at run time and may yield different results depending on the state of the running program. That is not the case with C and C++. Both languages use static scoping and do all name lookup at compile time.

C has four kinds of scope:

• A name has file scope if it's declared in the outermost scope of a translation unit, that is, outside of any function, structure, or union declaration. Its scope begins right after its declaration and runs to the end of the translation unit.

• A name (other than a statement label) has block scope if it's declared within a function definition (including that function's parameter list) or in a brace-enclosed block within that function. Its scope begins right after its declaration and runs to the end of the block immediately enclosing that declaration.

• A name has function prototype scope if it's declared in the function parameter list of a function declaration that is not also a definition. Its scope begins right after its declaration and runs to the end of the parameter list.

• Statement labels, and only statement labels, have function scope. A label can be defined only in the body of a function definition and is in scope everywhere in that body, even before the label has been defined.

For example, in Listing 1:

• Object k and functions f and g have file scope.

• Parameter i in function f and parameter n in function h have function prototype scope.

• Parameter i, objects j and k, and function h, all within function g, have block scope.

• Label done in function g has function scope.

1 | 2 | 3 | 4

Rate this article: Low High
Current rating
  • .
Embedded.com Career Center
Looking for a new job?
SEARCH JOBS

Browse all jobs

SPONSOR
RECENT JOB POSTINGS





 :