Sponsored Content
Full Discussion: Behaviour of default
Top Forums Programming Behaviour of default Post 44641 by Perderabo on Tuesday 9th of December 2003 10:48:01 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
setjmp(3UCB)					     SunOS/BSD Compatibility Library Functions					      setjmp(3UCB)

NAME
setjmp, longjmp, _setjmp, _longjmp - non-local goto SYNOPSIS
/usr/ucb/cc [ flag ... ] file ... #include <setjmp.h> int setjmp(env) jmp_buf env; void longjmp(env, val) jmp_buf env; int val; int _setjmp(env) jmp_buf env; void _longjmp(env, val) jmp_buf env; int val; DESCRIPTION
The setjmp() and longjmp() functions are useful for dealing with errors and interrupts encountered in a low-level subroutine of a program. The setjmp() function saves its stack environment in env for later use by longjmp(). A normal call to setjmp() returns zero. setjmp() also saves the register environment. If a longjmp() call will be made, the routine which called setjmp() should not return until after the longjmp() has returned control (see below). The longjmp() function restores the environment saved by the last call of setjmp(), and then returns in such a way that execution continues as if the call of setjmp() had just returned the value val to the function that invoked setjmp(); however, if val were zero, execution would continue as if the call of setjmp() had returned one. This ensures that a ``return'' from setjmp() caused by a call to longjmp() can be distinguished from a regular return from setjmp(). The calling function must not itself have returned in the interim, otherwise longjmp() will be returning control to a possibly non-existent environment. All memory-bound data have values as of the time longjmp() was called. The CPU and floating-point data registers are restored to the values they had at the time that setjmp() was called. But, because the register storage class is only a hint to the C compiler, variables declared as register variables may not necessarily be assigned to machine registers, so their values are unpredictable after a longjmp(). This is especially a problem for programmers trying to write machine-independent C routines. The setjmp() and longjmp() functions save and restore the signal mask while _setjmp() and _longjmp() manipulate only the C stack and regis- ters. None of these functions save or restore any floating-point status or control registers. EXAMPLES
Example 1 Examples of setjmp() and longjmp(). The following example uses both setjmp() and longjmp() to return the flow of control to the appropriate instruction block: #include <stdio.h> #include <setjmp.h> #include <signal.h> #include <unistd.h> jmp_buf env; static void signal_handler(); main() { int returned_from_longjump, processing = 1; unsigned int time_interval = 4; if ((returned_from_longjump = setjmp(env)) != 0) switch (returned_from_longjump) { case SIGINT: printf("longjumped from interrupt %d ",SIGINT); break; case SIGALRM: printf("longjumped from alarm %d ",SIGALRM); break; } (void) signal(SIGINT, signal_handler); (void) signal(SIGALRM, signal_handler); alarm(time_interval); while (processing) { printf(" waiting for you to INTERRUPT (cntrl-C) ... "); sleep(1); } /* end while forever loop */ } static void signal_handler(sig) int sig; { switch (sig) { case SIGINT: ... /* process for interrupt */ longjmp(env,sig); /* break never reached */ case SIGALRM: ... /* process for alarm */ longjmp(env,sig); /* break never reached */ default: exit(sig); } } When this example is compiled and executed, and the user sends an interrupt signal, the output will be: longjumped from interrupt Additionally, every 4 seconds the alarm will expire, signalling this process, and the output will be: longjumped from alarm SEE ALSO
cc(1B), sigvec(3UCB), setjmp(3C), signal(3C) NOTES
Use of these interfaces should be restricted to only applications written on BSD platforms. Use of these interfaces with any of the system libraries or in multi-thread applications is unsupported. BUGS
The setjmp() function does not save the current notion of whether the process is executing on the signal stack. The result is that a longjmp() to some place on the signal stack leaves the signal stack state incorrect. On some systems setjmp() also saves the register environment. Therefore, all data that are bound to registers are restored to the values they had at the time that setjmp() was called. All memory-bound data have values as of the time longjmp() was called. However, because the register storage class is only a hint to the C compiler, variables declared as register variables may not necessarily be assigned to machine registers, so their values are unpredictable after a longjmp(). When using compiler options that specify automatic register alloca- tion (see cc(1B)), the compiler will not attempt to assign variables to registers in routines that call setjmp(). The longjmp() function never causes setjmp() to return 0, so programmers should not depend on longjmp() being able to cause setjmp() to return 0. SunOS 5.11 7 Apr 1993 setjmp(3UCB)
All times are GMT -4. The time now is 06:36 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy