あらきけいすけのメモ帳

あらきけいすけの雑記帳2

C言語の仕様に「グローバル変数」という名前は出てこない

授業の資料作成で調べ物関連のメモ。C99の仕様書 "ISO/IEC 9899:1999 - Programming languages C" を参照していたら「グローバル変数」という語は全く無かった;その代わり "scope" (6.2.1), "linkage" (6.2.2) という語が丁寧に定義されている。いわゆる「グローバル変数」に相当する部分を拾い読みする:

If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which
terminates at the end of the translation unit. (6.2.1 Scopes of identifiers #4)

関数の定義のうち関数本体(function body)も波括弧で括られた複文 "block" なので、"any block" で「そのファイルのすべての関数の外」が含意されるようだ。

An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage. (6.2.2 Linkages of identifiers #1)

"linkage" とはメモリ上で同じ場所にあるものを読み(書き)できること。

If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage. (6.2.2 Linkages of identifiers #3)

変数のスコープがそのソースコードファイルの全体になる場合に、その変数は "internal linkage" を持つ。

定義の合わせ技を読み取るのは難しい。