Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Variable declaration inside a switch statement.

1 view
Skip to first unread message

Srinu

unread,
Oct 21, 2007, 7:00:49 AM10/21/07
to
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.

Dave Dunfield

unread,
Oct 21, 2007, 7:27:00 AM10/21/07
to
>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

----------------------------------------------------------------------------------------------
PS: GCC warns of "unrechable code at beginning of switch statement"

--
dave06a@ Low-cost firmware development tools: www.dunfield.com
dunfield. Classic computer collection: www.classiccmp.org/dunfield
com Some stuff I have for sale: www.dunfield.com/sale

abhy

unread,
Oct 21, 2007, 7:40:53 AM10/21/07
to

Can u tell me what weird results it gives ?

Keith Thompson

unread,
Oct 21, 2007, 2:32:46 PM10/21/07
to
abhy <abhijit...@gmail.com> writes:
> On Oct 21, 4:00 pm, Srinu <sinu.nayak2...@gmail.com> wrote:
>> 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?
>
> 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.)

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

karthikbalaguru

unread,
Oct 22, 2007, 3:32:55 AM10/22/07
to
On Oct 21, 4:00 pm, Srinu <sinu.nayak2...@gmail.com> wrote:

This is a famous question in interviews :):)

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

Karthik Balaguru

CBFalconer

unread,
Oct 21, 2007, 1:41:29 PM10/21/07
to

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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

CBFalconer

unread,
Oct 21, 2007, 1:43:38 PM10/21/07
to
abhy wrote:
> Srinu <sinu.nayak2...@gmail.com> wrote:
>
... snip ...

>
>> What, if any, the C standard says about it?
>
> Can u tell me what weird results it gives ?

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

abhy

unread,
Oct 22, 2007, 4:45:05 PM10/22/07
to

Hi Karthik

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

Eric Sosman

unread,
Oct 22, 2007, 4:56:06 PM10/22/07
to
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.
*/
...
}

--
Eric....@sun.com

Srinu

unread,
Oct 23, 2007, 2:28:04 AM10/23/07
to
Dear friends,

Thanks a lot for your valuable suggestions.

Srinu.

David Thompson

unread,
Nov 4, 2007, 6:02:22 PM11/4/07
to
On Sun, 21 Oct 2007 11:27:00 GMT,
Dave.D...@use.techsupport.link.on.my.website (Dave Dunfield)
wrote:

> >switch(x)
> >{
> >int y=2;
>
> >case 1:
> >printf("%d", y);
> >}

> 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

0 new messages