Output of the following code

R

Richard Tobin

KIRAN said:
int a = 1;

switch(a)
{
int b = 10;

case 1:
printf("Value of b = %d\n",b);
can anyone explain me why the above code is not printing the value of
b as 10?

Because the initialization is not executed. You go straught to "case
1", missing out the initialization. A good compiler will warn you
about this, though you may have to turn up the warning (and
optimisation) levels.

-- Richard
 
K

KIRAN

Hi all,



#include<stdio.h>

int main(void)
{
int a = 1;

switch(a)
{
int b = 10;

case 1:
printf("Value of b = %d\n",b);
break;

default:
printf("No match for a\n");
break;
}

return 0;
}

can anyone explain me why the above code is not printing the value of
b as 10?I am using VC++ 6.0 and when I run this code I got some
garbage value.


Regards,
Kiran
 
C

Chris Dollin

KIRAN wrote:

#include<stdio.h>

int main(void)
{
int a = 1;

switch(a)
{
int b = 10;

case 1:
printf("Value of b = %d\n",b);
break;

default:
printf("No match for a\n");
break;
}

return 0;
}

can anyone explain me why the above code is not printing the value of
b as 10?

The initialisation is never executed (because you jump past it) and, by
a quirk of C syntax/semantics, the code is legal, so no diagnostic is
required.
 
C

CryptiqueGuy

Hi all,

#include<stdio.h>

int main(void)
{
int a = 1;

switch(a)
{
int b = 10;

case 1:
printf("Value of b = %d\n",b);
break;

default:
printf("No match for a\n");
break;
}

return 0;

}

can anyone explain me why the above code is not printing the value of
b as 10?I am using VC++ 6.0 and when I run this code I got some
garbage value.


When you have a compound statement associated with a switch statement,
any statements that you intend to execute is to be associated with the
desired 'case' label. In this case, int b=10 is not associated with
any 'case' label, so the initialization is skipped, producing some
garbage value when you print the value of 'b'.
 
C

CBFalconer

KIRAN said:
#include<stdio.h>
int main(void) {
int a = 1;

switch(a) {
int b = 10;

case 1:
printf("Value of b = %d\n",b);
break;
default:
printf("No match for a\n");
break;
}
return 0;
}

can anyone explain me why the above code is not printing the value
of b as 10?I am using VC++ 6.0 and when I run this code I got some
garbage value.

Because it never executes the initialization code for b. Move that
statement outside thw switch portion.
 
K

KIRAN

R

Richard Heathfield

KIRAN said:
But K& R says that "An automatic variable declared and initialized in
a block is initialized each time the block is entered. " ( Refer
Section 4.8 Block Structure)

Looks like you have them by the short and curlies. I'd demand my money
back if I were you.
 
C

Christopher Benson-Manica

Richard Heathfield said:
KIRAN said:
Looks like you have them by the short and curlies. I'd demand my money
back if I were you.

I'm unable to find any errata for the original K&R, but since nothing
similar appears in the errata for the second edition, I can only
assume that you actually made a very subtle suggestion that OP procure
the second edition. I comment because I doubt that this was clear to
the OP.
 
R

Richard Heathfield

Christopher Benson-Manica said:
I'm unable to find any errata for the original K&R, but since nothing
similar appears in the errata for the second edition, I can only
assume that you actually made a very subtle suggestion that OP procure
the second edition.

No, I was actually suggesting that K&R2 is incorrect. The OP's quote is
indeed from K&R2 (page 84), and K's text seems to me to be wrong for a
compound statement that forms the body of a switch statement.

This is forgiveable, of course - my money-back suggestion was intended
to be light-hearted - but it's still an error, AFAICS.

Conflicting views, anyone?
 
R

Richard Tobin

Richard Heathfield said:
No, I was actually suggesting that K&R2 is incorrect. The OP's quote is
indeed from K&R2 (page 84), and K's text seems to me to be wrong for a
compound statement that forms the body of a switch statement. [...]
Conflicting views, anyone?

