473,402 Members | 2,064 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,402 software developers and data experts.

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 1842
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Rudi Hansen | last post by:
I dont seem to be able to find the switch statement in Python. I would like to be able to do switch(var) case 1 : print "var = 1" case 2: print "var = 2"
10
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a...
5
by: Bryan Parkoff | last post by:
C++ programmers and I tried to experience by writing 65536 switch cases. It is too large for Windows XP and other operating system to handle. It looks like that JMP Table obtains 256K bytes for...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
65
by: He Shiming | last post by:
Hi, I just wrote a function that has over 200 "cases" wrapped in a "switch" statement. I'm wondering if there are performance issues in such implementation. Do I need to optimize it some way? ...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
11
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test...
12
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
7
by: Rohit | last post by:
Hi, I am working on a switch module which after reading voltage through a port pin and caterogizing it into three ranges(open,low or high), passes this range to a function switch_status() with...
11
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can switch statements be nested? I've got a large routine that runs off of a switch statement. If one of the switches in switch #1 is true, that enters switch statement #2. Some of the...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.