473,383 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Output of the following code

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
16 1658
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
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
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
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
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
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
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
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
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
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
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
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
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
>>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 (40°39.22'N, 111°50.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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: H.S. Art | last post by:
Hi folks, I have a sript that runs very well. I do my output to the python shell with the print command. That works well, too. print 'Error occured ' + str(i+1) No I want to have my output...
6
by: Wescotte | last post by:
I'm writing a tiny php app that will log into our bank of america account and retrieve a file containing a list of checks that cleared the previous day. The problem I'm running into is when I...
5
by: Tom Lam lemontea | last post by:
Hi all, This is my very first post here, I've seriously tried some programming on C, and shown below is my very first program(So you can expect it to be very messy) that I wrote after I've learned...
22
by: Jaspreet | last post by:
I was recently asked this question in an interview. Unfortunately I was not able to answer it and the interviewer made a decision on my C strengths (or weekness) based on this single question and...
2
by: Will Rickards | last post by:
In my web application there is an interactive report. Then there need to be a printable version a pdf. So I found this java tool csstoxslfo and the java fop tool from apache that will take my...
0
by: IamtheEvster | last post by:
Hi All, I am currently using PHP 5 and MySQL 5, both on Fedora Core 5. I am unable to call a MySQL stored procedure that returns output parameters using mysql, mysqli, or PDO. I'm having a...
6
by: c676228 | last post by:
Hi everyone, I wrote a store procedure that fetch one row data in the database based on the parameter value I entered. After I created the store procedure, the store procedure code looks like...
8
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for...
3
by: MatsL | last post by:
Hi, This is seriously driving me crazy, could anyone explain to me why neither of these doesn't produce XHTML compliant output (it is being called in Render() btw): output.WriteLine("<img...
4
by: Warren Peace | last post by:
'Tis my first time trying to install PHP on my Win XP machine... Apache/2.2.3(Win32) PHP/5.2.0 Main problem is that with my lack of PHP experience, I'm not sure if my syntax is incorrect, or if...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.