C言語の静的関数 質問する

C言語の静的関数 質問する

C で関数を静的にする意味は何ですか?

ベストアンサー1

関数を作成すると、static他の翻訳単位から隠蔽され、カプセル化

ヘルパーファイル.c

int f1(int);        /* prototype */
static int f2(int); /* prototype */

int f1(int foo) {
    return f2(foo); /* ok, f2 is in the same translation unit */
                    /* (basically same .c file) as f1         */
}

int f2(int foo) {
    return 42 + foo;
}

メイン.c:

int f1(int); /* prototype */
int f2(int); /* prototype */

int main(void) {
    f1(10); /* ok, f1 is visible to the linker */
    f2(12); /* nope, f2 is not visible to the linker */
    return 0;
}

おすすめ記事