If you really want one: I suppose one could reserve the term "enter"
to mean "enter at the top" and always use some term such as "jump in
to" otherwise.

-- Richard
 
C

C. Benson Manica

No, I was actually suggesting that K&R2 is incorrect. The OP's quote is
indeed from K&R2 (page 84), and K's text seems to me to be wrong for a
compound statement that forms the body of a switch statement.

My apologies (both for posting from Google and for being wrong -
unfortunately I can do no more than again regret that K&R2 is on my
home desk rather than here). The statement certainly appears to be
incorrect and I suppose I'm merely surprised that it's evaded
inclusion in what I take to be the official errata (at
http://cm.bell-labs.com/cm/cs/cbook/2ediffs.html).
 
C

CryptiqueGuy

Christopher Benson-Manica said:



No, I was actually suggesting that K&R2 is incorrect. The OP's quote is
indeed from K&R2 (page 84), and K's text seems to me to be wrong for a
compound statement that forms the body of a switch statement.

This is forgiveable, of course - my money-back suggestion was intended
to be light-hearted - but it's still an error, AFAICS.

Conflicting views, anyone?

I don't subscribe to your view.

It is all about the interpretation of the phras "block is entered".

Here goes my view, which I think most people would agree.

Having a simple analogy between a house and a block, I think, to say
"enter the house" will evidently mean to *most* people (you migh not
come under that "most"!), that you enter into the house via its main
door.

In this particular case of the compound statement associated with the
switch statement, you "sneak" into the block. So the phrase "block is
entered" does not apply here.
 
C

Clark Cox

I don't subscribe to your view.

It is all about the interpretation of the phras "block is entered".

Here goes my view, which I think most people would agree.

Having a simple analogy between a house and a block, I think, to say
"enter the house" will evidently mean to *most* people (you migh not
come under that "most"!), that you enter into the house via its main
door.

In this particular case of the compound statement associated with the
switch statement, you "sneak" into the block. So the phrase "block is
entered" does not apply here.

Whether you sneak into a house or not, you're still entering it. I'd
argue that anytime you start on the outside of something and then end
up inside that same thing, you have entered it.
 
E

Eric Sosman

Clark Cox wrote On 06/19/07 12:44,:
[...]
In this particular case of the compound statement associated with the
switch statement, you "sneak" into the block. So the phrase "block is
entered" does not apply here.

Whether you sneak into a house or not, you're still entering it. I'd
argue that anytime you start on the outside of something and then end
up inside that same thing, you have entered it.

Or it has eaten you. (I've known code like that ...)
 
C

CBFalconer

KIRAN said:
But K& R says that "An automatic variable declared and initialized
in a block is initialized each time the block is entered. " ( Refer
Section 4.8 Block Structure)

But the block is never 'entered'. Control transfers to the 'case'
statements, which are really labels, and thus I always pull them
out to the left margin. See my editing of your code fragment
above.
 
C

Chris Torek

KIRAN said:
... I was actually suggesting that K&R2 is incorrect. The OP's quote is
indeed from K&R2 (page 84), and K's text seems to me to be wrong for a
compound statement that forms the body of a switch statement.

Indeed, it is also incorrect for blocks entered via "goto":

extern void use(int);

void foo(void)
{
goto inner;
{
int x = 42;
inner:
use(x); /* ERROR */
}
}

The value of "x" at the call to use() is indeterminate, and the
behavior of the code is undefined.

(This is equivalent to the switch statement method, since "switch"
is merely a goto in disguise. This is why case labels are labels,
in the same way that goto labels are labels, and why Duff's Device
works.)
This is forgiveable, of course - my money-back suggestion was intended
to be light-hearted - but it's still an error, AFAICS.

Conflicting views, anyone?

Perhaps DMR or BWK will see this, and add it to the errata.
 
R

Richard Heathfield

Chris Torek said:
Indeed, it is also incorrect for blocks entered via "goto":
Perhaps DMR or BWK will see this, and add it to the errata.

I've dropped a line to dmr alerting him to this problem (and to another
minor bug, or perhaps I should say "alleged bug", on p115).
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top