|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
>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 |
|
|
|
#3 (permalink) |
|
Messages: n/a
Hébergeur: |
On Oct 21, 4:00 pm, Srinu <sinu.nayak2...@gmail.com> wrote:
> 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 ? |
|
|
|
#4 (permalink) |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 (permalink) |
|
Messages: n/a
Hébergeur: |
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. -- 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 |
|
|
|
#6 (permalink) |
|
Messages: n/a
Hébergeur: |
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" |
|
|
|
#7 (permalink) |
|
Messages: n/a
Hébergeur: |
On Oct 21, 4:00 pm, Srinu <sinu.nayak2...@gmail.com> wrote:
> 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 |
|
|
|
#8 (permalink) |
|
Messages: n/a
Hébergeur: |
On Oct 21, 10:43 pm, CBFalconer <cbfalco...@yahoo.com> wrote:
> 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. > > -- > 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 fromhttp://www.teranews.com Hi Karthik I didn't get the meaning of your answer " A switch statement never enters at the top. "..? Please explain. |
|
![]() |
| Outils de la discussion | |
|
|