Afficher un message
Vieux 21/10/2007, 18h41   #4
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable declaration inside a switch statement.

Srinu 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?


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

  Réponse avec citation
 
Page generated in 0,04689 seconds with 9 queries