Simple function with a goto error handler:
void checkFile(char *name)
{
FILE *fp = fopen(name, "r");
if(NULL == fp)
{
fprintf(stderr, "Could not open %s\n", name);
goto end;
}
// use fp...
fclose(fp);
end:
;
}
Notice, if I remove the useless semicolon after end: the function fails to compile.
On GCC:
error: expected primary-expression before '}' token
16 | }
On MSVC:
error C2143: syntax error: missing ';' before '}'
So, I understand that C standard does say that the destination of the goto keyword expects a statement in § 6.8.6.1 p 2:
A goto statement causes an unconditional jump to the statement prefixed by the named label in the enclosing function
However, the error exists just because the label exists; if I remove the goto keyword, the label itself is still treated as an error and won't compile. I read the standard's section on "Labeled statements" (§ 6.8.1) but still didn't find anything that explained this odd constraint.