0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C言語 グローバル変数の正しい使い方

Last updated at Posted at 2021-08-24

グローバル変数の使い方

結論から言えば、使用する場所の1つで定義をして、ヘッダーファイルで宣言するのが良いようです。

グローバル変数の規則

  • 規則1 同名の strong symbol が複数存在してはならない。
  • 規則2 同名の strong symbol と weak symbol が存在する場合、strong symbol を選ぶ。
  • 規則3 同名の weak symbol が複数存在する場合は、任意の1つを選ぶ。

symbol のタイプ

この記事の範囲ではシンボルはグローバル変数のことだと考えても大丈夫です。

symbol のタイプ 詳細
strong symbol 関数および、初期化済みグローバル変数 => 定義( defined )
weak symbol 初期化されていないグローバル変数 => 宣言( declared )

これは、ストレージが割当られているかで判断されています。

グローバル変数の使い方

他の場所でstrong symbolとして定義されると意図しない値が設定されてしまう危険性があるため、使用したい場所で定義して、ヘッダーでexternで宣言しておきます。

ヘッダーでexternを使用する意義としては、明示的に宣言のみすることができるためです。予め定義を別ファイルで行っているため、規則3が適応されて定義が意図しない場所で起こることを防ぐことができます。

コードによる具体例

foo.c
/* foo.c */
# include <stdio.h>
# include "foo.h"

int x = 100;

int main()
{
	printf("x = %d\n", x);
	f();
	printf("x = %d\n", x);
	return 0;
}
bar.c
/* bar.c */
# include "foo.h"

void f()
{
	x = 200;
}
foo.h
/* foo.h */
extern int x;

void f(void);

出力結果

$ gcc foo.c bar.c && ./a.out 
x = 100
x = 200

参考

コンピュータ・システム 第七章
記憶域クラス指定子 - cppreference.com
c - How do I use extern to share variables between source files? - Stack Overflow

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?