初級 › error: expected declaration or statement at end of input の原因と直し方 › パターン2
error: expected declaration or statement at end of input の原因と直し方
波括弧{}の対応が崩れ、閉じ忘れがあるときに出るエラーです。
1 #include <stdio.h> 2 3 int main(void) { 4 int x = 5; 5 if (x > 0) { 6 printf("positive\n"); 7 _ ^ 8 return 0; 9 }
main.c:9:1: error: expected declaration or statement at end of input
空欄を埋めてコンパイル・実行を通す
if文の{を閉じないと、それ以降の行がすべてif文の中に取り込まれてしまい、最後の}がmain関数ではなくif文を閉じることになります。結果としてmain関数自体が閉じられないままファイルが終わります。
次の問題