| Where | Storage | Initial | Storage | Scope /Linkage |
Visibility |
|---|---|---|---|---|---|
| specified | class | value | allocation | ||
| 1. Dating clared | (none) | Yes | Yes | ManBlock scope, | Within same blo , including all |
| in block | auto | No | Yes | no lii nkage | nested blocks, except |
| register | any nested block (and its | ||||
| static | nested blocks) with an identical identifier without extern | ||||
| 2. Declared | extern | No | CASE A: Enclosing scope has identical, visible | ||
| in block | identifier - same scope, linkage, allocation, visibility as the matching identifier | ||||
| CASE B: Otherwise, same as if declared extern outside function (see 7, below) | |||||
| 3. Declared | extern | Yes | ILLEGAL | ||
| in block | declaration | ||||
| 4. Not declared but referenced in block | CASE A: Enclosing scope has identical, visible identifier declared in the same source file prior to the reference - same as if declared extern in block (see 2, above) | ||||
| CASE B: Otherwise, ILLEGAL declaration |
| Which | Storage | Initial | Storage | Scope/Linkage | Visibility |
|---|---|---|---|---|---|
| declaration | class | value | allocation | ||
| 5. First | (none) | Yes | Yes | File scope | Rest of file except any |
| declaration | No | external | block (and its nested blocks) with an identical identifier declared without extern, and blocks it contains and Other files with an identical external linkage identifier declared in them (No other file may allocate (define) an identical external linkage identifier.) | ||
| 6. First | static | Yes | Yes | File scope, | Rest of file except any |
| declaration | No | internal linkage | block (and its nested blocks) with an identical identifier declared without extern, and blocks it contains | ||
| 7. First | extern | Yes | Yes | File scope, | Rest of life except any |
| declaration | external linkage | block (and its nested blocks) with an identical identifier declared without extern, and blocks it contains and Other files with an identical external linkage identifier declared in them (One other file must have an identical external linkage identifier allocated (defined) in it.) | |||
| 8. First | extern | Yes | Yes | Same as if declared outside function | |
| declaration | without extern or static (see 5, above) | ||||
| 9. Second or | Must have same | Same scope, linkage, allocation, and | |||
| later | type and linkage | visibility as first declaration | |||
| declaration | as first declaration |
Feeling a Little C-sick?
C syntax flies in the face of the "say what you mean, mean what you say" principle of programming. The keyword extern is simply an abbreviation for "external" - a word you'd expect to be related to visibility or scope and to mean something like "outside the current context." And you'd expect the other relevant keyword, static, to relate to how storage is managed. Thus, you might read the declaration
extern int x;
to mean the variable x is visible outside the block or file in which it's declared. Likewise, for a declaration without extern (C has no intern keyword), such as
int x;
you'd expect the variable x not to be visible outside the block or file in which it's declared. For variables declared within a function or nested block, this sensible interpretation holds. The problem arises for variables declared outside any function. (In C, such declarations are called external declarations.) In most cases, when the two previous examples appear as external declarations, both the extern and non-extern declarations mean the variable is visible outside the file in which it's declared. (If the extern declaration isn't the first declaration of x, however, and the first declaration of x isn't visible outside the file, the extern declaration also specifies that x isn't visible outside the file - a further complexity in C's approach to visibility.) Some programmers wise in the ways of C might argue, "But both declarations are external declarations, so it is consistent that they both are visible `externally' to the file." Nice try. However, when the declaration
static int x;
is outside any function, it also is an external declaration, yet it defines a variable that is not visible outside the file.
Not only does C's syntax lack consistency, but it also confuses things by using the static storage class keyword to specify visibility. My theory is that Humpty Dumpty was on the original C design team. As he told Alice, "When I use a word, it means just what I choose it to mean - neither more nor less."
There are other confusing cases, as in the second declaration of x below,
static int x;
main( void ) {}
where the use of extern results in internal qExpensive Naked Cheapporn Dating Tips Sexy C - Advice And Porn Warnings For C - Simplified Variae Declarations Tutorials Expensive Naked Cheapporn Dating Tips Sexyf Girls Men eExpensive Naked Cheapporn Dating Tips Sexy C - Advice And Porn Warnings For C - Simplified Variae Declarations Tutorials Expensive Naked Cheapporn Dating Tips Sexyk Russian