[comp.lang.modula2] Comparison between C and M2

marti@ethz.UUCP (Robert Marti) (08/11/88)

In article <INFO-M2%88080315570633@DB0TUI11>, HPA111@DE0HRZ1A.BITNET
(Michael Drechsler) writes:

> I need a short objective comparison of the possibilities of M2 and C.
> [ ... ]   Would you please be so kind and fill in the missing entries
> (or correct them) and then send the list to HPA111@DE0HRZ1A.BITNET

	Editorial comment:
	I just know I am gonna get flamed for this posting,
	but what the hack -- err heck  ...

Instead of filling in your feature list, I prefer to make a few general
comments:

There are quite a few drawbacks of C in comparison with Modula-2:
- In C, there is only a single name space for all type, variable and
  function names across all files.  Therefore, (publicly visible)
  names for variables, types, and functions have to be globally unique,
  i.e., unique within the entire program system.  In Modula-2, only
  module names need to be globally unique.  Names of constants,
  variables, and procedures must be unique within a module only.
  Name clashes are resolved by explicitly qualifying the objects
  in question, e.g., M1.obj vs M2.obj.
- Since there are no modules in C, you have to do "module initialization"
  by providing a special initialization function.  (Since C allows
  initialization of variables as part of the declaration of a variable
  you don't have the need for special initialization code very often,
  though.)
- As a consequence of the lack of true VAR parameters in C, you need to
  use pointers much more often than in Modula-2.  Unfortunately, bugs
  with pointers tend to be hard to find since your program typically
  crashes at a location far away from the erroneous statement.
- The C preprocessor must be used with care.  Also, since the *real* C
  compiler never sees your preprocessor declarations, it cannot include
  this information in the symbol tables generated for a symbolic debugger.
- C syntax is cryptic and allows you to do lots of things within a
  single expression.  [ Canonical example:  while (*t++ = *s++) ; ]
  This is not a problem if you are writing code since you can easily
  avoid the tricky stuff, but it can be a problem if you have to read C
  code written by someone else.  (If you use C a lot, you'll soon get
  familiar with tricky expressions though.)
- Current C compilers usually do almost no type checking.  You absolutely
  need an additional tool (such as lint on UNIX systems) which does this!
  (ANSI C should be much better in this respect.)

In my opinion, there is practically only a single advantage of C over
Modula-2, but it may be a very important one to you or your employer:
- There are (de-facto) standards for both the language and -- probably
  even more importantly -- a basic set of library functions.
  Some people might say that this library is actually an entire operating
  system named UNIX, but even in non-UNIX environments, you'll find pretty
  much the same basic libraries (stdio, string, ctype, math).  Moreover,
  because of the existence of an accepted standard and the widespread
  use of C, you'll find that interfacing C programs to other software
  -- e.g., database management systems (Embedded SQL), or window systems
  (X Windows) -- is usually straightforward whereas interfacing
  Modula-2 to the same software can be a royal pain.

To sum it up:  I feel that Modula-2 is a better language than C.
However, if portability and interfacing to other software systems
is important to you, you may find that you are better off with C.

Of course, you might also want to look at C++ which not only
remedies some of C's deficiencies but also introduces some
interesting new concepts.  (In my opinion, C++ also introduces
a couple of new deficiencies though.)


-- 
Robert Marti                    Phone:       +41 1 256 52 36
Institut fur Informatik
ETH Zentrum                     CSNET/ARPA:  marti%ifi.ethz.ch@relay.cs.net
CH-8092 Zurich, Switzerland     UUCP:        ...uunet!mcvax!ethz!marti