[Santa Clara University]
Department of Mathematics
and Computer Science
[Return to Math 169 Homepage]

Math 169 Notes -- C Syntax Derivative Languages


Contents


A. Preface

This webpage is titled "C Syntax" Derivative Languages. The title was chosen to reflect the reality that the languages discussed here share some/much of the syntax of C. But sharing syntax is not the same as being different versions of the same language. To appreciate the differences, one needs to know the context and general purpose of the languages. In a sense, there is a lot of similarity between FORTRAN II and Fortran 90/95, even though there are major differences is syntax. But there is a lot of similarity in syntax between C and Java, even though the context and style of the languages are significantly different! It is easy to think of Java as revision of C++ which is a revision of C. But such thinking may mask more fundamental differences. For example, it has been suggested that the best way to understand Java is to try to learn Smalltalk first!

In the book Java Software Solutions: Foundations of Program Design, by John Lewis and William Loftus (Reading: Addison-Wesley, 1998), we read: "... Java should not be thought of as a revision of C++. ... At the heart of Java are important tenents of program design and implementation that are fundamentally distinct from the approach of C++." (p. 609)

In the book Java in a Nutshell, by David Flanagan (Bonn: O'Reilly & Associates, Inc., 1996), we read, "We saw in the last section that close analogies can be drawn between Java and C. Unfortunately for C++ programmers, the same is not true for Java and C++. ... the analogies between Java and C++ are not nearly as strong as those between Java and C. C++ programmers should forget all they know about C++ and read this section carefully, as beginners, to learn Java without the preconceptions that might come with knowing C++." (pp. 49-50)

This webpage tries to link the C Syntax Derivative Languages together because of their similar syntax. But unless one also acknowledges the fundamental differences of purposes and contexts, shifting from one language to another will not be easy. Nevertheless, learning Java, for example, after knowing C or C++ should be less difficult than without knowing C or C++, since one does not have to relearn the syntax for basic constructs such as for-loops, if-statements.

B. C Overview

A brief comparison of C and Pascal is available here.

C was developed by Dennis Ritchie at Bell Labs in New Jersey in the early 1970s. It is the foundation language of the Unix operating system. Unix v. 5 was released to universities in 1973 and was written primarily in C.

C continued the algorithmic style of Algol (also used in Pascal), but without some of the "syntactic sugar," i.e., English keywords. For example, C uses { } in place of BEGIN and END used in Pascal. The terseness of C can lead to a certain unreadability, and has led advocates to sponsor the International Obfuscated C Code Contest. One example of the power of C (and the confusion resulting) can be seen on Math 10, Notes 18 on the "Conditional Operator" (which has links to other terse codes).

C has features similar to what can be done with assembler languages, for example the ability to manipulate bit values. C's use of pointers enables programmers to view arrays as pointers, in a way not possible in other languages.

In contrast to Fortran and COBOL, C does not have an exponentiation operator, and in contrast to Pascal, nested functions are not allowed.

C. C++ Overview

C++ was developed by Bjarne Stroustrup in the early 1980s at Bell Labs. It is partially a superset of C intended to support OOP, but existing C code usually cannot be compiled directly with a C++ compiler without some (minor) changes. C++ can make use of existing C code and header files, in most cases, via separate compilation.

There are two types of changes introduced by C++ into the base C language:

  1. "improvements" to C
  2. support for OOP
Sample Improvements: Sample OOP Features:

D. Java Overview

Java was developed by James Gosling at Sun Microsystems in 1990. The language was based on the syntax of C and C++ but went a step further in incorporating the Object-Oriented paradigm, so that classes are omni-present and classes that are useful for graphics on web pages are built into the language.

After a few years of internal use at Sun, Java was released on the web in 1993 and began to get public attention around 1995 with the official release of version 1.0. Thus 2005 is the 10th anniversary of Java. Version 1.1 became available in 1997. The last official version, now named 5.0 (internally it is 1.5), was released September 2004. Note that there are enough differences between the versions that newer code will not run on older web browsers which are Java-enabled.

Java programs are activated in two stages. First, one compiles the Java code which results in a "class" file (written in byte-code). This compiled code can be interpreted by any machine that has a Java intepreter (or, if written for a web page, by a web browser). Because Java is not compiled directly for a specific platform, it is much slower than other languages. Because Java can be run on a variety of machines, it is more versatile than other languages for certain applications.

Applets are Java programs written for linking to web pages and useful especially for graphical features.

Writing Java Code

The syntax of the logical and mathematical core of Java code is oftentimes indistiguishable from C/C++ code. For example, the following for loop will run correctly in either language:

     sum = 0;
     // Test loop
     for(int i = 1; i < 10; i++)
         {
	   sum = sum + 1.0/i ;
	 }
There are some major differences in the languages, however. An overview of differences between Java and C/C++ can be found at this link.

Embedding Code in a Main Program

The following is a sample Java program into which the code which appears above has been embedded. Output is achieved by a call to the method System.out.println with the desired output included in parentheses. As is seen in the second output line, several items may be included together within the parentheses, joined by a plus sign. An alternative method, System.out.print may be used instead, which does not advance to the next line after outputting the data.

  class Testjava2 {
    public static void main (String[] args)
    {
      double sum;
      System.out.println("Test program");
      sum = 0;
      // Test loop
      for (int i = 1; i < 10; i++)
        {
	  sum = sum + 1.0/i;
        }
      System.out.println("Sum =" + sum);
    }
  }
The following is a sample C++ program equivalent to the Java program. Notice that the main differences consist in the presence of the #include statement, the main statement, and the output functions.
  #include<iostream.h>

  int main ()
    {
      double sum;
      cout << "Test program" << endl;
      sum = 0;
      // Test loop
      for (int i = 1; i < 10; i++)
        {
  	  sum = sum + 1.0/i;
        }
      cout << "Sum =" << sum << endl;
      return 0;
    }

Running Self-Standing Java Code

A self-standing Java program is compiled and executed according to the following steps.

Further information about Applets and other Examples are available.

Java and Pointers

It is commonly said that Java does not have pointers. It would be better to say that Java does not have explicit pointers in the same way that C and C++ do. Some people prefer to say that in Java everything is a pointer, but the syntax hides it from the user. (In a sense, in LISP and Scheme, everything is a pointer as well, even though we don't normally think of LISP identifiers as pointers as such.)

Perhaps this is best understood when one studies the way new is used in C++ and in Java.

In C++ new is usually used to allocate space for the object pointed to by a pointer variable. It can be also be used to allocate dynamic dynamic arrays. For example,

        node * p;  // this allocates space for
	           // p which can point to a node.
	p = new node;  // this actually allocates space
	           // for a node and links p to that space.
	int * a;   // this allocates space for
	           // a which can point to (one or more) int 
		   // variables.
	a = new int[20]; // this allocates space for
	           // 20 elements for array a.
Thus, in C++ even though one does not consider a to be a pointer, it can be thought in that way especially if dynamically allocated.

In Java, arrays must be allocated in this dynamic fashion via the operator new. For example,

        int[] a = new int[20]; // This both declares a as an
	           // array (pointer) and allocates space.

Evaluation of Java

For an interesting analysis of the "hype" about Java (and debunking of some of the myths), read Making Sense of Java by Hank Shiffman at Silicon Graphics. Among other things, Shiffman addresses issues regarding speed of Java relative to other languages (C++ and Fortran) and issues regarding how widespread Java may become.

A good overview of Java may be found on-lineon the Wikipedia free encyclopedia (link).

E. Perl Overview

Perl is an acronym for "Practical Extraction and Report Language" developed by Larry Wall in the late 1980s. The latest (as of February 2006) stable version release is numbered 5.8.8. Perl is a type of scripting shell language based on the syntax of C. In the last few years, Perl has increased in popularity as a means of processing CGI ("Common Gateway Interface") web related files and applications.

A shell script is a program that consists of commands to the operating system. Such a "program" is stored in what is called a "script file." A Perl program is similar to a shell script, but Perl commands are first compiled and then executed. Perl can make also use of expressions based on formal language theory, such as regular expressions, for the purpose of examining text files.

An online introduction to Perl is available at this link.

Sample Code: Hello World

Each Perl file begins with a special comment line (which begins with a # sign) indicating that it is a Perl file by giving the location of the compiler. Then the body of the code is written, which can look very similar to C code, but without being enclosed in any function skeleton. Thus, the following code will run on the "math" HP Unix server.

        #!/opt/perl5/bin/perl -w
	print ("Hello, world!\n");
This file (assume its name is testperl1 has to be understand by the system as an "executable" file, so after its creation, it should be made executable by giving the following command:
        chmod 700 testperl1
All one then has to do is type the filename (also forcing the system to look for the file in the current directory, if necessary, with an initial ./) at the system prompt, e.g.,
        testperl1
or (if needed)
        ./testperl1

Some Features

Perl makes use of several standard C/C++/Java structure, e.g., for, if ... else. It does not require variables to be declared before use, but variable names must begin with a $. The following is another sample code highlighting the similarity to C syntax.

        #!/opt/perl5/bin/perl -w
	for ( $i = 1; $i <= 10; $i++)
	{
	   print ("Hello, world! Number $i \n");
	}

Purpose/Context

Unlike C/C++, Perl was meant to assist users to handle text files, such as those generated from on-line forms associated with web pages. Because of this context, Perl programmers must think in terms of programs suited for recognizing character patterns rather than "crunching numbers."


This page is maintained by Dennis C. Smolarski, S.J.
dsmolarski at math.scu.edu
Last updated: 20 September 2007.