今日、私は1つの興味深いものを見つけました。gotoラベルの後に変数を宣言できないことを知りませんでした。
次のコードをコンパイルする
#include <stdio.h>
int main() {
int x = 5;
goto JUMP;
printf("x is : %d\n",x);
JUMP:
int a = 0; <=== giving me all sorts of error..
printf("%d",a);
}
次のようなエラーが発生します
temp.c: In function ‘main’:
temp.c:7: error: expected expression before ‘int’
temp.c:8: error: ‘a’ undeclared (first use in this function)
temp.c:8: error: (Each undeclared identifier is reported only once
temp.c:8: error: for each function it appears in.)
さて、その背後にある論理は何ですか?switchのcaseステートメント内に変数を作成できないと聞きました。JUMPはgotoステートメントと同じスコープ(私の場合はmain関数のスコープ)内にあるので、ここではスコープは問題ではないと思います。しかし、なぜこのエラーが発生するのですか?