Afficher un message
Vieux 21/10/2007, 19h32   #6
Keith Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Variable declaration inside a switch statement.

abhy <abhijitkrao283@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) kst-u@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"
  Réponse avec citation
 
Page generated in 0,04880 seconds with 9 queries