ターン:C言語:エラー:ラベルはステートメントの一部にしかなれず、宣言はステートメントではありません|



Turn C Language Error



オリジナル:https://blog.csdn.net/qq_30242609/article/details/52858115

シーンを復元

簡単なswitchステートメントのデモ



#include int main() { int a=1, b=2, re char c scanf('%c', &c) switch(c) { case '+': re = a + b break case '$': re = a - b re++ break case '#': int other = 3 re = a + b + other break default: printf('Illegal input! ') break } printf('%d ', re) }



エラー理由

新しい変数の定義のswitchステートメント。この例では、他の場合は「#」ステートメントブロックで新しい変数を定義します。
解決

caseステートメントブロックの新しい変数put中括弧を定義するために、このエラーはなくなりました。
コードが変更された後

#include int main() { int a=1, b=2, re char c scanf('%c', &c) switch(c) { case '+': re = a + b break case '$': re = a - b re++ break case '#': { int other = 3 re = a + b + other break } default: printf('Illegal input! ') break } printf('%d ', re) }