C言語のgoto文でerror: label at end of compound statement

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
helloworld
記事: 6
登録日時: 9年前

C言語のgoto文でerror: label at end of compound statement

#1

投稿記事 by helloworld » 8年前

よろしくお願いします。
C言語でgoto文を使うことは外道だと言われてしまいそうですが、
goto文を使った基本的なプログラムを作成したのですが、
エラーメッセージが出てしまします。
このエラーはどういった意味でしょうか?
また、コードを書き直してもらえませんか?
C言語です。C++ではないです。
よろしくお願いします。

コード:

 
#include <stdio.h>↲                                                                                                                         
↲
int main(void) {↲
    int n;↲
    printf(">");↲
    scanf("%d",&n);↲
    if ( n == 0 ) {↲
        goto R;↲
    }↲
    printf("ok\n");↲
    R:↲
 }
 

エラーメッセージ:
error: label at end of compound statement

label

Re: C言語のgoto文でerror: label at end of compound statement

#2

投稿記事 by label » 8年前

ブロックの最後の行にラベルをおいてはいけないのかと。
仕様はめんどくさいので探していません。

hide

Re: C言語のgoto文でerror: label at end of compound statement

#3

投稿記事 by hide » 8年前

名前が質問内容につられてしまう痛恨のミス。
上の回答は hide のものです。

ちなみに、なるべくそのままでそのプログラムを動かすとしたら、
ラベルの下で return 0; でもするか、gotoは忘れましょう。

helloworld
記事: 6
登録日時: 9年前

Re: C言語のgoto文でerror: label at end of compound statement

#4

投稿記事 by helloworld » 8年前

皆様、ご回答いただきありがとうございました‼
R:
の行の下に
return 0;
を付け加えたらコンパイルできました‼
お恥ずかしいです。

勉強になりました。
今後も初歩的なことで質問をしてご迷惑をおかけするかもしれませんが、
何卒よろしくお願いします。
本当にありがとうございました‼

閉鎖

“C言語何でも質問掲示板” へ戻る