The Artima Developer Community
The C++ Source | C++ Community News | Discuss | Print | Email | First Page | Previous | Next
Sponsored Link

The C++ Source
In the Spirit of C
Opinion
by Greg Colvin
June 21, 2004

Page 1 of 3  >>

Advertisement

Summary
Veteran developer Greg Colvin traces the evolution of C, C++ and Java with an eye to a better future.

In what sense can C, C++, and Java be described as sharing a spirit? At the most superficial level, they look similar. For instance, the following code fragment, placed in an appropriate context, will compute the greatest common divisor using Euclid's algorithm in all three languages:

int gcd(int m, int n) {
   while( m > 0 ) {
      if( n > m ) {
         int t = m; m = n; n = t;
      }
      m -= n;
   }
   return n;
}

But when we speak of sharing a spirit we are speaking of some essential commonality, beyond mere syntax. In search of that essence, let's turn to the Rationale for the first ANSI C standard:

The Committee kept as a major goal to preserve the traditional spirit of C. There are many facets of the spirit of C, but the essence is a community sentiment of the underlying principles upon which the C language is based. Some of the facets of the spirit of C can be summarized in phrases like
  1. Trust the programmer.
  2. Don't prevent the programmer from doing what needs to be done.
  3. Keep the language small and simple.
  4. Provide only one way to do an operation.
  5. Make it fast, even if it is not guaranteed to be portable.
The last proverb needs a little explanation. The potential for efficient code generation is one of the most important strengths of C. To help ensure that no code explosion occurs for what appears to be a very simple operation, many operations are defined to be how the target machine's hardware does it rather than by a general abstract rule.

In many ways the B programming language is the truest embodiment of this Spirit, and the evolution since then can be seeing as trading off among the five facets above. More on this later.

"Twenty years of schoolin' and they put you on the day shift" (Bob Dylan)

First, a little history of my own love affair with C. Back in 1983, armed with a fresh Ph.D. in psychology, I joined my mentor Peter Ossorio at an artificial intelligence startup. I was hired to do linguistic analysis, but when I arrived it rapidly became clear that the work that most needed doing was programming. The company had a contract to deliver a text analysis and retrieval system to run on the latest VAX computers, and had only a prototype system coded in FORTRAN. And not the fairly standard FORTRAN then accepted by the VMS compiler, but an older, heavily extended dialect that was no longer supported. Our resident mathematician, having debugged the prototype code when he arrived, decided it was time to recode the software in C. "Why C?" I asked. "Because", he said, laconic as ever, and gave me a copy of Kernigan & Ritchie.

We were too poor to lease time on a VAX, so we had to share a PC clone. It had 128K of RAM and two 320K floppy drives, one to hold the Lattice C compiler and libraries, the other to hold all our code, which had to run in 64K anyway, since MSDOS used the other 64K. As I recall the 8088 CPU was clocked at a screaming 4.77 MHz. Looking back it is clear that nothing but a small and simple language could have worked in that environment.

So, with K&R in hand and a C compiler on floppy, we set to work. The notational power of C quickly became clear, as I easily implemented my matrix algebra codes (real programmers can write FORTRAN in any language) and our mathematician turned his pattern matching theorems into recursive gems of dense pointer arithmetic. The efficiency of C then became obvious, as all our code fit into memory and ran faster than we had any reason to hope. When we were confident we had the code right we scheduled two weeks to port to VMS, using borrowed time on the nearby university's VAX. It took only two days, with just two bugs, caused by my thinking that ints would hold only 16 bits. Near the end of my second day at the university computer center I recall a student looking over my shoulder and asking "What's that?" "C code," said I. "What's C?" asked he. "Portable structured assembly language," said I. "Just what the world needs," said he, and I'm still not sure if he was being serious or sarcastic. But I was in love, and there was no looking back.

Page 1 of 3  >>

The C++ Source | C++ Community News | Discuss | Print | Email | First Page | Previous | Next

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us