Code:
switch(a)
{
case 1: // Not here.
break;
Actually, you can declare a variable in that particular place (at least according to Dinkumware), because it's the first case statement in the switch. But we know what you mean -- in general, a case statement doesn't constitute the beginning of a block, so you normally can't declare anything there.
Code:
if (b) // Not here.
Why not?
Of course, that wouldn't be very useful, because you couldn't use x at all . . .
To state this slightly more formally: variable declarations must be the first code inside a block. A block is enclosed with {}, and also occurs when you put only one statement after an if or else clause, or a loop.
Code:
if(1) int x;
do
int x;
while(0);
while(1) int x;
Of course, that's rarely (if ever) useful.
One point that should be clarified is that a switch statement's block begins at the {, not at each individual case label.
Also note that any code other than a declaration will end the "beginning" of the block and make it impossible to declare a variable. These are all declarations, and thus can be intermingled with variables:
Code:
void function(void);
typedef int number;
struct whatever {};
enum { MAX = BUFSIZ };
int x = y;
int x = yes();
double f = pow(2, 8);
These are not declarations, they are statements:
Code:
x = 0;
if(x) {}
x = y;
x = yes();