comp.lang.ada
 help / color / mirror / Atom feed
From: sun-barr!cs.utexas.edu!zaphod.mps.ohio-state.edu!sol.ctr.columbia.edu!ira .uka.de!chx400!sicsun!disuns2!lglsun!nebbe@ames.arc.nasa.gov  (Robb Nebbe)
Subject: Re: FORTRAN bug(was Re: C++ vs. Ada -- Is Ada loosing?)
Date: 11 Dec 92 15:38:11 GMT	[thread overview]
Message-ID: <1992Dec11.163811@lglsun.epfl.ch> (raw)

Michael Feldman:

   int x;
   ...
   x = 1;
   while (x <= 10);
   { 
      printf("%d\n", x);
      x++;
   }

For 10 points on your grade: what is printed? Why?
Try explaining it to a freshman.


Fred McCall:
 
Nothing is printed.  You built an infinite loop.  This is hard to
understand?  You made a while loop with an empty body and a condition
that is never met because you never increment x.  Your freshmen don't
get this?

Me:

Good syntax means that code should do what it looks like it should do. The
above code is an example of less than optimal bordering on poor language
syntax. The reason being that many compilers don't detect that what is
obviously wrong code is wrong. On top of that the problem is rather inocuous
and if I didn't know that the problem was there I might miss it when reading
through the code. Basically the presence of a typo must be detected by having
your program hang. A far from optimal situation.

The far worse problem with C syntax is the one that caused the problem for
ATT. (This is all second hand so anyone in the know should correct me if I'm
wrong.) A programmer had left out a break in a switch statement. I don't
know the what the actual code was like by a switch looks like this.

   switch (x)
   {
      case 1:
         printf("x == 1\n");
         break;
      case 2:
         printf("x == 2\n");
         break;
      default:
         printf("x != 1 && x != 2\n");
   } 
   printf("done");

The break is necessary to leave the switch because the cases only serve as
labels and execution will fall through to the next case statement. This is a
case (no pun) where assembler shows through the C syntax. The machine code that
is generated for the switch is a jump table based on the value of x. At the end
of the statements another jump is required to avoid executing the rest of the
code from the other case statements. 

The fact that these problems exist in C is well known and most C texts point
them out. I would like to point out that these idiosyncracies have not
prevented C from being succesfully used in a phenomenal amount of code.

People need to put C in perspective. When people started using C it was 
because the choice was between assembler and C for many projects. For larger
projects the choice was between something like FORTRAN with some assembler
and C. My experience in mixing assembler and FORTRAN is very limited but it is
far from a trivial endevour. In both cases I would unhesitatingly choose C.

Now there are other choices such as Ada for doing programs that interface with
the hardware. Why do people keep using C? A lot of it has to do with inertia.
Another reason that we shouldn't forget is that a good C/C++ programmer can
turn out some very high quality code and if it aint broke don't fix it. In 
fact I would go as far as to say that well written C/C++ code is almost as
readable as Ada.

The problem with Ada is that it is a lot more difficult to do code generation
in my head ;-) Some of you will scoff but my education is in computer
engineering (thus hardware) and there is a tremendous amount a satisfaction in
knowing what it is that I am telling the hardware to do and not just in the
abstract sense.

When I program in Ada this sense of satisfaction is just not there. Programming
in C is sort of like making a piece of furniture yourself from scratch and
programming in Ada is like buying a kit. 

However I put such personal feelings aside when I have to do a project because
my level of confidence in 5000 lines of Ada is definitely higher than my
level of confidence in 5000 lines of C/C++. This isn't that I'm a better
programmer in Ada than in C/C++. To the contrary I'm sure that I master C
better than Ada but I probably know Ada better than C++. The reason is that
when I write code in Ada it usually won't compile if it wasn't what I meant.
In C/C++ the compiler isn't as helpful. For novice Ada programmers however this
is viewed as frusterating because the compiler won't except their code and it
is what they mean :^)

Michael Feldman:
|> >Any similar idiosyncracies in Ada? 

Fred McCall:
|> 
|> Single character ones?  Probably not, but that just indicates that Ada
|> is incredibly more verbose than C.  One of the things I DISlike about
|> it, by the way.

Me:

Actually the fact that Ada is verbose has absolutly nothing to do with the
fact that it doesn't have any similar idiosyncracies. The difference is that
the syntax of Ada was studied to avoid such problems and this wasn't a 
concern when they thought up the syntax for C. If they designers of C had been
interested in avoiding such problems it wouldn't have been too hard.

Robb Nebbe                nebbe@lglsun.epfl.ch

P.S. If you don't put the breaks in the switch it would print the following
if x == 1:

x == 1
x == 2
x != 1 && x != 2
done

with breaks and x == 1 it works as one would expect printing

x == 1
done

             reply	other threads:[~1992-12-11 15:38 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1992-12-11 15:38 Robb Nebbe [this message]
  -- strict thread matches above, loose matches on Subject: below --
1992-12-18 21:56 FORTRAN bug(was Re: C++ vs. Ada -- Is Ada loosing?) Michael Feldman
1992-12-18  9:22 agate!spool.mu.edu!uwm.edu!zaphod.mps.ohio-state.edu!caen!uvaarpa!vger.ns
1992-12-16 17:40 Pascal Obry
1992-12-16 16:02 fred j mccall 575-3539
1992-12-15 16:36 enterpoop.mit.edu!bloom-picayune.mit.edu!mintaka.lcs.mit.edu!ogicse!uwm.e
1992-12-15  2:57 Michael Feldman
1992-12-15  2:43 Michael Feldman
1992-12-14 22:15 John Bollenbacher
1992-12-14 18:33 J. Giles
1992-12-14 17:04 agate!spool.mu.edu!yale.edu!qt.cs.utexas.edu!cs.utexas.edu!csc.ti.com!til
1992-12-14 17:00 agate!spool.mu.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!mksol!mccall
1992-12-14 16:55 agate!spool.mu.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!mksol!mccall
1992-12-12 17:53 Michael Feldman
1992-12-12 14:34 Fergus Jam es HENDERSON
1992-12-12 14:20 Fergus Jam es HENDERSON
1992-12-12  6:04 Bob Kitzberger
1992-12-12  0:42 Pascal Obry
1992-12-11 23:22 Tucker Taft
1992-12-11 22:24 John Nestoriak III
1992-12-11 21:31 Michael Feldman
1992-12-11 21:04 Tucker Taft
1992-12-11 13:29 agate!spool.mu.edu!uwm.edu!cs.utexas.edu!csc.ti.com!tilde.csc.ti.com!mkso
1992-12-09  6:02 Michael Feldman
1992-12-08 17:25 J. Giles
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox