N
Neil Zanella
Hello,
Unlike in pre-C99 versions of C where variables can only be defined at the
beginning of blocks, C99 allows variables to be defined in arbitrary
places inside blocks. However, gcc 3.2.2 seems to reveal that there
are still places where this is illegal. For instance, consider the
following code snippet.
int main(void) {
int i = 0;
switch (i) {
case 0:
char ch;
break;
}
}
Here the compiler complains with the error:
hello.c: In function `main':
hello.c:6: parse error before "char"
It seems like declaring a variable inside a switch statement is illegal.
Anyone know why this is the case. After all, the following compiles fine
with gcc 3.2.2:
int main(void) {
int i = 0;
switch (i) {
case 0: {
char ch;
break;
}
}
}
The only difference is the introduction of the block within the C
statement. Anyone know of similar cases where it is illegal to declare
variable in places where we would expect it to be legal in C99? Also,
can anyone explain the reason for the compiler complaint?
Thanks,
Neil
Unlike in pre-C99 versions of C where variables can only be defined at the
beginning of blocks, C99 allows variables to be defined in arbitrary
places inside blocks. However, gcc 3.2.2 seems to reveal that there
are still places where this is illegal. For instance, consider the
following code snippet.
int main(void) {
int i = 0;
switch (i) {
case 0:
char ch;
break;
}
}
Here the compiler complains with the error:
hello.c: In function `main':
hello.c:6: parse error before "char"
It seems like declaring a variable inside a switch statement is illegal.
Anyone know why this is the case. After all, the following compiles fine
with gcc 3.2.2:
int main(void) {
int i = 0;
switch (i) {
case 0: {
char ch;
break;
}
}
}
The only difference is the introduction of the block within the C
statement. Anyone know of similar cases where it is illegal to declare
variable in places where we would expect it to be legal in C99? Also,
can anyone explain the reason for the compiler complaint?
Thanks,
Neil