Default. (Rather long!)

Kim Chr. Madsen kimcm at diku.UUCP
Wed Jun 20 18:04:52 AEST 1984

Jack Jansen:

>       What does
>     switch(i) {
>        ...
>        switch(j) {
>            ...
>            default: ...
>        }
>    }

First of all it doesn't make sense to just put a switch into a
switch statement, because when you enter the first switch
statement, you just jump to the corresponding case-label and if
noone matches then you jump to the default-label, in the
outermost switch-level. You can see the default-label in each
level as a local label, not known to the switch statements that
surrounds the actual switch label.

When I say that it doen't make sense to just put a switch
statement into another, it's a truth with modifications:

        a) if it has the form:

                switch (a) {
                case 1 : ...;
                case 2 : switch (b) {
                         case 1 : ...;
                         default: ...;
                default: ...;
           it certainly can be usefull as a control statement.

        b) But if you on the other hand construct a switch-
           statement like this:

                switch (a) {
                    switch (b) {
           The switch-statements that lies within the outermost
           switch-statement will never be executed. [sic!]

If you want to see it for yourself then try to run this little

        main ()
            int i = 4,j = 5;

            switch (i) {
                switch (j) {
                case 7 : printf ("something wrong with j!\n");
                default: printf ("j = %d (5)\n",j);
            case 8 : printf ("something wrong with i!\n");
            default: printf ("i = %d (4)\n",i);

>  Or I could even add a 'goto default'.........
>  (Note: my C Ref Man doesn't say that default is a reserved word,
>  so using default as an ordinary label is perfectly legal)

No you couldn't because default is a reserved word, so either
is your C Ref Man corrupted, or you maybe had overlooked the
section where the keywords is listed (-: However If your C Ref
Man does miss this section I have printed it below...

                     C Reference Manual

                     Dennis M. Ritchie

                     Bell Laboratories
               Murray Hill, New Jersey 07974

                        May 1, 1977

2.3 Keywords

The following identifiers are reserved for use as  keywords,
and may not be used otherwise:

             int       extern    else
             char      register  for
             float     typedef   do
             double    static    while
             struct    goto      switch
             union     return    case
             long      sizeof    default
             short     break     entry
             unsigned  continue  auto

The keyword entry is not currently implemented by any
compiler but is reserved for future use.  Some imple-
mentations also reserve the word fortran.

                                        Kim Chr. Madsen.
                                        Dept. of Computer Science
                                        University of Copenhagen
UUCP: ...mcvax!diku!kimcm

More information about the Comp.lang.c mailing list