switch why?

V

vim

#include<stdio.h>
int main()
{
int a=10;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
defa1ut:
printf("NONE\n");
}
return 0;
}
My expectation :------The output is NONE.
But the output is different why?
 
V

Vladimir Oka

vim opined:
#include<stdio.h>
int main()
{
int a=10;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
defa1ut:
printf("NONE\n");
}
return 0;
}
My expectation :------The output is NONE.
But the output is different why?

Different in what way?

You probably meant:

case 1:
...
case 2:

`int`s are quite a different beast from `char`s.

Also, it's `default`.
 
P

pemo

vim said:
#include<stdio.h>
int main()
{
int a=10;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
defa1ut:
printf("NONE\n");
}
return 0;
}
My expectation :------The output is NONE.
But the output is different why?
But the output is different why?

Does it say 'Syntax Error'?
 
R

Roberto Waltman

pemo said:
Does it say 'Syntax Error'?

"defa1ut:", althought different from "default:", is a valid label. I
would expect a warning about "unreachable code", but no syntax error.
(I have not compiled the OP's sample)
 
P

pemo

Roberto said:
"defa1ut:", althought different from "default:", is a valid label. I
would expect a warning about "unreachable code", but no syntax error.
(I have not compiled the OP's sample)

LOL, - nicely spotted
 
E

Eric Sosman

vim wrote On 05/15/06 11:21,:
#include<stdio.h>
int main()
{
int a=10;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
defa1ut:
printf("NONE\n");
}
return 0;
}
My expectation :------The output is NONE.
But the output is different why?

Y'know, I was struck by the artificiality of the
problem in your earlier thread about "problem with
initialization" but decided to give a straight answer
anyhow. Now I see that you're just a troll who's found
a C puzzle book somewhere.

vim, there's an old saying about jokes: Use it once
and you're a wit; use it twice and you're a half-wit.
I suggest you quit before you get denormalized.
 
C

Clever Monkey

vim said:
#include<stdio.h>
int main()
{
int a=10;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
defa1ut:
Should be
def1at3:
or
flatulate:
printf("NONE\n");
}
return 0;
}
My expectation :------The output is NONE.

Just change your expectation to match reality. The output *should* be "42".
But the output is different why?
Gnomes or sprites. It's hard to tell, since both of them are known to
be l33t hax0rs. Consult your Tome of Magickal Computation.
 
K

Kenneth Brody

Roberto said:
pemo said:
vim wrote: [...]
defa1ut:
printf("NONE\n");
[...]
"defa1ut:", althought different from "default:", is a valid label. I
would expect a warning about "unreachable code", but no syntax error.
(I have not compiled the OP's sample)

If you turn up the warning level on your compiler (assuming that your
compiler actually has that ability) you may get a warning to appear.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 
K

Keith Thompson

vim said:
It will compile correctly.But the answer is not the expected one

*What* will compile correctly?

Read <http://cfaj.freeshell.org/google/>.

If you want to post puzzles, that's ok, but please let us know that
they're puzzles rather than actual code you're having problems with.
Some of us might be willing to provide help if you need it, but not be
interested in working on a puzzle.
 
C

Clever Monkey

Keith said:
*What* will compile correctly?

Read <http://cfaj.freeshell.org/google/>.

If you want to post puzzles, that's ok, but please let us know that
they're puzzles rather than actual code you're having problems with.
Some of us might be willing to provide help if you need it, but not be
interested in working on a puzzle.
Speaking of which, I'm not usually interested in "puzzles" since I have
my own problems to solve at work (for better or for worse, I am the C
guy by attrition -- none of the young coders are taught it in school
anymore), but are any of the C puzzle books worth the time?

To be more clear, is the purpose to highlight places coders tend to be
weakest and shine some lights into the darker corners?

This is not a spurious question. At least, I hope it's not. I'm
reading "Expert C Programming" by van der Linden right now and finding
that my understanding of the language is certainly deepening.

Of course, I like van der Linden's chatty style and appreciate the
humour. Are any of these puzzle books similar in this manner?
 
C

cbmanica

Clever said:
Speaking of which, I'm not usually interested in "puzzles" since I have
my own problems to solve at work (for better or for worse, I am the C
guy by attrition -- none of the young coders are taught it in school
anymore), but are any of the C puzzle books worth the time?

If you want some help, recent Georgia Tech graduates will know at least
some C.
To be more clear, is the purpose to highlight places coders tend to be
weakest and shine some lights into the darker corners?

Well, I can certainly imagine mistyping "default" and spending more
time than I'd like finding it :)
 
M

Martin Ambuhl

vim said:
#include<stdio.h>
int main()
{
int a=10;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
defa1ut:
printf("NONE\n");
}
return 0;
}
My expectation :------The output is NONE.
But the output is different why?

Some immediate possibilities:
1) You misspelled "default"
2) Your implementation uses a character encoded in which either '1' or
'2' has an encoding of 10.
 
K

Keith Thompson

vim said:
#include<stdio.h>
int main()
{
int a=10;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
defa1ut:
printf("NONE\n");
}
return 0;
}
My expectation :------The output is NONE.
But the output is different why?

If you're going to try to fool us by misspelling "default", at least
spell it as "defau1t" rather than "defa1ut".

Did you *really* expect the output to be "NONE", or did you already
know the answer before you posted the puzzle?
 
N

Neil

Roberto said:
You mistyped "default" (with a lower case 'L') as "defau1t" (with the
digit '1')


That is twice you took the time to type "But the answer is not the
expected one"
What did you expect?
What did you get?
How do you expect anyone to help?

Did you know that '1' != 1 ?
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top