c - Attribution in first line on switch statement make a error in gcc -
why code doesn't work unless remove comment?
#include <stdio.h> int main(){ int num = 5; switch(num){ case 5: //printf(""); int = 1; printf("%d", num+another); break; } }
gcc returns error:
prog.c: in function ‘main’: prog.c:7:13: error: label can part of statement , declaration not statement
thanks.
the reason outlined here: why can't variables declared in switch statement?
as workaround, can this:
#include <stdio.h> int main(){ int num = 5; switch(num){ case 5:; //printf(""); int = 1; printf("%d", num+another); break; } }
include semicolon after case workaround.fool compiler thinking statement follows , not declaration. taken here
Comments
Post a Comment