関連する C プログラムは次のとおりです:</p>
#include <stdio.h>
void testifbarisvisible();
int main()
{
void bar(int);
bar(1);
testifbarisvisible();
}
void testifbarisvisible()
{
bar(2);
}
void bar(int x)
{
printf("functionbar\n");
}
gcc の出力は次のとおりです。
% gcc -std=c99 -c /tmp/notfilescope.c
/tmp/notfilescope.c: In function ‘testifbarisvisible’:
/tmp/notfilescope.c:14:2: warning: implicit declaration of function ‘bar’
/tmp/notfilescope.c:7:7: note: previous declaration of ‘bar’ was here
/tmp/notfilescope.c:14:2: error: incompatible implicit declaration of function ‘bar’
/tmp/notfilescope.c:7:7: note: previous implicit declaration of ‘bar’ was here
7 行目のステートメントを削除すると、出力は次のようになります。
% gcc -std=c99 -c /tmp/notfilescope.c
/tmp/notfilescope.c: In function ‘main’:
/tmp/notfilescope.c:8:2: warning: implicit declaration of function ‘bar’
/tmp/notfilescope.c: At top level:
/tmp/notfilescope.c:17:6: warning: conflicting types for ‘bar’
/tmp/notfilescope.c:8:2: note: previous implicit declaration of ‘bar’ was here
gcc のバージョンは次のとおりです。
% gcc --version
gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2)
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
gcc の 2 つの出力の違いについて混乱しています。
これは gcc のドキュメントからのものです。「ブロック内の外部変数と関数の宣言は、宣言を含むブロックにのみ適用されます。つまり、同じ場所にある他の宣言と同じスコープを持ちます。」
したがって、7 行目の関数宣言は 14 行目の関数呼び出しとは関係がないと思います。しかし、結果は意見が間違っていることを示しています。それらはすべて関数 'bar' の暗黙的な宣言ですが、そのうちの 1 つはエラー (関数 'bar' の互換性のない暗黙的な宣言) につながり、もう 1 つは警告 ('bar' の競合する型) につながります。
この質問は私を長い間混乱させてきました。誰かが私を助けることができますか?