LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-24-2005, 06:55 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Rep: Reputation: 45
problematic case statement


Plz take a look to the following code



printf("snmp_fetch_oid_value: To response->errstat einai %ld \n",response->errstat);
switch(response->errstat){
case (SNMP_ERR_TOOBIG):
printf("Too big messange \n");
case (SNMP_ERR_NOSUCHNAME):
printf("No such name idiot \n");
case (SNMP_ERR_BADVALUE):
printf("Bad value \n");
case (SNMP_ERR_READONLY):
printf("Read only value \n");
case (SNMP_ERR_GENERR):
printf("Generic error \n");
default:
printf("Everything is fine \n");
}



After the execution i take the following message....
It seems that case checks all the statements

after execution:

snmp_fetch_oid_value: To response->errstat einai 2
No such name idiot
Bad value
Read only value
Generic error
Everything is fine
 
Old 08-24-2005, 07:24 AM   #2
sind
Member
 
Registered: Jun 2005
Posts: 75

Rep: Reputation: 15
You need to put a break statement after each case block, so that the compiler knows to exit the switch block. Otherwise, execution will "fall" through all of the remaining case blocks to the end of the switch block.

Code:
printf("snmp_fetch_oid_value: To response->errstat einai %ld \n",response->errstat);

switch(response->errstat)
{
    case (SNMP_ERR_TOOBIG):
        printf("Too big messange \n");
        break;

    case (SNMP_ERR_NOSUCHNAME):
        printf("No such name idiot \n");
        break;

    case (SNMP_ERR_BADVALUE):
        printf("Bad value \n");
        break;

    case (SNMP_ERR_READONLY):
        printf("Read only value \n");
        break;

    case (SNMP_ERR_GENERR):
        printf("Generic error \n");
        break;

    default:
        printf("Everything is fine \n");
}
~sind
 
Old 08-24-2005, 09:16 AM   #3
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
Thaths my problem.. b ut i can really understand why the break is necessary...... All the case statements are not valid... The mystery is not why the execution falls to every category but why every statement seems to be true....
Thx for your time
 
Old 08-24-2005, 09:56 AM   #4
mic
Member
 
Registered: Jun 2005
Posts: 44

Rep: Reputation: 16
It's like sind told you, but maybe you didn't completely understand what he was trying to say.

The condition with SNMP_ERR_NOSUCHNAME is true, others may not be true. However, all statements after finding "true" in the switch block get executed. Unless you put "break;"

Try the sind's code, you'll see it works as it should.
 
Old 08-24-2005, 10:05 AM   #5
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
Thx a lot both of u ... u were right
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting sLoPPy cASE to Pretty Case with tr lowpro2k3 Programming 4 04-13-2005 09:13 PM
Why are all my upper case files being shown as lower case?? [Kernel 2.6.9-1.667 FC3] t3gah Fedora 4 03-11-2005 05:09 PM
Case Statement With Aix Ksh Scripting ']['HeBroken AIX 2 02-09-2005 01:44 PM
KSH Scripting case statement..... ']['HeBroken Programming 1 12-10-2004 11:38 AM
bash'ed by case statement??? 3inone Programming 2 04-29-2004 05:52 PM


All times are GMT -5. The time now is 05:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration