gotoのラベルの置く場所によってエラーが出るか出ないかが変わります。
なぜでしょうか?
OS:win7, compiler:mingw-w64
コード抜粋
goto lbl;
switch(s){
case 1:{
lbl:
printf(label);
break;
}
error: a label can only be part of a statement and a declaration is not a statement
goto lbl;
switch(s){
case 1:{
lbl:
printf(label);
break;
}
error: a label can only be part of a statement and a declaration is not a statement
#include <stdio.h>
int main(void) {
const char* label = "";
int s = 0;
goto lbl;
switch(s){
case 1:{
lbl:
printf(label);
break;
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define GetRand (((unsigned int)((float)rand() / 32768.0)) * 10)
int main(void) {
const char* label = "print\n";
srand((unsigned int)time(NULL));
int s = GetRand;
if(s == 3) goto lbl;
while(0){
switch(s){
case 1:{
lbl:
int ed = s * GetRand;
printf("end(%d, %d)", s, ed);
goto end;
break;
}
case 0:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:{
printf(label);
printf(" = %d\n", label);
}
default:{
printf("error");
exit(1);
break;
}
}
s = GetRand;
}
end:
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define GetRand (((unsigned int)((float)rand() / 32768.0)) * 10)
int main(void) {
const char* label = "print\n";
srand((unsigned int)time(NULL));
int s = GetRand;
if(s == 3) goto lbl;
while(0){
switch(s){
case 1:{
lbl:
printf(""); // これ追記しただけです
int ed = s * GetRand;
printf("end(%d, %d)", s, ed);
goto end;
break;
}
case 0:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:{
printf(label);
printf(" = %d\n", label);
}
default:{
printf("error");
exit(1);
break;
}
}
s = GetRand;
}
end:
return 0;
}