Behaviour of default


 
Thread Tools Search this Thread
Top Forums Programming Behaviour of default
# 1  
Old 12-09-2003
Network Behaviour of default

Hi,

If I have the following code :

int i=2;
switch(i)
{
case 1 : {};break;
default : { printf("d"); };break;
case 2 : { printf("2"); };break;
}


what will be the output?

I had an understanding that case statements are taken sequentially so default will be executed. But when I tried this program it is printing 2.

Can you please help me with this??Smilie
# 2  
Old 12-09-2003
default comes when the i does not match any of the specified values given in the cases that is 1 and/or 2

Regards
JK
# 3  
Old 12-09-2003
A modern compiler may be better than an older one when interpreting your code.

Also a different C compiler may compile your code differently than the one you are using.

If you mention your OS and compiler maybe some others could try your code out on in a different environment to see if you there is another answer.

In HP-UX 10.20 C the answer displayed is 2.

I would say that missing the 'break' in a C switch statement is employing a feature of the compiler, not the language. Even though I have used it myself in the past it could lead to problems in the future if the complier or OS was changed.

A similar trick in C is something like

if (flag)
{
/* do some stuff */
}

Here you are relying on the compiler to interpret the variable in a certain way. With compilers nothing is guaranteed and syntax like that in C could just as easily not work the way that you wan it to. Better to say exactly what you mean rather than imply:

if (flag == 1)
{
/* do some stuff */
}

C is a very powerful language precisely because it will almost let you get away with anything. Just as long as the syntax looks ok a compiler will produce any old executable. But it might not work though.

Remember YOU are the one doing the thinking, not the compiler Smilie

MBB
# 4  
Old 12-09-2003
I guess all the options are read at once and default is only used as a last option. Is there some reason you want "default" to execute, soorajmu, instead of the correct option?
# 5  
Old 12-09-2003
I must disagree with mbb. The C language is governed by an ansi standard.

Any compiler that takes soorajmu's code and fails to print "2" is broken. Similiarly, code like:
if(flag)
is explicitly allowed and even encouraged. Every compiler must work with code like that.

The order of the cases in a switch only affect fallthroughs and nothing else. mbb doesn't like fallthroughs and many folks agree. Even K&R cautions against them. But fallthroughs are allowed by the standard.

Fallthroughs also violate the tenets of structured programming. I normally adhere to those tenets with a rigidity that most programmers see as unnatural. For instance, I never have used setjmp/longjmp and I never will. But I actually like fallthroughs. I believe that they can result in more readable code than is possible with pure structured techniques. For example, I like this kind of thing:
Code:
switch(i)
{
      case 1:   function1();
                break;
      default:  printf("Illegal value, defaulting to 2\n"); 
      case 2:   function2();
                break;
}

Now, that is just example code. With only a single call to function2(), I might just duplicate it in the default case and not fallthrough to case 2. But imagine more lines of code. If I duplicate them all, a programmer must compare them to be sure that the default case and the 2 case are exactly the same.
# 6  
Old 12-09-2003
Perderabo has a fair point. A feature of the language then, and the C language is governed by standards.

Even so, standards may change and compilers have been known to have faults ...

Exploiting C's loose typing could cause you more problems that it would solve. If you code fall throughs, undefined conditionals, undefined type casting etc then you leave the door wide open for unpredicatable and/or faulty code.

(The HP 11.00 C compiler is stricter than the HP 10.20 C compiler and traps problems with loose typing that used pass through)

Anyway, just trying to highlight some of the pitfalls with C.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Ps command different behaviour

Hi Experts, ps command behavior in Redhat is such that it outputs all the output(of long lengths). In Unix the ps command output was limited to only 80 chars. In that if you pipe its output to another command hen the 80 chars restriction wouldn't be there. This 80 char limitation will only be... (14 Replies)
Discussion started by: Albert_Pinto7
14 Replies

2. Programming

Sort behaviour

I see strange results when sorting with -n options and I wander if somebody can explain it. Input file and two results: $ cat aa 14 -1 11 -1 0 -1 0 $ sort -u aa -1 0 (1 Reply)
Discussion started by: migurus
1 Replies

3. UNIX for Dummies Questions & Answers

behaviour of awk

Can someone explain how the below awk simulates cat? pandeeswaran@ubuntu:~$ awk '1' file PSAPSR3 3722000 91989.25 2 98 PSAPSR7 1562000 77000.1875 5 95 PSAPUNDO 92000 4087.5625 4 96 pandeeswaran@ubuntu:~$ awk '2' file PSAPSR3 3722000 91989.25 2 98 PSAPSR7 1562000 77000.1875 5 95 PSAPUNDO... (3 Replies)
Discussion started by: pandeesh
3 Replies

4. Programming

different behaviour in fg and bg

fg = foreground bg = background I have a cobol program that I start with a very simple script. The script is not at fault as it has not changed and the program worked in fg and bg before. I have altered the logging in the program and moved my cursor declare to working storage. The program runs... (6 Replies)
Discussion started by: Bruble
6 Replies

5. UNIX for Advanced & Expert Users

eval behaviour

Hi, I have snippet like the following x="1" prompt1="hi" if I say eval echo \$prompt$x then it is giving o/p "hi" if I say `eval echo \$prompt$x` here it is giving 1 ! if I add one more escape character i.e. `eval echo \\$prompt$x` then it is giving "hi" Can you please... (3 Replies)
Discussion started by: shahnazurs
3 Replies

6. Shell Programming and Scripting

cp -R behaviour

i 've noticed the following difference between freebsd cp and gnu cp from the freebsd cp man page: -R ... If the source_file ends in a /, the contents of the directory are copied rather than the directory itself. ... on gnu cp from the man pagewhile on gnu cp manpage: ‘-r'... (2 Replies)
Discussion started by: aegis
2 Replies

7. Shell Programming and Scripting

Why this behaviour of IF condition?

I have a variable, defndata, which is a number (fetched from a file using awk). I want that if defndata is not initialized (that is its not found in the file using awk), then to execute a block of statements, otherwise execute another block. if then .... else ... fi Now this... (4 Replies)
Discussion started by: indianjassi
4 Replies

8. Shell Programming and Scripting

A Strange Behaviour!!!

Can some-one give me a view to this : I have a directory in an unix server, having permissions r-xr-xr-x .This directory is basically a source directory. Now there is another directory basically the destination directory which has all the permissions. Note:I log in as not the owner,but user... (5 Replies)
Discussion started by: navojit dutta
5 Replies

9. Shell Programming and Scripting

Count behaviour when using su -

Gentlemen, OK, I have an odd issue here perhaps someone can shed some light for me. When running a script as its user/owner the below pause/wait works just fine. When a developer has used su - to assume the username the pause does not happen... This is a HPUX11.00 machine, I have copied the blurb... (1 Reply)
Discussion started by: Eronysis
1 Replies

10. Programming

Can some 1 explain why this behaviour

#include <iostream> using namespace std; int main() { const int l_test = 999999999; int *l_ptr = (int*) &l_test; cout<<"Constant Addr:"<<&l_test<<" Value:"<<l_test<<endl; cout<<"Pointer Addr:"<<l_ptr<<" Value:"<<*l_ptr<<endl; *l_ptr = 888888888; // Manipulating... (2 Replies)
Discussion started by: helpmenow
2 Replies
Login or Register to Ask a Question