Is the static keyword associated with a scope?

In C89, does the static affect scope?

My software guide told me:

"The variable marked with statics at the top of the file technically does not have a global scope. Static is a scope specifier and also a storage keyword. A concept that encompasses character visibility, although visibility is automatically compiled to have a storage duration built-in in almost all languages. By this I mean that you cannot name a region that also does not determine the duration of storage in C / C ++. The expression region is not defined by the user, and in C / C ++ - l-param and r- param area block is completely lexical in C / C ++ by user-defined functions Function The scope is completely lexical in C / C ++ by user-defined bodies and declarations.The file area does not technically exist in C / C ++, since globals and the scope of the module depend on depending on the scope of the lexicon, the keyword defined using static in C / C ++ is defined, another change in the vocabulary of the scope of the access rule, but visibility remains based on the Global scope module is the default in C / C ++, when not applied etsya another area and lexical controlled keyword extern. The problem is that static is not just a scope specifier as a keyword. This is a scope specifier and a memory keyword. "

I'm confused. I always thought that static was related to the visibility between translation units and the duration of the variable storage. Both of them are not related to the region. Is that not so? Is a static / volumetric relation in C ++?

+6
source share
4 answers

A variable marked as static at the top of the file does not technically have a global scope.

A "global scope" is not a concept that exists in C. The correct term is the scope of a file. In C ++, there is a similar concept called the global namespace. It seems that people with overtime combined these two terms.

Static is a scope specifier as well as a storage keyword.

static not a scope qualifier; it is a storage class specifier. static may affect the duration of communication and storage, but not the scope.

Scope is a concept that encompasses character visibility, although visibility is automatically compiled to have a storage duration internally linked to almost all languages.

The scale has nothing to do with the visibility of symbols (in the sense of a linker). Linkage (hence this is called link ). The second sentence is gibberish.

By this, I mean that you cannot name an area that also does not determine the duration of storage in C / C ++.

This sentence also makes no sense. Consider a local static variable in the block area. It has a static storage duration, although the block area defines automatic storage variables.

The scope of the expression is not defined by the user, but in C / C ++ - l-param and r-param

"Volume of expression" does not make sense. "l-param" and "r-param" are also meaningless words.

Skipping the part about the “lexical” and “module”, because it has zero meaning.

The problem is that static is not just a scope specifier as a keyword. It is an area qualifier and a memory keyword.

Again, static has nothing to do with scope or memory. Using this simplified explanation, all other aspects of the duration, volume, and initialization of the storage are largely ignored, so it just doesn't work.

+6
source

Section 6.2.1 of the C11 standard defines what “scope” means:

For every other object that is indicated by an identifier, the identifier is visible (i.e., can be used) only in the area of ​​the program text, called its area. Different objects denoted by the same identifier either have different scopes, or are in different namespaces. There are four types of areas: function, file, block, and function prototype. (A function prototype is a declaration of a function declaring the types of its parameters.)

Section 3.1.2.1 of the C89 / 90 specification is almost identical:

The identifier is visible (that is, it can be used) only inside the area of ​​the program text, called its area. There are four types of areas: function, file, block, and function prototype. (A function prototype is a declaration of a function that declares the types of its Parameters.)

Thus, there is no such area as a global scope, at least up to level C. An identifier defined outside any function or block has the scope of the file, and the presence or absence of static does not affect this, only the symbol reference , which is something completely different (but which may lead your date or confuse the term "scope").

+3
source

Your informant is confused. static does not affect volume.

The file area is incorrect because you can create multi-level translation units using the #include directives or other hypothetical implementation-specific features. Global reach is also incorrect, because a program may consist of several translation units. Modules are still not part of the language.

static may affect communication, but this is a different concept for scope.

+1
source

The static has several uses in C. For example, in the C source file at the top, you can:

 #include <stdio.h> // .. other includes and comments and stuff int globallyVisibleInt = 0; // a variable that other compilation units can see static int fileVisibleInt = 0; // a variable visible in the file from this point 

The fileVisibleInt variable is created and initialized while the application is loading, but if you try to access it from some other compilation unit, you will receive an error message from the linker when you try to link.

You can also use static in a function to create a variable that will exist and maintain state.

 int myFunc (int k) { static int mySavedInt = 0; // create and initialize a permanent int variable if (k > 10) { mySavedInt = k; // use the variable to save a value for the next time function is called } else if (mySavedInt > 22) { // do some stuff } } 

The point at which the static variable is visible to another source in the file is at the point where it appears in the source file, and its visibility is determined by various scope rules. For example, static defined in a function is visible only in this function or if static used in some other, more limited area, such as if or for , then it is a constant variable that is only visible in this area.

There are various phrases that are general, but not necessarily technically accurate, or something you will find in the standards. For example, the phrase "global reach" means that the thing is visible outside the compilation unit. Under the "module" I would take a function. For most everyday activities, language weakness works great.

In C ++, static may be slightly different depending on whether you use C ++ constructs such as class and using static as a qualifier for methods and members, or if you use its old C method.

See also:

File scope and global scope: C and C ++ .

Why should file scope static variables be initialized to zeros?

Dr. Dobbs: Scopes in C ++ .

0
source

All Articles