By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,965 Members | 2,043 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,965 IT Pros & Developers. It's quick & easy.

Output of the following code

P: n/a
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

Jun 18 '07 #1
Share this Question
Share on Google+
16 Replies


P: n/a
In article <11**********************@g37g2000prf.googlegroups .com>,
KIRAN <ki*****@gmail.comwrote:
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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 18 '07 #2

P: n/a
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.

--
Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Jun 18 '07 #3

P: n/a
On Jun 18, 7:29 pm, KIRAN <kira...@gmail.comwrote:
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'.

Jun 18 '07 #4

P: n/a
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?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.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

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

Jun 19 '07 #5

P: n/a
On Jun 19, 3:10 am, CBFalconer <cbfalco...@yahoo.comwrote:
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?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.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account fromhttp://www.teranews.com
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)

Jun 19 '07 #6

P: n/a
KIRAN said:
On Jun 19, 3:10 am, CBFalconer <cbfalco...@yahoo.comwrote:
<snip>
>Because it never executes the initialization code for b. Move that
statement outside thw switch portion.

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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 19 '07 #7

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrote:
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.
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.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jun 19 '07 #8

P: n/a
Christopher Benson-Manica said:
Richard Heathfield <rj*@see.sig.invalidwrote:
>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.

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?

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 19 '07 #9

P: n/a
In article <G6******************************@bt.com>,
Richard Heathfield <rj*@see.sig.invalidwrote:
>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
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 19 '07 #10

P: n/a
On Jun 19, 10:20 am, Richard Heathfield <r...@see.sig.invalidwrote:
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).

Jun 19 '07 #11

P: n/a
On Jun 19, 7:20 pm, Richard Heathfield <r...@see.sig.invalidwrote:
Christopher Benson-Manica said:
Richard Heathfield <r...@see.sig.invalidwrote:
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.
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?
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.

Jun 19 '07 #12

P: n/a
On 2007-06-19 08:53:28 -0700, CryptiqueGuy <SR**********@gmail.comsaid:
On Jun 19, 7:20 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>Christopher Benson-Manica said:
>>Richard Heathfield <r...@see.sig.invalidwrote:
>>>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.
>>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?

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.

--
Clark S. Cox III
cl*******@gmail.com

Jun 19 '07 #13

P: n/a
Clark Cox wrote On 06/19/07 12:44,:
On 2007-06-19 08:53:28 -0700, CryptiqueGuy <SR**********@gmail.comsaid:
>[...]
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 ...)

--
Er*********@sun.com

Jun 19 '07 #14

P: n/a
>>KIRAN said:
>>>>... 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)
In article <G6******************************@bt.com>
Richard Heathfield <rj*@see.sig.invalidwrote:
>... 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.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Jun 19 '07 #15

P: n/a
KIRAN wrote:
CBFalconer <cbfalco...@yahoo.comwrote:
>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;
} /* edited by cbf */
>>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.

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.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

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

Jun 19 '07 #16

P: n/a
Chris Torek said:
>>>KIRAN said:
>... 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)

In article <G6******************************@bt.com>
Richard Heathfield <rj*@see.sig.invalidwrote:
>>... 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":
<snip>
>
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).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 20 '07 #17

This discussion thread is closed

Replies have been disabled for this discussion.