470,855 Members | 1,143 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,855 developers. It's quick & easy.

switch why?

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?

May 15 '06 #1
15 1611
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`.

--

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

May 15 '06 #2
vim wrote:
#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'?
--
==============
Not a pedant
==============
May 15 '06 #3
vim
It will compile correctly.But the answer is not the expected one

May 15 '06 #4
"pemo" <us***********@gmail.com> wrote:
vim wrote:
#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'?


"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)
May 15 '06 #5
On 15 May 2006 08:30:45 -0700, "vim" <vg*****@gmail.com> wrote:
It will compile correctly.But the answer is not the expected one


You mistyped "default" (with a lower case 'L') as "defau1t" (with the
digit '1')
May 15 '06 #6
Roberto Waltman wrote:
"pemo" <us***********@gmail.com> wrote:
vim wrote:
#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'?


"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
--
==============
Not a pedant
==============
May 15 '06 #7


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.

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

May 15 '06 #8
vim wrote:
#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.
May 15 '06 #9
Roberto Waltman wrote:

"pemo" <us***********@gmail.com> wrote:
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:Th*************@gmail.com>
May 15 '06 #10
"vim" <vg*****@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@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.
May 15 '06 #11
Keith Thompson wrote:
"vim" <vg*****@gmail.com> writes:
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.

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?
May 15 '06 #12
Clever Monkey wrote:
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 :-)

May 15 '06 #13
vim wrote:
#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.

May 15 '06 #14
"vim" <vg*****@gmail.com> writes:
#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?

--
Keith Thompson (The_Other_Keith) ks***@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.
May 15 '06 #15
Roberto Waltman wrote:
On 15 May 2006 08:30:45 -0700, "vim" <vg*****@gmail.com> wrote:
It will compile correctly.But the answer is not the expected one


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 ?
May 16 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Rudi Hansen | last post: by
10 posts views Thread by Myster Ious | last post: by
5 posts views Thread by Bryan Parkoff | last post: by
10 posts views Thread by clueless_google | last post: by
65 posts views Thread by He Shiming | last post: by
3 posts views Thread by pgraeve | last post: by
11 posts views Thread by ME | last post: by
12 posts views Thread by | last post: by
11 posts views Thread by =?Utf-8?B?anAybXNmdA==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.