Variable declaration inside a switch statement.

S

Srinu

Hi all,

If we compile the below piece of code, it gets compiled. But gives
weird result.

switch(x)
{
int y=2;

case 1:
printf("%d", y);
}


What, if any, the C standard says about it?

Srinu.
 
D

Dave Dunfield

Hi all,
If we compile the below piece of code, it gets compiled. But gives
weird result.
switch(x)
{
int y=2;
case 1:
printf("%d", y);
}

By strange co-incidence I received this same question from a co-
worker only last week (He thought it was a compiler bug) - this
was my answer:

----------------------------------------------------------------------------------------------------
Dear Dave!

I don't think this is a serious problem (I guess we never did it)
however local variables defined inside switch brackets are not
initialized properly.
What do you think about this?
void test_test_test(void)
{
UINT c = 0;

switch(c)
{
UINT t = 30000;
case 0:
default:
printf("\r\nMust be 30000: %d",t);
break;
}
}

Hi <name removed>,

Thats a really weird thing to do, however it is NOT a bug!

From K&R-2, page 223:

"Initialization of automatic objects is performed each time the
block is entered at the top, and proceeds in the order of the
declarators. If a jump into the block is executed, these
initializations are not performed."

A switch statement is by definition a jump into it's block,
the block is never entered at the top, and therefore your
initialized never gets executed.

An automatic declaration with initialization does two things,

1) Reserve space for the variable - this is normally done
at entry to the function (the compiler works out the minimum
footprint for all blocks at compile time and generates the
reservation at function entry.

2) Code is generated to initialize the variable when the block
is entered. This logically occurs at the point in the source
code where the declaration occurs. In the case of your
switch, any other statement positioned where your declaration
is would not execute either!

Regards,
Dave
 
A

abhy

Hi all,

If we compile the below piece of code, it gets compiled. But gives
weird result.

switch(x)
{
int y=2;

case 1:
printf("%d", y);

}

What, if any, the C standard says about it?

Srinu.

Can u tell me what weird results it gives ?
 
C

CBFalconer

Srinu said:
If we compile the below piece of code, it gets compiled. But
gives weird result.

switch(x) {
int y=2;

case 1: printf("%d", y);
}

What, if any, the C standard says about it?

What you fail to realize is that initialization of an automatic
variable requires the generation of code. That code has to go
where the "int y = 2;" statement appears. There is no reason for
the switch statement to transfer control to that code, so y is
uninitialized (or worse) when the printf is executed.
 
K

Keith Thompson

abhy said:
Can u tell me what weird results it gives ?

Please don't use silly abbreviations like "u" for "you". This isn't a
chat room. Take the time to spell out simple words.

Yes, showing the actual questionable output is almost always a good
idea. In this particular case, though, it's unnecessary. The
variable y is uninitialized when it's printed; any output is possible.
(Actually, the behavior is undefined, so in principle *anything* is
possible, but it will most likely print some arbitrary value of type
int.)
 
K

karthikbalaguru

Hi all,

If we compile the below piece of code, it gets compiled. But gives
weird result.

switch(x)
{
int y=2;

case 1:
printf("%d", y);

}

What, if any, the C standard says about it?

This is a famous question in interviews :):)

A switch statement never enters at the top.
So, your initialization never gets executed.

Karthik Balaguru
 
A

abhy

... snip ...



u hasn't posted in c.l.c for some time.

Hi Karthik

I didn't get the meaning of your answer " A switch statement never
enters at the top. "..?
Please explain.
 
E

Eric Sosman

abhy wrote On 10/22/07 16:45,:
[...]
I didn't get the meaning of your answer " A switch statement never
enters at the top. "..?
Please explain.

He means this:

switch (x) {
/*
* Nothing here will ever be executed,
* because `switch' proceeds directly
* to the chosen case or to the end of
* the entire block if no case is chosen.
*/

case 42:
/*
* This is the first piece of code that
* the `switch' can ever execute.
*/
...
}
 
D

David Thompson

From K&R-2, page 223:

"Initialization of automatic objects is performed each time the
block is entered at the top, and proceeds in the order of the
declarators. If a jump into the block is executed, these
initializations are not performed."

A switch statement is by definition a jump into it's block,
the block is never entered at the top, and therefore your
initialized never gets executed.
Right.

An automatic declaration with initialization does two things,

1) Reserve space for the variable - this is normally done
at entry to the function (the compiler works out the minimum
footprint for all blocks at compile time and generates the
reservation at function entry.
IPSYM the maximum of the amounts required by any 'stack' of nested and
hence concurrently alive subblocks, each of which (per subblock and
hence stack) is fixed at compiletime, except VLAs as below.
2) Code is generated to initialize the variable when the block
is entered. This logically occurs at the point in the source
code where the declaration occurs. In the case of your
switch, any other statement positioned where your declaration
is would not execute either!
.... in C89. In C99 declarations can occur after statements, and the
initializations occur (only) when the declaration is 'executed'.

Except for VLA types. They may be and probably are _allocated_ (only)
when the declaration is 'executed', and jumping 'past' the declaration
(formally, into the scope) is a Constraint Violation.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top